Search code examples
actionscript-3flash-cs6flashdevelop

Accessing Variables from separate class


Hey everyone so I am trying to access boolean variables from a separate class called mcPressMachineusing my main class SmashyFoodEngine. What I want to do is when the score is >= 6 then change the boolean values like so in my Main Engine Class Enter Frame Event:

if (nScore >= 6)
        {
            for each(var AllPresses:mcPressMachine in aPressMachineArray)
            {
                AllPresses.level_1 = false;
                AllPresses.level_2 = true;
            }
        }

in my mcPressMachine class I have the booleans set up to control what frames of that Movie Clip class on the timeline will be played next. So basically if level_1 is true then play randome frames from (1,8) if level_2 is true then frames (9,13) etc... Which is setup like so in the pressMachine Class:

public var level_1:Boolean = true;
public var level_2:Boolean = false;

private function init():void 
    {

        if (level_1)
        {
            var randomFrameLevel_1:Number = randomNumber(1, 8);
            this.gotoAndStop(randomFrameLevel_1);

        }

        if (level_2)
        {
            var randomFrameLevel_2:Number = randomNumber(9, 13);
            this.gotoAndStop(randomFrameLevel_2);

        }
    }

so When I trace it and the score is >= 6 the boolean values change perfectly but nothing happens on the screen. The Movie Clips are the exact same and dont change to the level_2 frames (9-13);

Also this is the Movie clip in my Main Class that is added to the stage and im trying to change:

private function addNewPressMachines():void 
    {
        //have array of press machines come out 5 at a time. 
        for (var i:int = 0; i < 2; i++)
        {
            //trace(aPressMachineArray.length + "PRESS MACHINES");
            pressMachine = new mcPressMachine();
            pressMachine.x = startPoint.x + (xSpacing * i);
            pressMachine.y = (stage.stageHeight / 2);

            stage.addChildAt(pressMachine, 1);
            aPressMachineArray.push(pressMachine);

            pressMachine.inner.top.visible = false;
            pressMachine.inner.top.topText.text =  " " + sharedObjectHighScore.data.highScore;


        }
    }

Can anyone see if I am doing something wrong? I don't get any errors just not working properly. Any advice would be awesome. Thank you!


Solution

  • This is the best I can do at the moment.

    var _var1:Boolean;
    var _var2:Boolean;
    
    public function MyClass(first:Boolean,second:Boolean):void{
        this._var1 = first;
        this._var2 = second;
        if (_var1 == true) {
        // stuff
        }
        if (_var2 == true){
        // etc.
        }
    

    This passes your arguments from into the constructor code and saves the values in properties of that class instance.

    Or even simpler

    var _level:int;
    public function MyClass(lev:int=1):void{
         this._level = lev;
         if (lev == 1){
         // do stuff
         } else if (lev==2){
         // etc
         }
    

    You'll notice here that I've set the _level value equal to the lev argument. That's really only necessary if you need the instance of this class to have a _level property for other things. You see I could have just as well done this:

    public function MyClass(lev:int=1):void{
         if (lev == 1){
         // do stuff
         } else if (lev==2){
         // etc
         }
    

    in which I leave out the declaration of the _level property both in the class definition and also in the constructor code. What this means is you can pass variables into a constructor without that information being permanently stored in that specific instance of the class, but still have those parameters affect the result permanently.

    And then when you instantiate a new instance of that class just do

    var myInstance = new MyClass(true, false); // using the first example
    

    or in my better, simpler version

    var myInstance:MyClass = new MyClass(2); // for the second example