Simple task question


I have a little question about tasks. This is a piece of my code:

def catchInput(task):
            self.accept("escape", sys.exit)
            self.accept('arrow_up', movePandaForward)
            self.accept('arrow_down', movePandaBackward)
            #print task.time
            return task.cont
       taskMgr.add(catchInput, "keys")

Now, If I press the ‘arrow_up’ button, my panda does move, but only once, I expected that it would keep moving forward. I don’t get it, my ‘catchInput’ doesn’t seem to accept my buttonpress every frame, but only once. Anybody has an idea what I’m doing wrong, what’s happening?

Key events only fire once when you press it down. The usual way to do it is to listen for the down event and the up event (arrow_up-up), storing the key state in a variable, and create a movement task that moves it by the speed multiplied by the elapsed time (globalClock.getDt()) to get a correct result.

Don’t put your accept() calls into a task. You should only bind them once.

I would advise you to study the Roaming Ralph sample program, it shows how to deal with this.

Indeed I got it working now. Roaming Ralph is a very good sample program!

Only one extra question. Why use the globalClock.getDt() function? Why don’t just do actor.setX(actor.getX + 1) for example?

I don’t realy get what that clock has to do with movement.

Thanks for the answer!

The trick with the lobalClock.getDt()is to make the movement independent from the frame rate.

If you’d move the avatar (let’s say he’s in a car) 1 meter every frame then at 30fps he would move at 30 m/s (~108km/h) with a frame rate of 120 fps he’d blazed at 430km/h

Oh yes Wezu, you’re so true, that I didn’t even think of that!

Programming with frames is a little different than ‘normal’ programming! Yet quiet interesting, thanks :slight_smile: