开发者

problem in define variable on other class in Action Script 3

I have 2 class Class A , Class B.

I have a variable in Class a 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 fla开发者_如何学Pythonsh.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;
        }
    }
}

when define class a in class b with under line :

class B :

public var myClassA:classA;

myClassA = new classA();

but when i get DEGREE in class b

trace(myClassA.DEGREE) ; 

it equal 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
        }
    }
} 

anyone could help?


you can use static variables if you want to access their values outside of a class without instantiating the class. public static var is global, which some purists might lambaste you for using since they have the tendency of dilute the paradigm of object-oriented programming, so use them sparingly and with caution.

Document Class:

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);
        }
    }
}

Class A:

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

    //Variables
    public var secondDegree:uint = 40;

    //Constructor
    public function ClassA()
        {

        }
    }
}

Output:

30
100
40
50


Hi i see your variable is changed on mouseUp and your trace happen on Click. That might be the problem. You could try to change DEGREE value on mouseDown and trace it on mouseUp. It should return the changed value. I think the click event happen before the mouseUp event.

Best of luck, marco

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜