Hi, I am trying to control that when the player presses some key, the game will process only one entry of that instead of 10~15 entries entries… I tried this:
def delayTask(self, task):
self.acceptKey = True
And then call:
taskMgr.doMethodLater(1, self.delayTask, 'stop spam')
So, what I want to do is, the player presses and releases “A”, I receive a single Entry of “A” instead of 10~15.
But it is not working every time, if I increase the time the program gets very slow in time, and the CPU consumption increases. Can anyone help me in this one?
Thanks in advance.
I’m not quite sure what you’re talking about, but I suspect you may have copied someone’s code that is designed to record whether a key is up or down at any given time, and do a series of events every frame while they key is held down.
If you don’t want that sort of behavior, but you only want to do one action when a particular key is pressed, just hang a hook on that one key, like this:
Yes I got the code of keyboard entry from roaming ralph example. I will try that one when I got home. Still would be easier if I could accept A and wait for 1 second to accept A again.
self.lastA = 0
now = globalClock.getFrameTime()
elapsed = now - self.lastA
if elapsed < 1:
self.lastA = now
... do something ...
I had to do that once but not in Panda.
A possibility is to test that within your doSomethingForA function by comparing two variables TimeA= now and TimeB= last time A was pressed.
Edit: You were the quicker David
Thanks a lot guys, I had to rewrite some parts of the code and add a few methods, but its working perfectly now.
so here is what I did:
self.accept("arrow_left", self.setIfLeftKeyPressed, [True])
self.accept("arrow_right", self.setIfRightKeyPressed, [True])
self.accept("arrow_up", self.setIfUpKeyPressed, [True])
self.accept("arrow_down", self.setIfDownKeyPressed, [True])
self.accept("a", self.setIfConfirmKeyPressed, [True])
self.accept("s", self.setIfCancelKeyPressed, [True])
Also have a method to get the key pressed status, so I get the value, if it is true I do whatever is needed and set it to false again.