addChild自定义类AS3
这是我尝试实例化到主类中的类:
public class Character extends Sprite {
[Embed(source='../lib/front1.svg')]
private var front1Class:Class;
private var crosshair:Sprite = new front1Class ();
public function Character() {
trace("started");
Mouse.hide();
crosshair.scaleX = 5;
crosshair.scaleY = 5;
this.addChild(crosshair);
stage.addEventListener(Event.ENTER_FRAME, MrEveryFrame);
stage.addEventListener(MouseEvent.CLICK, click);
}
private function click(evt:MouseEvent):void {
trace("clicked @ " + evt.stageX + "," + evt.stageY);
}
public function MrEveryFrame(e:Event):void
{
crosshair.x = mouseX - 15;
crosshair.y = mouseY - 15;
}
}
当我将其设置为文档类时,它工作正常。
但是...当我将其设为我的文档类并尝试从那里调用它时:
public class Shell extends Sprite
{
private var character:Sprite = new Character ();
public function Shell()
{
addChild(character);
}
}
它会中断,并且不再显示精灵对象(尽管它确实擦除了鼠标指针)。
这是怎么回事?您无法将自定义精灵或影片剪辑类实例化为 DisplayObject 类???
This is the class I am trying instantiate into my main class:
public class Character extends Sprite {
[Embed(source='../lib/front1.svg')]
private var front1Class:Class;
private var crosshair:Sprite = new front1Class ();
public function Character() {
trace("started");
Mouse.hide();
crosshair.scaleX = 5;
crosshair.scaleY = 5;
this.addChild(crosshair);
stage.addEventListener(Event.ENTER_FRAME, MrEveryFrame);
stage.addEventListener(MouseEvent.CLICK, click);
}
private function click(evt:MouseEvent):void {
trace("clicked @ " + evt.stageX + "," + evt.stageY);
}
public function MrEveryFrame(e:Event):void
{
crosshair.x = mouseX - 15;
crosshair.y = mouseY - 15;
}
}
When I set it to the document class, it works fine.
However... when I make THIS my document class and try to call it from there:
public class Shell extends Sprite
{
private var character:Sprite = new Character ();
public function Shell()
{
addChild(character);
}
}
It breaks, and no longer shows the sprite object (though it does erase the mouse pointer).
What's the deal here? You can't instantiate custom sprite or movieclip classes into a DisplayObject class???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
构造函数中的阶段为
null
。正如您自己发现的那样,只有当您的类是文档类时,这才有效。因此,请像这样更改您的构造函数:添加侦听器将仅在已知阶段后才访问该阶段,并且它不再是
null
The stage is
null
in the constructor. That only works when your class is the Document Class, as you found out yourself. So change your constructor like this:Adding the listener will access the stage only after the stage is known, and it's no longer
null