How would I create an idle state?

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
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

How would I create an idle state?

Post by iPoisonxL »

The title might be a bit misleading, because I already know how to do an idle.. but it's a bit too long to explain in the title.

First, I have two pictures. One, let's say, idle1.png, and the other, idle2.png. idle1.png would be facing right, for when player was walking right, and the other idle picture would be facing left, for when player was walking left. Now, is it possible for Love2d to detect which side the player was walking last? If so, I would really like to know.

If some of you still don't understand, here's what I mean.
Player is walking right, then stops. The idle would be idle1.png, which is facing right.
Player is walking left, then stops. The idle would be idle2.png, which is facing left.

Help, please?

Here is my code
NOTE: Yes, I have AnAL (Animations And LOVE)

Code: Select all

require 'AnAL'
----
function love.load()
	love.graphics.setBackgroundColor(255,255,255)
	imgIdle = love.graphics.newImage('/sprites/animationidle.png')
	imgLeft = love.graphics.newImage('/sprites/animationleft.png')
	imgRight = love.graphics.newImage('/sprites/animationright.png')
	animIdle = newAnimation(imgIdle, 60, 64, 0.2, 0)
	animLeft = newAnimation(imgLeft, 66, 64, 0.2, 0)
	animRight = newAnimation(imgRight, 66, 64, 0.2, 0)
	x=100
	y=100
	speed=200
end
----
function love.update(dt)
	if (love.keyboard.isDown('left')) then
		face='left'
		x=x-speed*dt
		animLeft:update(dt)
	elseif (love.keyboard.isDown('right')) then
		face='right'
		x=x+speed*dt
		animRight:update(dt)
	else
		face='idle'
		animIdle:update(dt)
	end
end
----
function love.draw()
	if (face=='left') then
		animLeft:draw(x,y)
	end

	if (face=='right') then
		animRight:draw(x,y)
	end

	if (face=='idle') then
		animIdle:draw(x,y)
	end

end
Yes, the player is a cat. Deal with it.
Attachments
iloveyouuu.love
(6.23 KiB) Downloaded 51 times

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How would I create an idle state?

Post by micha »

In the update code you can simply do this:

Code: Select all


function love.update(dt)
  if (love.keyboard.isDown('left')) then
    face='left'
    x=x-speed*dt
    animLeft:update(dt)
  elseif (love.keyboard.isDown('right')) then
    face='right'
    x=x+speed*dt
    animRight:update(dt)
  else
    if face == 'left' then
      face = 'leftidle'
    else
      face = 'rightidle'
    end
    animIdle:update(dt)
  end
end
Which means that you check for the value of face from the previous time step. If it was 'left' then you go to face = 'leftidle', else you go to 'rightidle'
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: How would I create an idle state?

Post by Taehl »

Very easy to do. All you need is a variable for keeping track of which way you're facing. Personally, I'd keep animation state as a variable, too, and save yourself some code. Try this:

Code: Select all

require 'AnAL'
----
function love.load()
   love.graphics.setBackgroundColor(255,255,255)
   imgIdle1 = love.graphics.newImage('/sprites/animationidle1.png')	-- following posted convention about image names
   imgIdle2 = love.graphics.newImage('/sprites/animationidle2.png')
   imgLeft = love.graphics.newImage('/sprites/animationleft.png')
   imgRight = love.graphics.newImage('/sprites/animationright.png')
   animIdle1 = newAnimation(imgIdle1, 60, 64, 0.2, 0)
   animIdle2 = newAnimation(imgIdle2, 60, 64, 0.2, 0)
   animLeft = newAnimation(imgLeft, 66, 64, 0.2, 0)
   animRight = newAnimation(imgRight, 66, 64, 0.2, 0)
   x=100
   y=100
   speed=200
   face,anim=1,animIdle
end
----
function love.update(dt)
   if (love.keyboard.isDown('left')) then
      face=-1
      anim=animLeft
      x=x-speed*dt
   elseif (love.keyboard.isDown('right')) then
      face=1
      anim=animRight
      x=x+speed*dt
   else
      anim = face==1 and animIdle1 or animIdle2
   end
   anim:update(dt)
end
----
function love.draw()
   anim:draw(x,y)
end
----
function love.keypressed(k)
   if k=='escape' then love.event.quit() end
end
Better yet, you could multiply your character's x-scale by by your face direction, which will flip the image for you. Then you wouldn't even need separate left-facing images and animations. Or at least, you can do this with normal images. I'm not sure if AnAL doess scaling (the wiki page doesn't seem to talk about the draw function)...
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: How would I create an idle state?

Post by iPoisonxL »

Taehl wrote:Very easy to do. All you need is a variable for keeping track of which way you're facing. Personally, I'd keep animation state as a variable, too, and save yourself some code. Try this:

Code: Select all

require 'AnAL'
----
function love.load()
   love.graphics.setBackgroundColor(255,255,255)
   imgIdle1 = love.graphics.newImage('/sprites/animationidle1.png')	-- following posted convention about image names
   imgIdle2 = love.graphics.newImage('/sprites/animationidle2.png')
   imgLeft = love.graphics.newImage('/sprites/animationleft.png')
   imgRight = love.graphics.newImage('/sprites/animationright.png')
   animIdle1 = newAnimation(imgIdle1, 60, 64, 0.2, 0)
   animIdle2 = newAnimation(imgIdle2, 60, 64, 0.2, 0)
   animLeft = newAnimation(imgLeft, 66, 64, 0.2, 0)
   animRight = newAnimation(imgRight, 66, 64, 0.2, 0)
   x=100
   y=100
   speed=200
   face,anim=1,animIdle
end
----
function love.update(dt)
   if (love.keyboard.isDown('left')) then
      face=-1
      anim=animLeft
      x=x-speed*dt
   elseif (love.keyboard.isDown('right')) then
      face=1
      anim=animRight
      x=x+speed*dt
   else
      anim = face==1 and animIdle1 or animIdle2
   end
   anim:update(dt)
end
----
function love.draw()
   anim:draw(x,y)
end
----
function love.keypressed(k)
   if k=='escape' then love.event.quit() end
end
Better yet, you could multiply your character's x-scale by by your face direction, which will flip the image for you. Then you wouldn't even need separate left-facing images and animations. Or at least, you can do this with normal images. I'm not sure if AnAL doess scaling (the wiki page doesn't seem to talk about the draw function)...
thanks!! That really helped!
PS. Am I the only one that laughs a bit in my head when I read AnAL?

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: How would I create an idle state?

Post by iPoisonxL »

micha wrote:In the update code you can simply do this:

Code: Select all


function love.update(dt)
  if (love.keyboard.isDown('left')) then
    face='left'
    x=x-speed*dt
    animLeft:update(dt)
  elseif (love.keyboard.isDown('right')) then
    face='right'
    x=x+speed*dt
    animRight:update(dt)
  else
    if face == 'left' then
      face = 'leftidle'
    else
      face = 'rightidle'
    end
    animIdle:update(dt)
  end
end
Which means that you check for the value of face from the previous time step. If it was 'left' then you go to face = 'leftidle', else you go to 'rightidle'
Kitty thanks you! <3
D2dH070.png
D2dH070.png (355.88 KiB) Viewed 211 times

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 64 guests