Page 1 of 1

RPG-style walking animations with AnAL

Posted: Fri Sep 28, 2012 10:55 pm
by maage
Admittedly, I'm new to love2D (but certainly not to programming). I'm struggling to understand how I can get AnAL to read a sprite sheet and change the character direction based off the key pressed. That is, I want it to be like a Zelda or RPG style walking system where the character's sprite changes based on direction according to user input.

This is the code that I have so far:

Code: Select all

require("AnAL")

SPRITE_WIDTH = 32
SPRITE_HEIGHT = 48

function love.load()	
	img = love.graphics.newImage("sprites/chikaru.png")
	spr = newAnimation(img, SPRITE_WIDTH, SPRITE_HEIGHT, 0.2, 0)
	spr:seek(3)
end

function love.update(dt)

	if love.keyboard.isDown("left") then
		spr:seek(3)
		x = x - 100
		spr:update(dt)
	end
    if love.keyboard.isDown("right") then
		spr:seek(7)
    	x = x + 100
		spr:update(dt)
	end
    if love.keyboard.isDown("up") then
		spr:seek(11)
    	y = y - 100
		spr.update(dt)
	end
    
	if love.keyboard.isDown("down") then
		spr.seek(15)
		y = y + 100
		spr.update(dt)
    end

end

function love.draw()
	love.graphics.draw(spr, x, y)
end
What am I doing wrong? Is there a tutorial someone can point me towards? I feel pretty lost, I've just been looking at other people's source code and trying to figure out what they did.

Re: RPG-style walking animations with AnAL

Posted: Fri Sep 28, 2012 11:53 pm
by SudoCode
Create a variable that is updated on input and draw the animations from that. It's no different than changing the image displayed based on keypress.

Re: RPG-style walking animations with AnAL

Posted: Sat Sep 29, 2012 1:15 am
by verilog
Hello!
This post I wrote about flipping images a while ago might be helpful for you :)

viewtopic.php?f=4&t=9825&p=60239#p60239

Re: RPG-style walking animations with AnAL

Posted: Sat Sep 29, 2012 9:05 am
by T-Bone
You mix

Code: Select all

spr:update(dt)
with

Code: Select all

spr.update(dt)
They mean different things. I think the first one is the one you want, if you call spr.update(dt), the function "update" cannot know what sprite (in this case spr) you are talking about and that seems wrong.