1

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!

Nathan
  • 536
  • 4
  • 21
  • You are doing this code on init() which gets called once when the machine is instantiated, yes? – Neal Davis Oct 14 '16 at 19:41
  • Yes, that is correct – Nathan Oct 14 '16 at 19:42
  • Are the booleans in the machine class just being reset every time a new one is added from my main class? – Nathan Oct 14 '16 at 19:43
  • 1
    Yes. You are declaring your variables as true and false in the constructor code. Then you are using a conditional that depends on those values but they will always be the same. You need to change the Boolean values and then call the init() function again. I would probably do this a bit differently. I'll post an answer if I get some time. But in the short term, try doing what I just suggested. And you'll have to make that a public function, not private (which is why I'd do it differently... Basically I'd just include those values as arguments in the constructor). – Neal Davis Oct 14 '16 at 19:47
  • Oh okay. I believe I know what you are getting. It yeah in the mean time ill try to work on that. Thank you for the quick response. Ill let you know how it goes, if I get it working – Nathan Oct 14 '16 at 19:49

1 Answers1

1

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
Neal Davis
  • 2,010
  • 2
  • 12
  • 25
  • This is something I was trying to figure out how to do. Still struggling but Ill keep trying new ways. Ill let you know how it goes. so in my Machine class I create those variables. Then I can change them in my Main Class when i add a new machine? – Nathan Oct 14 '16 at 20:24
  • Hey figured it out. Thanks man I appreciate all the help and quick responses. – Nathan Oct 14 '16 at 20:36
  • No problem. Good to hear you got it sorted. – Neal Davis Oct 14 '16 at 20:40