Hi, I’m new to panda, and programming in general.
I am making a game and I want to control the player like those old 2D rpgs, just 8 directions of movement.
I think I got the hang of moving an object around with the arrow keys using the Roaming Ralph tutorial. I can currently move an object in four directions, up, down, left, and right.
What I can’t seem to figure out is how to move an object diagonally by checking for when 2 arrow keys are being pressed at the same time.
Thanks for the quick reply. I’ll go and try that now.
By the way, you wouldn’t happen to know what these mean in the tutorial:
camright = base.camera.getNetTransform().getMat().getRow3(0)
camright.normalize()
Well I have been using the self.playerObj(self.playerObj,moveSpeed) method to move my character around, and that works fine, but when I press an arrow button while holding down another arrow button, the messenger thing registers the first arrow button as up, So my keyBank is updated accordingly, which means that I can only read one keystroke at a time…
reading that back, it was a little hard to follow , so I’ll try to give a little example.
I’m pressing the up button
the messenger sends arrow_up
the control event accepts this and updated my keyBank, so that ‘up’ = True
My PlayerMovement task reads this and moves the character up
(Everything is going fine so far)
I’m now pressing left button (I never stop pressing the up button)
the messenger sends arrow_up-up ([color=red]here is the problem)
the control event accepts this and updated my keyBank, so that ‘up’ = False
the messenger send arrow_left
the control event accepts this and updated my keyBank, so that ‘left’ = True
My PlayerMovement task reads this and moves the character left (crap )
So, if there was some way to read when two keys are being pressed at the same time, it would help me bunches
Manakel: If I did that, the player wouldn’t stop moving. That would be great if I was making pacman
T Rex: Thanks, that solved my problem! It was my if statements. The before and after code is below. (I’m so brain dead, should have posted the code from the beginning…)
Before:
if timeElapsed >= self.movementUpdateTime:
if self.keyBank['up']:
if not self.keyBank['down'] and not self.keyBank['right'] and not self.keyBank['left']:
self.playerPosY = self.playerPosY + self.playerMoveSpeed
self.playerHeading = 0
self.previousMovementUpdateTime = Task.time
if self.keyBank['left']:
if not self.keyBank['up'] and not self.keyBank['down'] and not self.keyBank['right']:
self.playerPosX = self.playerPosX - self.playerMoveSpeed
self.playerHeading = 90
self.previousMovementUpdateTime = Task.time
if self.keyBank['down']:
if not self.keyBank['up'] and not self.keyBank['right'] and not self.keyBank['left']:
self.playerPosY = self.playerPosY - self.playerMoveSpeed
self.playerHeading = 180
self.previousMovementUpdateTime = Task.time
if self.keyBank['right']:
if not self.keyBank['up'] and not self.keyBank['down'] and not self.keyBank['left']:
self.playerPosX = self.playerPosX + self.playerMoveSpeed
self.playerHeading = 270
self.previousMovementUpdateTime = Task.time
self.player.setPos(self.playerPosX,self.playerPosY,self.playerPosZ)
self.player.setH(self.playerHeading)
return Task.cont
The only problem now is that, as you can see, I’m setting the player’s heading according to which button is being pressed, but I don’t know how to set the player’s heading to the diagonal ones (45,135,225,315)
Any suggestions
edit: Sorry, disregard that last part, easily solved.
if self.keyBank['up'] and self.keyBank['left']: self.playerHeading = 45
if self.keyBank['left'] and self.keyBank['down']: self.playerHeading = 135
if self.keyBank['down'] and self.keyBank['right']: self.playerHeading = 225
if self.keyBank['right'] and self.keyBank['up']: self.playerHeading = 315
No, my way is silly. Why would I want to do 4 memory writes (up & down), if I could use the already existing keybanks ‘up’ & ‘down’ ?
God knows why : it were some shorts in my head.