so I have this spritesheet (4 sprites in a row and 3 in a coloumn) which I use to animate a character in a game I make. It animates just fine without a problem, like I want it to
the problem start to arise when I want to change the state from “dash” (running to the enemy) to “attack” (well, attack the enemy) it doesn’t seem to play the attack sprite from the start (index 0)
I’ve used self._currentFrame = 3 on the set_state(self) function so that when the function changes it resets the frame to the third frame, which makes (self.currentFrame + 1) % 4 returns 0
but still, sometimes it doesn’t do what I want, and start the animation at about index 2 or 3 (the end of animation). How do I make sure that my animation starts at index 0?
my updating code is as follows, if it helps
self.frameTime += dt
if self.fps is not -1:
while self.frameTime > 1.0 / self.fps:
self.frameTime -= 1.0 / self.fps
self.currentFrame = (self.currentFrame + 1) % 4
self.currentVFrame = (self.currentVFrame + 1) % 3
Thanks Andrew, but I’ve solved it. It turned out I just need to adjust the
self.fps(the animation fps, not the screen) so that1.0 / self.fpsis smaller thanself.frameTimeThanks Andrew, is there any way I can give you reputation? or end this question?
edit:
why the currentFrame printed as 3?
The Actor class (the class that inherited from AnimatedGameObject) has a
set_state(self, newState)function (which changes theself.currentFrameto 3) that changes the state, so that the update code above (as I’ve explained) returns 0 and the animation starts at the beginning of the frameThe problem is when the
self.fpsin theupdate(self, dt)function has a value of 4, the1.0 / self.fpshas a lesser value than theself.frameTimeand as the effect, theself.currentFrameis not set to 0 at the beginning of the animationSo I actually just need to double the
self.fpsto 8 and the code works just like I want it to