ceb Posted March 18, 2014 Share Posted March 18, 2014 Hi Im not sure if this is possible but for example if you hold the up and right arrow keys the player moves north east. Ive been trying to code this but just can't. Please Help Thanks. Link to comment Share on other sites More sharing options...
ghostrifle Posted March 18, 2014 Share Posted March 18, 2014 you must set direction variables when pressing the arrow keys. then in the update loop, move the player (or whatever) according to these vars. Link to comment Share on other sites More sharing options...
Ross Posted March 18, 2014 Share Posted March 18, 2014 Set up a couple "ifs" in the update loop. if( "PRESSING RIGHT ARROW" ){ "SPRITE".x += "MOVE AMOUNT"}else if( PRESSING LEFT ARROW){ "SPRITE".x -= "MOVE AMOUNT"} if( "PRESSING DOWN ARROW" ){ "SPRITE".y += "MOVE AMOUNT"}else if( "PRESSING UP ARROW"){ "SPRITE".y -= "MOVE AMOUNT"} ceb 1 Link to comment Share on other sites More sharing options...
ceb Posted March 18, 2014 Author Share Posted March 18, 2014 Here is what my player movement code is:var nothing = 'left'; if (cursors.left.isDown) { player.body.velocity.x = -230; if (nothing != 'left') { player.animations.play('left'); nothing = 'left'; } } else if (cursors.right.isDown) { player.body.velocity.x = 230; if (nothing != 'right') { player.animations.play('right'); nothing = 'right'; } } else if (cursors.down.isDown) { player.body.velocity.y = 230; if (nothing != 'down') { player.animations.play('down'); nothing = 'down'; } } else if (cursors.up.isDown) { player.body.velocity.y = -230; if (nothing != 'up') { player.animations.play('up'); nothing = 'up'; } } else { if (nothing != 'idle') { player.animations.stop(); if (nothing == 'left') { player.frame = 0; } nothing = 'idle'; } }And here is the demo ive got running http://justukfreebies.co.uk/games/, as you can see the player does not move diagonally when 2 keys left and up, or right and up are pressed. I was wondering if this was possible. Link to comment Share on other sites More sharing options...
Ross Posted March 18, 2014 Share Posted March 18, 2014 You have all the movement in one "if" you need to separate out the horizontal movement from the vertical movement. if (cursors.left.isDown){...}else if (cursors.right.isDown){...} if (cursors.up.isDown){...}else if (cursors.down.isDown){...} ceb 1 Link to comment Share on other sites More sharing options...
ceb Posted March 18, 2014 Author Share Posted March 18, 2014 Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts