Page 1 of 1
Moving bird.
Posted: Fri Aug 22, 2014 12:04 am
by MxMCube
This is my game so far. Ignore the background. xD
I have this:
Code: Select all
bird = {}
bird.x = 0
bird.y = height/2
bird.speed = 300
What I want, is that the bird.x goes from 0 to 854 then back to 0 then back to 854, and so on. I want it to go 300 (bird.speed) pixels forward every frame.
Please help,
Maxim Deprez (MxMCube)
Re: Moving bird.
Posted: Fri Aug 22, 2014 1:20 am
by veethree
First, Give the bird the following variables:
Width: You can get these from the bird image using image:getWidth(), this will be used when detecting the collision for the right side of the screen.
Direction: This can be a String, Boolean or a number. It needs to have be able to 2 different values, One for each moving direction. In the example i used a string.
Code: Select all
screen = {
width = love.graphics.getWidth(),
height = love.graphics.getHeight()
}
function love.load()
bird = {
x = 0,
y = screen.height / 2,
speed = 300,
width = 32,
direction = "right"
}
end
function love.update(dt)
if bird.direction == "right" then
bird.x = bird.x + bird.speed * dt
if (bird.x + bird.width) > screen.width then
bird.x = screen.width - bird.width
bird.direction = "left"
end
else
bird.x = bird.x - bird.speed * dt
if bird.x < 0 then
bird.x = 0
bird.direction = "right"
end
end
end
function love.draw()
love.graphics.rectangle("fill", bird.x, bird.y, bird.width, bird.height)
end
What is happening there is simply depending on the value of "bird.direction" it moves the bird either left or right, If it's going in either direction and hits the edge of the screen, it flips the direction.
Hope this helps, If you need further explanation of the code let me know.
EDIT: Just realized that i assumed you want it to hit the edge then turn around, If 854 isn't the edge of the screen, Replace this line "if (bird.x + bird.width) > screen.width then" with "if bird.x > 854 then". That would also render the birds "width" value obsolete.
Re: Moving bird.
Posted: Fri Aug 22, 2014 1:31 am
by MxMCube
Thanks a ton dude! This did the job. I never thought of making bird.direction. Thanks
Re: Moving bird.
Posted: Fri Aug 22, 2014 7:00 am
by Plu
Sidenote: the bird is currently moving at 300 pixels per second, not frame. This is probably a better idea because 300pixels per frame would be impossible to follow as well as differ heavily from machine to machine depending on how many frames per second it can handle.
Re: Moving bird.
Posted: Sun Aug 24, 2014 4:40 pm
by Jasoco
I'm sorry but I can't ignore the background. You're either a huge fan of Andy Kauffman, a huge fan of Phil Fish or a very confused fan of Jim Carrey.
Also, you could look into utilizing a third-party tweening library like Flux or Tween to handle movement for you. It makes things so much easier with a little bit of effort.
If bird is at destination, create Flux/Tween animation to move to new destination. When new destination point is reached, set birdAdDestination flag to true. And so on.