2013-12-10 88 views
0

我的代碼工作正常。假設用戶按1時輸入一個圖像,當他/她按下2時換出另一個圖像。但是,如果在先按下相同的號碼後按1或2,則會出現#2025錯誤。例如:按1,然後再按1。ActionScript 3:錯誤#2025

ArgumentError:錯誤#2025:提供的DisplayObject必須是調用者的子項。 在flash.display一::級DisplayObjectContainer/removeChild之() 在warren_fla :: MainTimeline/reportKeyDown2()

代碼

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

回答

6

你不檢查,如果它已經是一個孩子消除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); 
+0

謝謝。 +1效率 – Warren

0

你可能需要添加的BMP的多次。與刪除它們一樣,但如果您嘗試刪除不在顯示列表中的子項,則會看到您看到的錯誤。嘗試使用此代碼在所有的情況下,你要添加或刪除:

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

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

----------- -------------編輯

好的,雖然顯然你可以在不檢查的情況下addChild(因爲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); 
} 

輸出:

 
------------------- 
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 
2

在reportKeyDown(),嘗試移動:

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

,檢查的鍵碼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是刪除它之前的孩子:

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

謝謝。完美工作。 – Warren