Simple animation function doesn't work

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
Rizon
Prole
Posts: 2
Joined: Sat Jul 16, 2016 6:22 pm

Simple animation function doesn't work

Post by Rizon »

Hi everyone,

I am trying to make a simple animation function. At first the function turned the variables that I put in, into strings somehow. I have solved this problem with the solution given to me on stackoverflow: http://stackoverflow.com/questions/3841 ... -in-love2d.
Now my problem is that only the first image is shown and not second. His upper body should move slightly up and down.

Does anybody know how I can improve my code?

Controls:
3 - Go to the game state
w/up - Move the player up
a/left - Move the player left
d/right - Move the player right
s/down - Move the player down

Code:

Code: Select all

function love.load()
	--Makes images look pixelated
	love.graphics.setDefaultFilter("nearest")
	--Variables
	player = {x = 10, y = 10}
	--Animation
	animate = {frame = nil}	
	function animate:animate(...)
		local i = 1
		self.frame = select(i, ...)
		i = i + 1
		if i < #arg then
			i = 1
		end
	end
	animateRizon = animate
	rizon1 = love.graphics.newImage('rizonstand1.png')
	rizon2 = love.graphics.newImage('rizonstand2.png')
	animateRizon:animate(rizon1, rizon2)
	--Gamestates
	stateIntro = false
	stateMenu = false
	stateGame = false
end



function love.update(dt)
	--Movement player
	function movement()
		if love.keyboard.isDown('escape') then
			love.event.push('quit')
		end

		if love.keyboard.isDown('d', 'right') then
			player.x = player.x + 1
		elseif love.keyboard.isDown('a', 'left') then
			player.x = player.x - 1 
		elseif love.keyboard.isDown('s', 'down') then
			player.y = player.y + 1
		elseif love.keyboard.isDown('w', 'up') then
			player.y = player.y - 1 
		end
	end 
	--Change states in the program
	function stateChange()
		if love.keyboard.isDown('escape') then
			love.event.push('quit')
		end
		if love.keyboard.isDown('1') then
				stateIntro = true
				stateMenu = false
				stateGame = false
		elseif love.keyboard.isDown('2') then
				stateIntro = false
				stateMenu = true
				stateGame = false 
		elseif love.keyboard.isDown('3') then
				stateIntro = false
				stateMenu = false
				stateGame = true
		end
	end 
	
end



function love.draw()
	--Scales everything
	love.graphics.scale(3, 3)
	stateChange()
	if stateIntro == true then
	elseif stateMenu == true then
	elseif stateGame == true then
		love.graphics.setBackgroundColor(0, 0, 255)
		love.graphics.draw(animateRizon.frame, player.x, player.y)
		movement()
	end
end
Attachments
Game.love
My game
(1.99 KiB) Downloaded 91 times
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Simple animation function doesn't work

Post by Positive07 »

Enjoy and learn most of all!
Attachments
Game.love
(2.05 KiB) Downloaded 93 times
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: Simple animation function doesn't work

Post by CanadianGamer »

The way I do it is I make a table of images and an iterator variable and just use the update loop to loop through it
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
Rizon
Prole
Posts: 2
Joined: Sat Jul 16, 2016 6:22 pm

Re: Simple animation function doesn't work

Post by Rizon »

Thanks for the help, it works now :).
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests