Would like to move MovieClip using onstage buttons, not arrow keys.

Posted by Anne on Stack Overflow See other posts from Stack Overflow or by Anne
Published on 2010-03-23T08:09:03Z Indexed on 2010/03/23 8:13 UTC
Read the original article Hit count: 257

Filed under:
|
|

To move the MC, using arrow keys I used the following and it worked: var timer:Timer; var direct:String; initStage();

function initStage() {
   stage.addEventListener(KeyboardEvent.KEY_DOWN,startMove);
}

function startMove(e:KeyboardEvent):void {
   switch (e.keyCode) {
      case Keyboard.RIGHT:
      direct = "right";
      break;
      case Keyboard.LEFT:
      direct = "left";
      break;      
      case Keyboard.DOWN:
      direct = "down";
      break;
      case Keyboard.UP:
      direct = "up";
   }


   timer = new Timer(10);
   timer.addEventListener(TimerEvent.TIMER, moveBox);
   timer.start();
   stage.removeEventListener(KeyboardEvent.KEY_DOWN, startMove);
   stage.addEventListener(KeyboardEvent.KEY_UP, stopMove);
}

function stopMove(e:KeyboardEvent):void {
   timer.stop();
   initStage();
}

function moveBox(e:TimerEvent):void {
   switch (direct) {
      case "right":
      box.x += 1;
      break;
      case "left":
      box.x -= 1;
      break;
      case "up":
      box.y -= 1;
      break;
      case "down":
      box.y += 1;
      break;      
   }
}

I tried to convert this to use my onstage buttons: up_btn, down_btn, left_btn, right_btn to move MC box but couldn't figure it out. Can anyone help me convert this? Thanks in advance for any help you might offer. Annie

© Stack Overflow or respective owner

Related posts about as3

Related posts about flash