Movement Prob

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
hallfour2009
Prole
Posts: 1
Joined: Sat Jan 04, 2014 1:50 pm

Movement Prob

Post by hallfour2009 »

Hello, I am trying to use simple movement that isn't working, here's the code:

Code: Select all

 
         player.xvel = 0
         player.spe = 144000
   function player.move(dt)
	if love.keyboard.isDown("d") then
      player.xvel = player.xvel + (player.spe * dt)
   elseif love.keyboard.isDown("a") then
      player.xvel = player.xvel - (player.spe * dt)
   end 
The error says "Tried to perform arithmetic on local dt (a nil value)".

Thanks!
-Gordon Hall
hallfour2009
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Movement Prob

Post by DaedalusYoung »

When you are calling that function, you don't pass the dt value. Note it's a local value only in the love.update(dt) function, you can't access it unless you pass it.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Movement Prob

Post by Roland_Yonaba »

Fairly normal. To actually have the value of dt, you have to place the right part of your code in a callback function.
Inside love update, precisely.
Roughly, you have to separate your code into pieces, and each piece goes into the right callback. Those callbacks functions are fired by löve, so you do not have to handle them.

Code: Select all

function love.load()
 -- this is where goes all the code the init things, and it is fired once
 -- at the very start of the game
end

function love.updated(dt)
 -- Here, you can get the value of dt. This callback is called periodically, each tick time,
 -- so this is where goes to update things (dynamics, movement, etc).
end

function love.draw()
  -- This is where you draw/render all your entities and stuff 
end
See callbacks, for more details on this.

In your case, you can have this layout:

Code: Select all

function love.load()
  player = {}
  player.xvel = 0
  player.spe = 144000

  function player.move(dt)
    if love.keyboard.isDown("d") then
      player.xvel = player.xvel + (player.spe * dt)
    elseif love.keyboard.isDown("a") then
      player.xvel = player.xvel - (player.spe * dt)
    end 
  end
end

function love.update(dt)
  player.move(dt) -- pass the dt value to the function!
end

function love.draw()
  player.draw() -- eventually, if defined.
end
This might contains some typos, I wrote it from scratch, but hope you get the logic.
And that's your first Löve story, huh ? Welcome to Löve, then! :awesome:
Hope this helps.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Movement Prob

Post by veethree »

You also most likely have to actually change the players position according to the velocity.

Code: Select all

player.x = player.x + player.xvel * dt
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests