2011-06-22 53 views
0

我正在使用以下代碼在Flash遊戲中顯示廣告。除了一個小問題外,它效果很好。如果顯示廣告前startButton函數被調用我得到一個錯誤說:AS3 - 錯誤 - 提供的DisplayObject必須是小孩

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at VirusDefender/clickStart()[VirusDefender::frame1:17]

有沒有錯誤時,廣告是在舞臺上。我試圖在try catch中包裝removeChild,但那不起作用。

有人可以告訴我如何防止removeChild行被調用,如果廣告尚未顯示。有時需要3秒才能顯示廣告,以便人們在上臺前點擊。

stop(); 

//Start Button 
startScreen.startButton.addEventListener(MouseEvent.CLICK,clickStart); 
function clickStart(event:MouseEvent) 
{ 
sndFire=new fire_sound(); 
sndFireChannel=sndFire.play(); 
removeChild(l); 
gotoAndStop("play"); 
} 

// Help Button 
startScreen.helpButton.addEventListener(MouseEvent.CLICK,clickHelp); 
function clickHelp(event:MouseEvent) 
{ 
sndFire=new fire_sound(); 
sndFireChannel=sndFire.play(); 
removeChild(l); 
gotoAndStop("help"); 
} 


// SMAATO Advertising Code for Start Page 
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp"); 
var variables:URLVariables = new URLVariables(); 
variables.adspace = "0"; 
variables.pub = "0"; 
variables.devip = "127.0.0.1"; 
variables.format = "IMG"; 
variables.adcount = "1"; 
variables.response = "XML"; 
request.data = variables; 
var loader:URLLoader = new URLLoader(); 

var l:Loader = new Loader(); 

loader.addEventListener(Event.COMPLETE, onComplete); 
loader.load(request); 


function onComplete(e:Event):void 
{ 
var data:XML = new XML(loader.data as String); 
var status:String = data.*::status.toString(); 
if(status == "success") 
{ 
var ad:XMLList = data.*::ads.*::ad; 
var link:String = ad.*::link.toString(); 

l.load(new URLRequest(link)); 
addChild(l); 
l.x = 135; 
l.y = 265; 
var clickurl:String = ad.*::[email protected](); 
l.addEventListener(MouseEvent.CLICK, onAdClick); 
} 
function onAdClick(e:MouseEvent):void 
{ 
    var request:URLRequest = new URLRequest(clickurl); 
    navigateToURL(request); 
} 
} 

謝謝!豐富的

回答

3

裹行成:

if (l != null && contains(l)) { 
    removeChild(l); 
} 

這將檢查是否l不爲空和孩子,然後才刪除。

+1

在調用contains或removeChild之前,有時候可能會確保l不等於null。 – Bakapii

+0

好點!將此添加到答案中。 – DanielB

+0

太棒了。感謝球員它完美的作品,是有道理的。 – Rich

0

此外,作爲Loader是一個DisplayObject,你可以簡單地把它任何事情之前添加到舞臺(它不需要加載完畢,然後才能將其添加到舞臺),並按下啓動時刪除它,這樣你如果你確實想讓它在新聞中消失,它將不會在後面揮之不去。

相關問題