Jump to content

How can I control a sprite?


Recommended Posts

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;  }}
Link to comment
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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