Actionscript 3 行动脚本3:错误#2025

Actionscript 3 行动脚本3:错误#2025,actionscript-3,flash,flash-cs5,flash-cs6,Actionscript 3,Flash,Flash Cs5,Flash Cs6,我的代码运行良好。假设用户按1时输入一幅图像,按2时将其换成另一幅图像。但是,当我之前按过相同的数字后再按1或2时,我会得到一个#2025错误。例如:先按1,然后再按1 ArgumentError:Error#2025:提供的DisplayObject必须是子对象 打电话的人的名字。 在flash.display::DisplayObjectContainer/removeChild()中 在佛罗里达州沃伦尤:MainTimeline/reportKeyDown2() 代码 您可能会多次添加bm

我的代码运行良好。假设用户按1时输入一幅图像,按2时将其换成另一幅图像。但是,当我之前按过相同的数字后再按1或2时,我会得到一个#2025错误。例如:先按1,然后再按1

ArgumentError:Error#2025:提供的DisplayObject必须是子对象 打电话的人的名字。 在flash.display::DisplayObjectContainer/removeChild()中 在佛罗里达州沃伦尤:MainTimeline/reportKeyDown2()

代码


您可能会多次添加bmp。删除它们也一样,但是如果您尝试删除不在显示列表中的子项,您将看到错误。在添加或删除以下内容的所有情况下,请尝试使用此代码:

if (!contains(bmp)) { addChild(bmp); }  // or bmp2

if (contains(bmp)) { removeChild(bmp); } // or bmp2
-----------编辑-------------

好的,虽然您显然可以不进行检查就添加Child(因为addChild将从显示树中删除对象),但执行检查更有效。事实上,它似乎快了3倍左右。此测试:

var clip = new MovieClip();
var i,j,tin,dur;
var tries = 100000;

for (j=0; j<5; j++) {
    trace("-------------------");

    tin = new Date().time;
    for (i=0; i<tries; i++) { if (!contains(clip)) { addChild(clip); } }
    dur = new Date().time - tin;
    trace("Check Adding: "+dur);

    removeChild(clip);

    tin = new Date().time;
    for (i=0; i<tries; i++) { addChild(clip); }
    dur = new Date().time - tin;
    trace("Just Adding: "+dur);
}
var clip=new MovieClip();
变量i,j,tin,dur;
var=100000;

对于(j=0;j您正在删除
bmp
,而不检查它是否已经是孩子

function reportKeyDown2(event:KeyboardEvent):void 
{ 
    if (event.keyCode == 50) {
        //trace("2 is pressed");
        bmp2.x = 230;
        bmp2.y = 150;
        addChild(bmp2);
        if(contains(bmp)) 
            removeChild(bmp);
    }
} 
您的代码也可以重构为以下更简单的版本:

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

var bdata = new image1(stage.stageWidth, stage.stageHeight);
var bdata2 = new image2(stage.stageWidth, stage.stageHeight);
var bmp = new Bitmap(bdata);
var bmp2 = new Bitmap(bdata2);

function reportKeyDown(event:KeyboardEvent):void 
{ 
    if (event.keyCode == Keyboard.NUMBER_1) {
        swapBitmaps(bmp, bmp2);            
    } else if (event.keyCode == Keyboard.NUMBER_2) {
        swapBitmaps(bmp2, bmp);            
    }
}

function swapBitmaps(first:Bitmap, second:Bitmap):void
{
      first.x = 230;
      first.y = 150;
      addChild(first);
      if(contains(second)) {        
           removeChild(second);
      }
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
在reportKeyDown()中,尝试移动:

if (contains(bmp2)) {
    removeChild(bmp2);
}
在检查keycode的if语句中:

function reportKeyDown(event:KeyboardEvent):void 
{ 
    if (event.keyCode == 49) {
        //trace("1 is pressed");
        bmp.x = 230;
        bmp.y = 150;
        addChild(bmp);

        if (contains(bmp2)) {
            removeChild(bmp2);
        }
    }
}
在reportKeyDown2中,在删除bmp之前,请检查并确保bmp为子级:

function reportKeyDown2(event:KeyboardEvent):void 
{ 
    if (event.keyCode == 50) {
        //trace("2 is pressed");
        bmp2.x = 230;
        bmp2.y = 150;
        addChild(bmp2);

        if(contains(bmp))
        {
            removeChild(bmp);
        }
    }
} 

谢谢。工作做得很好。谢谢。效率+1
function reportKeyDown(event:KeyboardEvent):void 
{ 
    if (event.keyCode == 49) {
        //trace("1 is pressed");
        bmp.x = 230;
        bmp.y = 150;
        addChild(bmp);

        if (contains(bmp2)) {
            removeChild(bmp2);
        }
    }
}
function reportKeyDown2(event:KeyboardEvent):void 
{ 
    if (event.keyCode == 50) {
        //trace("2 is pressed");
        bmp2.x = 230;
        bmp2.y = 150;
        addChild(bmp2);

        if(contains(bmp))
        {
            removeChild(bmp);
        }
    }
}