Hello all, I am following along with Fireclaw the Fox’s book: “Panda 3D Game Development: Game Development for everyone” and I am stuck at the part of User Interactions. At the moment the character model does move, and also the model stops its idle animation when moving. However, the walking animation does not begin and the model is static while moving.
I need help figuring out what I am missing.
Here is the code:
#!/usr/bin/python
__author__ = "Fireclaw the Fox"
__license__ = """
Simplified BSD (BSD 2-Clause) License.
See License.txt or http://opensource.org/licenses/BSD-2-Clause for more info
"""
# Panda3D imports
from direct.actor.Actor import Actor
from direct.fsm.FSM import FSM
from panda3d.core import KeyboardButton
class Player(FSM):
def __init__(self, charId, charNr):
FSM.__init__(self, "FSM-Player{}".format(charNr))
self.charId = charId
charPath = "characters/character{}/".format(charNr)
self.character = Actor(
charPath + "char", {
"Idle":charPath + "idle",
"walk":charPath + "walk",
"punch_l":charPath + "punch_l",
"punch_r":charPath + "punch_r",
"kick_l":charPath + "kick_l",
"kick_r":charPath + "kick_r",
"Hit":charPath + "hit",
"defend":charPath + "defend"
})
self.character.setH(90)
self.character.reparentTo(render)
self.character.hide()
self.walkSpeed = 2.0 # units per second
self.leftButton = KeyboardButton.asciiKey("d")
self.rightButton = KeyboardButton.asciiKey("f")
def start(self, startPos):
self.character.setPos(startPos)
self.character.show()
self.request("Idle")
taskMgr.add(self.moveTask, "move task {}".format(self.charId))
def stop(self):
self.character.hide()
taskMgr.remove("move task {}".format(self.charId))
def enterIdle(self):
self.character.loop("Idle")
def moveTask(self, task):
speed = 0.0
isDown = base.mouseWatcherNode.isButtonDown
if isDown(self.leftButton):
speed += self.walkSpeed
if isDown(self.rightButton):
speed -= self.walkSpeed
yDelta = speed * globalClock.getDt()
self.character.setY(self.character, yDelta)
if speed != 0.0 and self.state != "Walk" and self.state != "Walk_back":
if speed < 0:
self.request("Walk")
else:
self.request("Walk_back")
elif speed == 0.0 and self.state != "Idle":
self.request("Idle")
return task.cont
def exitIdle(self):
self.character.stop()
def enterWalk(self):
self.character.loop("Walk")
def exitWalk(self):
self.character.stop()
def enterWalk_back(self):
self.character.loop("Walk_back")
def exitWalk_back(self):
self.character.stop()
Thanks in advance.