Page 1 of 1

How can I make this walking animation work?

Posted: Wed Jul 04, 2012 11:58 pm
by LaserGuns
Okay, I have 4 walking animation pictures, 2 facing left, 2 facing right. Their names are:
- playerwalk1.png
- playerwalk2.png
- playerwalk1flipped.png
- playerwalk2flipped.png

How can I make it so when I hold down "D" it switches between "playerwalk1flipped.png" and "playerwalk2flipped.png" at 0.5 second intervals, and when I press "A" it switches between "playerwalk1.png" and "playerwalk2.png" at 0.5 second intervals?

This is the code I have so far:

Code: Select all

function love.keyboard.isDown(key,dt)
	if key == "d" then
		player.texture = love.graphics.newImage("images/playerwalk1flipped.png")
	if key == "a" then
		player.texture = love.graphics.newImage("images/playerwalk1.png")

Re: How can I make this walking animation work?

Posted: Thu Jul 05, 2012 1:25 am
by onedaysnotice
use AnAL
https://love2d.org/wiki/AnAL

or anim8
https://love2d.org/wiki/anim8

Makes the process much easier. :) They're easy to understand and use :)

Re: How can I make this walking animation work?

Posted: Thu Jul 05, 2012 4:05 am
by verilog
Hey, man!

Consider using AnAL, also, I'd suggest using the same image for both directions, left and right, and flipping along the X axis according to a “flip” variable. Consider the draw mnemonic (If you use AnAL, you'll pass the same parameters to the draw() function):

Code: Select all

love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy )
Where the “sx” and “sy” parameters define the image scale. You should be able to flip your image along the X axis by passing the following (signed) integers:

sx = 1 (facing right)
sx = -1 (facing left)

The same applies for the “sy” parameter, but this will flip your image along the Y axis.

Something like this should do (pseudo code):

Code: Select all

function player:update(dt)
  if love.keyboard.isDown("right") then

    self.imageFlipX = 1
    self.imageOffsetX = 0 --image x axis remains the same

  elseif love.keyboard.isDown("left") then
 
   self.imageFlipX = -1
   self.imageOffsetX = imageWidth --image x axis has been flipped!

  end
end
Please note that you'll also need to define an offset value along the X axis when the image is flipped (usually, "self.imageOffsetX" will be defined as the width of your image).

Code: Select all

function player:draw()
  love.graphics.draw( drawable, self.positionX, self.positionY, self.rotation, self.imageFlipX, self.imageFlipY, self.imageOffsetX, self.imageOffsetY )
end


Re: How can I make this walking animation work?

Posted: Sat Jul 07, 2012 6:18 am
by LaserGuns
Thank you for all of the responses, we figured it out on our own.