AS3 - 错误 - 提供的 DisplayObject 必须是子对象
我正在使用以下代码在 Flash 游戏中显示广告。除了一个小问题之外,它工作得很好。如果在显示广告之前调用 startButton 函数,我会收到一条错误消息:
ArgumentError:错误#2025:提供的 DisplayObject 必须是调用者的子级。 在 flash.display::DisplayObjectContainer/removeChild() 在 VirusDefender/clickStart()[VirusDefender::frame1:17]
广告在舞台上时没有错误。我尝试将removeChild 包装在try catch 中,但这不起作用。
有人可以告诉我如何防止在广告尚未显示时调用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);
}
}
谢谢!富有的
I'm using the following code to display an advertisement in a flash game. It works great except for one small issue. If the startButton function gets called before the ad is displayed I get an error saying:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at VirusDefender/clickStart()[VirusDefender::frame1:17]
There are no errors when the advert is on the stage. I tried wrapping the removeChild in a try catch but that did not work.
Could someone please tell me how to prevent the removeChild line from being called if the ad has not been displayed yet. Sometimes it takes up to 3 seconds for the ad to show so people may click before it gets on the stage.
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);
}
}
Thanks! Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将该行换行为:
这将检查
l
是否不为 null 且为子级,然后才将其删除。Wrap the line into:
This will check if
l
is not null and a child, and only then removes it.或者,由于 Loader 是一个 DisplayObject,您只需在执行其他操作之前将其添加到舞台(在将其添加到舞台之前不需要完成加载)并在按下开始时将其删除,这样您就不会让它徘徊或者如果您确实希望将其付诸印刷,请稍后再加入。
Alternatively, as Loader is a DisplayObject, you can simply add it to the stage before anything else (it need not have finished loading before you can add it to the stage) and remove it when start is pressed, that way you wont have it lingering or popping in later if you do in fact want it gone on press.