Flash Game ArgumentError:错误#2025 removeChild

时间:2014-11-13 04:12:14

标签: actionscript-3 flash

我正试图让蝙蝠角色在我的游戏中删除jack / 2/3孩子,但我不断收到ArgumentError:错误#2025。我明白我可能会两次去除一个孩子吗?我环顾四周,我对这些东西并不是很有经验,所以我很难理解需要做些什么来解决这个问题。有人可以告诉我具体需要用我的代码做什么吗?

var jack:pumpkin = new pumpkin();
var jack2:pumpkin = new pumpkin();
var jack3:pumpkin = new pumpkin();
var score:Number = 0;

scoreBox.text = String(score);

addChild(jack);
jack.x = 125;
jack.y = 285;

addChild(jack2);
jack2.x = 270;
jack2.y = 310;

addChild(jack3);
jack3.x = 445;
jack3.y = 285;

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveLeft);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveRight);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveDown);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveUp);
stage.addEventListener(KeyboardEvent.KEY_UP, bump);

function moveLeft(e:KeyboardEvent):void {

if (e.keyCode == 37) {
        bat.x -= 5;
    }
}
function moveRight(e:KeyboardEvent):void {

if (e.keyCode == 39) {
        bat.x += 5;
    }
}
function moveDown(e:KeyboardEvent):void {

if (e.keyCode == 40) {
        bat.y += 5;
    }
}
function moveUp(e:KeyboardEvent):void {

if (e.keyCode == 38) {
        bat.y -= 5;
    }
}

function bump(e:KeyboardEvent):void {

    stage.addEventListener(Event.ENTER_FRAME, bumpIt);

    function bumpIt(e:Event):void {

        if (bat.hitTestObject(jack)) {
            stage.removeEventListener(Event.ENTER_FRAME, bumpIt);
            removeChild(jack);
            score++;
            scoreBox.text = String(score);
        }
        if (bat.hitTestObject(jack2)) {
            stage.removeEventListener(Event.ENTER_FRAME, bumpIt);
            removeChild(jack2);
            score++;
            scoreBox.text = String(score);
        }
        if (bat.hitTestObject(jack3)) {
            stage.removeEventListener(Event.ENTER_FRAME, bumpIt);
            removeChild(jack3);
            score++;
            scoreBox.text = String(score);
        }
    }

    }

1 个答案:

答案 0 :(得分:2)

由于嵌套的bumpit()事件处理程序,如果你的命中测试没有碰撞,你可以在每个键上添加另一个输入帧事件。

  • KeyboardEvent.KEY_UP,事件处理程序调用{​​{1}},它会添加一个bump()侦听器,调用Event.ENTER_FRAME
    • 如果bumpIt()为真,则删除bat.hitTestObject()
    • 如果没有,您仍然会Event.ENTER_FRAME每个帧都会Event.ENTER_FRAME侦听{。}}。

因此,您的每一个键都可能会添加另一个帧处理程序。

如果发生了10个按键事件且没有一个按下测试对象,则现在每帧调用bumpIt()十次。

如果你需要按键测试,只需将逻辑放在那里:

bumpIt

或者,如果您在按键后跟踪动画序列,可能会添加一些状态变量,例如:

stage.addEventListener(KeyboardEvent.KEY_UP, bump);

function bump(e:KeyboardEvent):void {
    if (bat.hitTestObject(jack)) { /* ... */ }
}

然后,如果您的命中测试有效并且您删除了帧处理程序,请重置状态变量:

var isFlying:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_UP, bump);

function bump(e:KeyboardEvent):void {
    // If bat is already flying, don't add another frame handler
    if (isFlying)
        return;

    // Otherwise, indicate bat is now flying and add frame handler
    isFlying = true;

    stage.addEventListener(Event.ENTER_FRAME, bumpIt);
}

另一种解决方案是删除嵌套的处理程序。因为您在stage.removeEventListener(Event.ENTER_FRAME, bumpIt); isFlying = false; 中嵌套了bumpIt(),所以您会看到由于范围而导致的回调累积触发:

bump()

只需宣传function bump():void { function bumpIt():void { /* ... */ } }

bumpIt()