Jump to content

Character movement & multiple keys issue


Zhob
 Share

Recommended Posts

Hi, 

I'm currently working on a tile base RPG game using Phaser.

function create():

controls = { 
  up: game.input.keyboard.addKey(Phaser.Keyboard.Z),
 (...),
  shift: game.input.keyboard.addKey(Phaser.Keyboard.SHIFT) };
}

If I press Z, my character needs to move and if I press SHIFT + Z, my character has to rotate towards the Z direction. 

function update() :

if (controls.shift.isDown) {
	if (controls.up.isDown){
        rotateCharacter();
	}
}
else {
    if (controls.up.isDown){
        moveCharacter();
	}
}

 

Right now, when try to rotate my character using SHIFT + Z, it triggers the rotation AND the movement. If I press Z alone, the character moves as expected.

I've tried checking if my SHIFT key was up but it didn't change anything.

How can I achieve that?

 

Thanks, in advance.

Link to comment
Share on other sites

Thank you! 

I didn't mention that I was working on a multiplayer game and seeing your fiddle made think that my mistake wasn't on the controls but rather on how I render other players on the map... Original issue solved then!

Quick question :

Characters have to move 46px by 46px. The only way I found was this kind of "hack" :

if (controls.up.isDown){
	if (controls.up._justUp) {
		moveCharacter(); // Moves the character by 46px in the direction
	}
}

I updated your fiddle here.

Is there a better way to do this? Also, if you spam the key, the tween restarts so the character might have moved more than 46px (edit: and I don't want that).

Edited by Zhob
Quick edit
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...