0

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

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?

8
  • 2
    What is the full definition of DEGREE? And where is the assignment of 30 taking place? Commented Jun 19, 2011 at 6:16
  • var DEGREE:Number; DEGREE = 30; DEGREE define in mouse_up event Commented Jun 19, 2011 at 6:26
  • 1
    And where the assignment is taking place? I am suspecting you are tracing the number before it is assigned. And is this public? If no, then how you are accessing in class B? Please post the real relevant code. Otherwise its difficult to guess where is the problem. Commented Jun 19, 2011 at 6:28
  • CLASS A : 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 sample extends MovieClip { public var DEG:Number = 0; public function sample() { addEventListener(MouseEvent.MOUSE_UP , OnMouseUp); } function OnMouseUp(evt:MouseEvent):void { DEG = this.flashshow.rotation; } } } Commented Jun 19, 2011 at 6:54
  • 1
    Is DEG and DEGREE same? You have initialized it with zero. No wonder that trace output is zero. Commented Jun 19, 2011 at 6:56

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thx alot but i want change value and can't use static , when i change a value of variable in mouseUp Event , dont set it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.