[Solved] help with animation drawing code.

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
FoundationIDE
Prole
Posts: 6
Joined: Wed Jun 05, 2013 6:51 pm

[Solved] help with animation drawing code.

Post 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?
Last edited by FoundationIDE on Sun Jun 09, 2013 8:25 pm, edited 1 time in total.
Joemag
Prole
Posts: 24
Joined: Sun Apr 14, 2013 5:42 pm

Re: help with animation drawing code.

Post 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
FoundationIDE
Prole
Posts: 6
Joined: Wed Jun 05, 2013 6:51 pm

Re: help with animation drawing code.

Post by FoundationIDE »

Basically I am trying to get CurrentAnimationTile_Player to change between PlayerAni1 and PlayerAni2 every second.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: help with animation drawing code.

Post 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.
FoundationIDE
Prole
Posts: 6
Joined: Wed Jun 05, 2013 6:51 pm

Re: help with animation drawing code.

Post 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 :')
Post Reply

Who is online

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