Page 1 of 1

Need help with grid system

Posted: Tue Mar 12, 2013 11:35 pm
by florodude
Here's what I want it to do. Every second, the player moves 100 pixels to the right. Here's my code

Code: Select all

playerxx = playerxx + (100 * (10 * pdt))
Playerxx = player's x coord
pdt = my var for delta time

As you can see, this code would indeed move the player right 100, but it also moves every pixel in between,
how can I get it to jump that 100 pixels?

Re: Need help with grid system

Posted: Wed Mar 13, 2013 12:01 am
by iPoisonxL
You want him to be.. gridlocked, yes?

Well, you're going to need the function love.keypressed. That function calculates if you PRESS a key, and not how long you hold it down.

Code: Select all

function love.keypressed(key)
  if key=='right' then
    playerxx=playerxx+100
  end
end
Now, lets look at that. You call the function, that's simple enough. But they field, there's "key" in it. What is that? Well, it's the key you press. "k" also works instead of "key", but I thought I'd show you with "key".
Then, we check IF the KEY that was PRESSED is the right arrow, then increase playerxx by 100!
I think you can figure out the rest yourself. If you need to use dt in it, put love.keypressed in love.update.

EDIT:

Upon reading it once again, it looks like you want him to move 100 pixels every.. second? Well, you'd need to set up a timer for that.

Code: Select all

function love.update(dt)
  timer=0
  timer=timer+dt
  if (timer>1) then
    playerxx=playerxx+100
    timer=0
  end
end
As you can see in this, you set a timer variable to ZERO. Then, you increase the timer's value by dt, which is the amount of TIME in one FRAME.
Since this is ran every frame, once the timer is greater than 1, it will make him move to the right, and reset the timer to 0. Makes sense?