2011-06-18 204 views
0

我正在使用以下代碼在我正在處理的手機遊戲中顯示廣告。它工作正常,但我不知道要在clickStart函數中放置什麼來在遊戲開始前從舞臺上移除廣告。我一直在玩removeChild,但似乎無法讓它工作。AS3 - 刪除兒童問題

stop(); 
startButton.addEventListener(MouseEvent.CLICK,clickStart); 

function clickStart(event:MouseEvent) { 
gotoAndStop("play"); 
} 


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

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

    var l:Loader = new Loader(); 
    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); 
    } 

    } 
} 

回答

0

因爲變量超出了範圍,所以需要將Loader的聲明移到onComplete函數之外。

var l:Loader = new Loader(); 

你可以嘗試把它放在相同的範圍內,因爲這些:

var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp"); 
var variables:URLVariables = new URLVariables(); 

,那麼你應該能夠更新clickStart功能是這樣的:

function clickStart(event:MouseEvent) { 
removeChild(l); 
gotoAndStop("play"); 
} 
+0

您也可能想要將偵聽器移除到MouseEvent.CLICK。另外,將onAdClick函數移出onComplete函數可能是一個好主意。 – Bakapii

+0

謝謝。完美的作品。 – Rich