Moving bird.

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
MxMCube
Prole
Posts: 8
Joined: Thu Aug 21, 2014 11:56 pm

Moving bird.

Post by MxMCube »

Image
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)
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Moving bird.

Post 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.
MxMCube
Prole
Posts: 8
Joined: Thu Aug 21, 2014 11:56 pm

Re: Moving bird.

Post by MxMCube »

Thanks a ton dude! This did the job. I never thought of making bird.direction. Thanks ;)
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Moving bird.

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Moving bird.

Post 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.
Post Reply

Who is online

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