Trouble with animating an actor while moving

Greetings! I am new to Panda3d and Python, so I apologize if I sound all over the place. I’m not even sure if I’m posting in the right topic, but I’ll hope for the best because this sounded like the place to post my issue! Now that I got that out of the way…

I was having trouble with animating my character whenever he/she tries to walk/turn. The animation simply doesn’t play but the character gets into their position like they are trying to do the animation. I’m using a key map to help my character move. Here’s the code for method that plays the animation:

    # dt here represents delta time
    def move(self, dt):
        if (not self.keyMap["w"] and not self.keyMap["s"] and not self.keyMap["a"] and not self.keyMap["d"]):
                self.body.setH(self.body, dt * 90);
            if (self.keyMap["d"]):
                self.body.setH(self.body, dt * -90);
            if (self.keyMap["w"]):
                self.body.setY(self.body, dt * 10);
            if (self.keyMap["s"]):
                self.body.setY(self.body, dt * -10);

Here is also the key map that I use:

    def setKeyMaps(self):
        self.keyMap = {"w": False,
                       "s": False,
                       "a": False,
                       "d": False};
        # If key is pressed, set to true
        self.accept("w", self.setKey, ["w", True]);
        self.accept("s", self.setKey, ["s", True]);
        self.accept("a", self.setKey, ["a", True]);
        self.accept("d", self.setKey, ["d", True]);
        # If key isn't pressed anymore, set to false
        self.accept("w-up", self.setKey, ["w", False]);
        self.accept("s-up", self.setKey, ["s", False]);
        self.accept("a-up", self.setKey, ["a", False]);
        self.accept("d-up", self.setKey, ["d", False]);

The only other place I do anything with the animation is when I “load” it into my ‘body’ instance of the Actor class.

Edit: In addition, when I don’t have any input from the keys (I don’t have the code for the keys at all,) and I load in the animations one by one to test, the animations work perfectly fine by themselves.

Any thoughts on this matter would be appreciated.

The problem is most likely that you call play () in the loop. The animation gets stuck on the first frame. You need to add logic to block the re-call of play () if it was called.

You can use loop methods.

actor.loop('Animation Name')

Already tried something like this if that is what you were suggesting:


Unfortunately didn’t work, so I tried just doing play() instead.
I am still unsure of what the problem is.

Edit: or did you mean also checking if animations are already playing using something like…


Yes, I think it’s better for you to implement as in Ralph’s example.

        if self.keyMap["forward"] or self.keyMap["left"] or self.keyMap["right"]:
            if self.isMoving is False:
                self.isMoving = True
            if self.isMoving:
                self.ralph.pose("walk", 5)
                self.isMoving = False

1 Like

Oh my goodness, I completely forgot about those examples. Thank you! That answers my question.