logic for Animating player facing left / right

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
tehryanx
Prole
Posts: 17
Joined: Fri Apr 04, 2014 8:26 pm

logic for Animating player facing left / right

Post by tehryanx »

The main character in my game can walk left, right, up or down (double dragon style). I've used anim8 to create 4 animations. walk left, walk right, stand left and stand right. The further I get the more I realize the way I'm doing this isn't wokring out. When I move my player up or down I have to check if he's facing left or facing right first, but he could be standing facing left, or walking facing left. Eventually I'll have other states like ducking, jumping, dashing, etc. I don't wan tto have to check them all like I am here:

Code: Select all

	elseif love.keyboard.isDown('up') and player.yvel > -player.speed then
		player.yvel = player.yvel - player.speed * dt
		if player.animstate == player.walkanim_right or player.animstate == player.standanim_right then 
			player.animstate = player.walkanim_right 
so does it make sense to just have a facing = "left" variable somewhere? or is there a better way to do this? I pasted my player.lua below.

Code: Select all

player = {}

function player.load()
	
	-- world coordinates
	player.x, player.y = 50, 100

	-- movement velocity and friction
	player.xvel, player.yvel = 0, 0
	player.friction = 6

	player.speed = 1000

	-- animations from player spritesheet
	player.spritesheet = love.graphics.newImage("cook.png")
	local g = anim8.newGrid(80, 128, player.spritesheet:getWidth(), player.spritesheet:getHeight())

	player.walkanim_left = anim8.newAnimation(g('1-4', 1), 0.1)
	player.standanim_left = anim8.newAnimation(g('1-4', 2), 0.1)
	player.walkanim_right = player.walkanim_left:clone():flipH()
	player.standanim_right = player.standanim_left:clone():flipH()	

	-- set current animation
	player.animstate = player.standanim_right


end

function player.draw()

	player.animstate:draw(player.spritesheet, player.x, player.y)

end

function player.physics(dt)
	player.x = player.x + player.xvel * dt
	player.y = player.y + player.yvel * dt
	player.xvel = player.xvel * (1 - math.min(player.friction * dt, 1))
	player.yvel = player.yvel * (1 - math.min(player.friction * dt, 1))
end 

function player.move(dt)
	if love.keyboard.isDown('right') and player.xvel < player.speed then
		player.xvel = player.xvel + player.speed * dt
		player.animstate = player.walkanim_right
	elseif love.keyboard.isDown('left') and player.xvel > -player.speed then
		player.xvel = player.xvel - player.speed * dt
		player.animstate = player.walkanim_left
	elseif love.keyboard.isDown('down') and player.yvel < player.speed then
		player.yvel = player.yvel + player.speed * dt
		if player.animstate == player.walkanim_right or player.animstate == player.standanim_right then 
			player.animstate = player.walkanim_right 
		else 
			player.animstate = player.walkanim_left 
		end
	elseif love.keyboard.isDown('up') and player.yvel > -player.speed then
		player.yvel = player.yvel - player.speed * dt
		if player.animstate == player.walkanim_right or player.animstate == player.standanim_right then 
			player.animstate = player.walkanim_right 
		else 
			player.animstate = player.walkanim_left 
		end

	else
		if player.animstate == player.walkanim_left then player.animstate = player.standanim_left end
		if player.animstate == player.walkanim_right then player.animstate = player.standanim_right end
	end

end

function player.update(dt)

	player.physics(dt)
	player.move(dt)
	player.animstate:update(dt)

end
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: logic for Animating player facing left / right

Post by verilog »

Hi, tehryanx!
I'd suggest using the same image for both directions, left and right, and flipping along the X axis according to a “flip” variable. The parameters “sx” and “sy” in the draw function are horizontal/vertical scale, but you can pass on negative values to flip an image along a particular axis.

I find that using a 1/-1 variable for storing the (horizontal) direction of a characters is very useful. You can both check which direction he's facing and use the same information to flip the image, something like this:

Code: Select all

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

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

  elseif love.keyboard.isDown("left") then

 
   player.imageFlipX = -1
   player.imageOffsetX = imageWidth --image x axis has been flipped!

  end
end
Just consider that if you use this approach, the image origin will also be flipped, thus, you'll might need to introduce an offset if you want to correctly flip the image according to its center. The offset is usually the width of your sprite.

You can then draw the image like this:

Code: Select all

function player:draw()
  love.graphics.draw( drawable, player.positionX, player.positionY, player.rotation, player.imageFlipX, player.imageFlipY, player.imageOffsetX, player.imageOffsetY )
end
Hope that helps!
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 3 guests