要使用箭头键移动MC,我使用了以下内容并且它有效: 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;
}
}
我试图将其转换为使用我的舞台上的按钮:up_btn,down_btn,left_btn,right_btn来移动MC框但无法弄明白。任何人都可以帮我转换吗? 提前感谢您提供的任何帮助。 安妮
答案 0 :(得分:0)
我会做这样的事情:
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;
}
}