Page 1 of 1

[Solved] help with animation drawing code.

Posted: Sun Jun 09, 2013 7:26 pm
by FoundationIDE

Code: Select all

PlayerAni1 = love.graphics.newImage("assets/player/player_1.png")
PlayerAni2 = love.graphics.newImage("assets/player/player_2.png")
I initialized two images which are my two animation frames. seems ok. working fine.

Code: Select all

Player_CurrentAnimationTile = nil
AnimationState_Player = 0
These are my animation varibles. I need Player_CurrentAnimationTile to change between PlayerAni1 & PlayerAni2 every second, which I am having trouble with.
AnimationState_Player goes from 0 to 1 using the following code.

Code: Select all

if AnimationState_Player == 0 then
		AnimationState_Player = AnimationState_Player + dt
	else
		AnimationState_Player = 0
	end
	if AnimationState_Player == 0 then Player_CurrentAnimationTile = PlayerAni1 else Player_CurrentAnimationTile = PlayerAni2 end
and then in love.draw I have the following:

Code: Select all

	love.graphics.draw(Player_CurrentAnimationTile, PlayerPosition_X, PlayerPosition_Y)
it should switch between the two frames every second (according to the wiki) but it just flickers between the two images really, really fast. Any idea what i'm doing wrong?

Re: help with animation drawing code.

Posted: Sun Jun 09, 2013 8:13 pm
by Joemag
The error is in the following code: (I just beautified it)

Code: Select all

if AnimationState_Player == 0 then
      AnimationState_Player = AnimationState_Player + dt
else
      AnimationState_Player = 0
end

if AnimationState_Player == 0 then 
    Player_CurrentAnimationTile = PlayerAni1 
else 
    Player_CurrentAnimationTile = PlayerAni2 
end
I don't know what you tried to do, but what this code does is:

1. possibility - Running this code with AnimationState_Player = 0 :
  1. AnimationState_Player is 0, so AnimationState_Player will be set to AnimationState_Player + dt
  2. now AnimationState_Player is not 0 so Player_CurrentAnimationTile = PlayerAni2
  3. next time it will be the 2. possibility
2. possibility - Running this code with AnimationState_Player > 0 :
  1. AnimationState_Player is not 0, so AnimationState_Player will be set to 0
  2. now AnimationState_Player is 0 so Player_CurrentAnimationTile = PlayerAni1
  3. next time it will be the 1. possibility
So Player_CurrentAnimationTile changes every time

Re: help with animation drawing code.

Posted: Sun Jun 09, 2013 8:18 pm
by FoundationIDE
Basically I am trying to get CurrentAnimationTile_Player to change between PlayerAni1 and PlayerAni2 every second.

Re: help with animation drawing code.

Posted: Sun Jun 09, 2013 8:20 pm
by micha
This is what you want:

Code: Select all

AnimationState_Player = AnimationState_Player + dt
if AnimationState_Player > 1 then
  AnimationState_Player = AnimationState_Player - 1
  if Player_CurrentAnimationTile = PlayerAni1 then
    Player_CurrentAnimationTile = PlayerAni2
  else
    Player_CurrentAnimationTile = PlayerAni1
  end
end
This uses AnimationState_Player as a time counting variable. As soon as the time passed is more than one second, then the image is changed and 1 second is subtracted from the counted time.

Re: help with animation drawing code.

Posted: Sun Jun 09, 2013 8:24 pm
by FoundationIDE
micha wrote:This is what you want:

Code: Select all

AnimationState_Player = AnimationState_Player + dt
if AnimationState_Player > 1 then
  AnimationState_Player = AnimationState_Player - 1
  if Player_CurrentAnimationTile = PlayerAni1 then
    Player_CurrentAnimationTile = PlayerAni2
  else
    Player_CurrentAnimationTile = PlayerAni1
  end
end
This uses AnimationState_Player as a time counting variable. As soon as the time passed is more than one second, then the image is changed and 1 second is subtracted from the counted time.
Works Perfectly :')