在Actionscript 3中的其他类上定义变量的问题

时间:2011-06-19 06:10:54

标签: flash actionscript-3

我有2班A班,B班。

我在班级DEGREE中有一个变量。

package com.sample
{ 
    //CLASS A
    import flash.display.MovieClip; 
    import flash.events.MouseEvent; 
    import flash.display.Stage; 
    import flash.events.Event; 
    import flash.display.Sprite; 
    import flash.sampler.StackFrame; 

    public class classA extends MovieClip
    { 
        public var DEGREE:Number = 0; 
        public function classA() 
        {
            addEventListener(MouseEvent.MOUSE_UP , OnMouseUp);
        }

        function OnMouseUp(evt:MouseEvent):void
        {
            DEGREE = this.flashshow.rotation;
        }
    }
}

在类b中使用下划线定义类a时:

B级:

public var myClassA:classA;

myClassA = new classA();

但是当我在b级获得DEGREE

trace(myClassA.DEGREE) ; 

它等于ZER0 no 30。

package com.sample
{
    import flash.display.MovieClip; 
    import flash.display.Stage; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import com.MrMind.flashShow;

    public class classB extends MovieClip
    {
        public var mmm:classA = new classA(); 
        public function classB () : void
        {
            addEventListener(MouseEvent.CLICK , mee);
        }

        public function mee(evt:MouseEvent):void 
        {
            trace(mmm.DEGREE); // OUT PUT ZER0
        }
    }
} 

任何人都可以提供帮助吗?

2 个答案:

答案 0 :(得分:1)

如果要在类之外访问它们的值而不实例化类,则可以使用静态变量。 public static var是全球性的,一些纯粹主义者可能会贬低你使用它,因为它们倾向于淡化面向对象程序设计的范式,所以谨慎使用它们并谨慎使用。

文件类:

package
{
//Imports
import flash.display.Sprite;

//Class
public class DocumentClass extends Sprite
    {
    //Constructor
    public function DocumentClass()
        {
        //Call ClassA's Static variable - Class A doesn't need to be instantiated.
        trace(ClassA.firstDegree);

        //Call CalssA's public variable - Error, class must first be instantiated.
        //trace(ClassA.secondDegree);
        //ERROR 1119: Access of possibly undefined property secondDegree through a reference with static type Class.

        //Issues may arrise via global values that can be set and retreived from anywhere.
        ClassA.firstDegree = 100;
        trace(ClassA.firstDegree);

        //Instantiate ClassA first, and then retreive its public variable
        var a:ClassA = new ClassA();
        trace(a.secondDegree);
        a.secondDegree = 50;
        trace(a.secondDegree);
        }
    }
}

A类:

package
{
//Class
public class ClassA
    {
    //Static Variables
    public static var firstDegree:uint = 30;

    //Variables
    public var secondDegree:uint = 40;

    //Constructor
    public function ClassA()
        {

        }
    }
}

输出:

30
100
40
50

答案 1 :(得分:0)

您好我发现您的变量在mouseUp上已更改,并且您的跟踪在Click上发生。这可能是问题所在。您可以尝试在mouseDown上更改DEGREE值并在mouseUp上跟踪它。它应该返回更改的值。我认为click事件发生在mouseUp事件之前。

祝你好运, 马可