开发者

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

开发者 https://www.devze.com 2022-12-24 08:09 出处:网络
To move the MC, using arrow keys I used the following and it worked: var timer:Timer; var direct:String;

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 bu开发者_开发知识库t couldn't figure it out. Can anyone help me convert this? Thanks in advance for any help you might offer. Annie


I would do something like this:

btnDown.addEventListener(MouseEvent.MOUSE_DOWN, onDown)
stage.addEventListener(MouseEvent.MOUSE_UP, onUp)
btnUp.addEventListener(MouseEvent.MOUSE_DOWN, onDown)
stage.addEventListener(MouseEvent.MOUSE_UP, onUp)
btnLeft.addEventListener(MouseEvent.MOUSE_DOWN, onDown)
stage.addEventListener(MouseEvent.MOUSE_UP, onUp)
btnRight.addEventListener(MouseEvent.MOUSE_DOWN, onDown)
stage.addEventListener(MouseEvent.MOUSE_UP, onUp)

var pressedState:int = 0;

var myTimer:Timer = new Timer(10);
myTimer.addEventListener(TimerEvent.TIMER, onTimerTick);

function onDown(e:MouseEvent)
{
    myTimer.start();
    switch(e.currentTarget)
    {
        case btnDown:
        pressedState = 1;
        break;
        case btnUp:
        pressedState = 2;
        break;
        case btnLeft:
        pressedState = 3;
        break;
        case btnRight:
        pressedState = 4;
        break;
    }
}

function onUp(e:MouseEvent)
{
    myTimer.stop();
    pressedState = 0
}

function onTimerTick(e:TimerEvent)
{
    switch(pressedState)
    {
        case 1:
        mc.y++
        break;
        case 2:
        mc.y--;
        break;
        case 3:
        mc.x--;
        break;
        case 4:
        mc.x++;
        break;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消