문제

My code works fine. It's suppose to bring in one image when the user presses 1 and swap it out for another when he / she presses 2. But, when I presses 1 or 2 after having previously pressing either the same number I get a #2025 error. Ex: Pressing 1 then pressing 1 again.

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at warren_fla::MainTimeline/reportKeyDown2()

Code

import flash.events.KeyboardEvent;

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 == 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);
    removeChild(bmp);
}

} 

stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown2);
도움이 되었습니까?

해결책

You are removing bmp without checking if it is already a child.

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);
    }
} 

Also your code can be refactored into this simpler version :

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);

다른 팁

In reportKeyDown(), try moving:

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

Inside the if statement that checks the keycode:

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);
        }
    }
}

And in reportKeyDown2, check to make sure that bmp is a child before removing it:

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);
        }
    }
} 

You're potentially adding the bmp's multiple times. Same with removing them, but if you try to remove a child that isn't on the display list, you'll get the error you're seeing. Try using this code in all cases where you're adding or removing:

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

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

----------- edit -------------

OK, while obviously you can addChild without checking (since addChild will remove the object from the display tree), it's more efficient to do the check. In fact, it seems to be about 3x faster. This test:

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);
}

outputs:

-------------------
Check Adding: 9
Just Adding: 25
-------------------
Check Adding: 8
Just Adding: 25
-------------------
Check Adding: 9
Just Adding: 24
-------------------
Check Adding: 9
Just Adding: 24
-------------------
Check Adding: 9
Just Adding: 25
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top