【问题标题】:AS3 - Remove Child ProblemAS3 - 消除子问题
【发布时间】:2011-06-19 04:05:28
【问题描述】:

我正在使用以下代码在我正在开发的手机游戏中显示广告。它工作正常,但我不知道在 clickStart 函数中放置什么以在游戏开始之前从舞台上删除广告。我一直在玩 removeChild 但似乎无法让它工作。

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.*::action.@target.toString();
    l.addEventListener(MouseEvent.CLICK, onAdClick);

    function onAdClick(e:MouseEvent):void
    {
    var request:URLRequest = new URLRequest(clickurl);
    navigateToURL(request);
    }

  }
}

【问题讨论】:

    标签: actionscript-3 removechild


    【解决方案1】:

    您需要将 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");
    }
    

    【讨论】:

    • 您可能还想删除 MouseEvent.CLICK 的侦听器。此外,将 onAdClick 函数从 onComplete 函数中移出可能是个好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多