[英]ADDED_TO_STAGE function never called
I have this class which should take a button from stage ( xBtn ). 我有这个课,应该从舞台上按一下按钮( xBtn )。
package com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx extends MovieClip {
private var xBtn : MovieClip;
public function STXbutonx() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null) : void {
trace("int!"); //this is never called
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
}
and in Document class I called like this : 在Document类中,我这样调用:
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx();
}
}
Why my init function is never called? 为什么从未调用过我的init函数?
Thank you! 谢谢!
Because you never add it to the stage. 因为您永远不会将其添加到舞台上。
Change your Document class to 将您的Document类更改为
public function Main() {
xx = new STXbutonx();
addChild(xx);
}
Main holds the reference to the stage, adding a child to Main will add the child to the stage. Main拥有对舞台的引用,将一个子代添加到Main会将子代添加到舞台。 Thus the event listener in STXbutonx will fire.
因此,将触发STXbutonx中的事件侦听器。
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn = stage.getChildByName('ics')as MovieClip; // now I have acces of undefined property stage...
//现在我有未定义属性阶段的权限...
You don't have access to the stage because STXButon is not on the stage, it is not an DisplayObject. 您无权访问舞台,因为STXButon不在舞台上,它不是DisplayObject。 To get around this, do this:
要解决此问题,请执行以下操作:
package com.stx.utils { 包com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx{
private var xBtn : MovieClip;
private var stage : Stage;
public function STXbutonx(stage:Stage) {
this.stage = stage;
init();
}
private function init() : void {
trace("int!");
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
} }
And of course 而且当然
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx(stage);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.