我正在尝试从 parent 的 child 分配 parent 的变量
//Parent
public class Main extends Sprite
{
public var selectedSquare:Sprite;
public function Main()
{
//inits and adds new Square child class to display list
}
...
-------
//Child
public function dragSquare(evt:MouseEvent):void
{
Sprite(parent).selectedSquare = this; //evil doesn't work!
parent.addChild(this);
this.startDrag();
}
我收到此错误,但我正在将父级从 displayObjectContainer 转换为 Sprite,所以我不知道为什么它不起作用。
1119: Access of possibly undefined property selectedSquare through a reference with static type flash.display:Sprite.
最佳答案
您应该将parent转换为Main而不是Sprite,因为Sprite不会有任何对“selectedSquare”的引用。如果 Main 要扩展 MovieClip,这不会成为问题,因为 MovieClip 可以动态创建引用。
对子函数的建议修改:
public function dragSquare(evt:MouseEvent):void
{
(parent as Main).selectedSquare = this;
parent.addChild(this);
this.startDrag();
}
关于actionscript-3 - AS3从子类访问父类的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2619067/