actionscript-3 - AS3从子类访问父类的变量

标签 actionscript-3 casting parent-child

我正在尝试从 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/

相关文章:

sqlite - PureMVC 中的异步数据库访问层

image - JPG 与 PNG 的 AS3 CPU 性能

c++ - 从模板派生类转换为模板基类

c++ - 从 Type* 转换为 Type,反之亦然

css - 覆盖 Wordpress 子主题中的样式

css - 如何将文本 div 完美地放置在图像 div 旁边,而不用文本环绕它?

php - 从父子中计算子级 php mysql 5 级深

android - 在(Air for android)发布者处调用可能未定义的方法 loadmovie

iphone - 云内容管理系统

swift - 不能使数字为负