0

Ok, this is my problem: i have a variable in a class named Mirror

public var line:Sprite = new Sprite(); 

i want to use that variable in another class named MovieClip. I imported the class in the MovieClip class:

   public class MovieClips extends MovieClip
{
    import Mirror;
    public function MovieClips(radius:int, _x:int, _y:int,size:int,span:int,addl:Array) 
    {....

but when i try to do something like this:

if (Mirror.line.hitTestObject(ball) == true) {
                    speedY *= -1;
                }

It shows up like an error: Acess of a possibly undefined property line through a reference with static type Class. I'm not sure what did i do wrong? Am i importing my class in the wrong way?

4

2 回答 2

0

您应该将Mirror对象传递给您的MovieClips对象,如下例所示:

package {

    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var mirror:Mirror = new Mirror();
            var movieClips:MovieClips = new MovieClips(mirror);

        }// end function

    }// end class

}// end package

import flash.display.MovieClip;

class Mirror extends MovieClip {

    public var line:MovieClip;

    public function Mirror():void {

        line = new MovieClip();

    }// end function

}// end class

class MovieClips extends MovieClip {

    public function MovieClip(mirror:Mirror):void {

        mirror.line;

    }// end function

}// end class

您也可以通过Mirror.line只读来改进这一点:

class Mirror extends MovieClip {

    private var _line:MovieClip;

    public function get line():MovieClip {

        return _line;

    }// end function

    public function Mirror():void {

        _line = new MovieClip();

    }// end function

}// end class
于 2012-11-13T20:21:34.820 回答
0

如果你有一个类,喜欢Mirror并做类似的事情Mirror.line,这意味着你正在尝试访问一个名为 line 的静态类变量。静态变量/方法与类相关,而不是该类的实例。

您尚未将 line 定义为静态变量,因此您会收到错误消息。我不确定您是否希望它成为静态变量,并且很可能您只是将类与Mirror类的实例混淆了Mirror

您可能在某处创建 Mirror 类的实例:
var myMirror:Mirror = new Mirror()或者如果使用 FlashPro,则在时间线上拖动一个实例(然后可以给它一个“myMirror”实例作为示例)。

您将访问lineMirror 类的实例。所以有了上面的内容myMirror.line

导入一个类不会让您访问该类的任何实例,因此如果您希望能够Mirror从您的MoveClips类中访问一个实例,您需要传入对它的引用,或者使用parent关键字来获取到正确的范围(因此,如果您的 Mirror 实例和您的 MovieClips 实例在同一时间线/父级上,您可以这样做Mirror(MovieClip(parent).myMirror).line- 假设您的父级是 MovieClip 类型)。


如果您确实希望它作为静态变量,只需将静态访问修饰符添加到它的定义中。

public static var line:Sprite

请记住,静态变量始终在内存中,无论您是否创建了它们的任何实例。

于 2012-11-10T22:30:09.880 回答