Legomite Posted November 8, 2014 Share Posted November 8, 2014 I want to control the movement of a sprite using arrow keys or a sprite touch pad with arrow keys.How to set the speed of movement and gravity. Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 8, 2014 Share Posted November 8, 2014 Hi @Legomite You need to make sure that you update the position of the sprite at every frame. The code below should move a sprite by 5px when you press a Right Button or a Left Button that are showing on the screen ...var left = false;var right = false;buttonLeft.mousedown = buttonLeft.touchstart = function() { left = true; right = false;}buttonLeft.mouseup = buttonLeft.touchend = function() { left = false; right = false;}buttonRight.mousedown = buttonRight.touchstart = function() { left = false; right = true;}buttonRight.mouseup = buttonRight.touchend = function() { left = false; right = false;}var player = new game.Sprite('player.png');player.position.set(240*game.scale, 160*game.scale);player.anchor.set(0.5, 1);this.stage.addChild(player);game.scene.addObject(player); player.update = function() { if (left) { player.x = player.x - 5*game.scale; } else if (right) { player.x = player.x + 5*game.scale; }} Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.