Need help with grid system

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
florodude
Prole
Posts: 20
Joined: Mon Jul 30, 2012 10:16 pm

Need help with grid system

Post 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?
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Need help with grid system

Post 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?

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests