How can I make this walking animation 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
LaserGuns
Prole
Posts: 33
Joined: Sun Apr 29, 2012 12:55 am

How can I make this walking animation work?

Post 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")
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How can I make this walking animation work?

Post 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 :)
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: How can I make this walking animation work?

Post 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

LaserGuns
Prole
Posts: 33
Joined: Sun Apr 29, 2012 12:55 am

Re: How can I make this walking animation work?

Post by LaserGuns »

Thank you for all of the responses, we figured it out on our own.
Post Reply

Who is online

Users browsing this forum: No registered users and 133 guests