Search code examples
classactionscript-3flashdevelop

How to access Number through separate class?


Hey everyone so this is something that I have always had trouble trying to accomplish or understand. So I have my main Engine class calledescapeEngine where I have a private var nScore I want to be able to access this variables through a separate class called mcPlanets but I don't know how I would accomplish this. I know how to do the opposite but not how to access a var from my main Engine class. Can anyone help me out?


Solution

  • I am not sure what you are trying to do, but here is an example that may help you:

    Inside esacapeEngine class (main), create a public var nString and new instance of mcPlanets.

    // two lines in escapeEngine.as
    var nScore = 0;
    var mcPlant = new mcPlanets(this);   
    

    So, when you create new mcPlanets, pass in the reference (keyword 'this' in the parentheses). Now mcPlanets knows about your main class.

    And now in mcPlanets class, write this:

    public class mcPlanets 
    {
        private var escapeEngine;
    
        public function mcPlanets(main)  // 'this' = 'main'
        {
            escapeEngine = main;
    
            // access nScore defined in main class
            escapeEngine.nScore = 5;
        }
    }
    

    In this example, nScore must be a public variable, it could be a private but you should use 'get and set' methods.