Access to functions and variables of another class


I have a Main class and a GameScreen class. In the GameScreen class, I have a button that returns the user to the main menu. However, my gotoMainMenu function is in the Main class. I tried making the gotoMainMenu function static, but inside that function I call non-static functions.

Currently I have this code which passes the gotoMainMenu function into my gameScreen object:

gameScreen = new GameScreen(gotoMainMenu);

In the GameScreen constructor I assign this to an instance variable, and then I add an event listener using that instance variable as the second parameter, like this:

backArrowButton.addEventListener(MouseEvent.CLICK, gotoMainMenuMainFunction);

Is this a good way to do this? If not, can you provide another solution that is not considered bad coding?

I have another problem that is very similar. I have a Hero class which needs access to many of the variables in my GameScreen class. I know static variables are a fix, but I was taught that this is bad coding, and that static variables should only be used for constants.

Currently, I pass in instance variables as parameters like this:

hero = new Hero(levelData, map, gameCont);

Then in the Hero class I assign these parameters to instance variables, very similar to how I pass in functions. These variables refer to the same object. I need to pass in primitive data types into the onEnterFrame function like this:

hero.onEnterFrame(up, left, right, mouse);

The problem with this is that I do not like storing instance variables, that refer to the same object, in 2 separate classes. I feel like there is a much better way to do this. Please provide a solution to these 2 problems, and make sure that it is not considered bad coding.

First of all, your solution of your first problem is indeed considered a bad coding practice.

Now let's get on with good ways to fix your problems. As I see you are creating a GameScreen instance in your Main class. That means you have a reference to that object, therefore you can add an event listener for a custom event that tells your Main class to return to main menu. Your code would be something like this:

// you just create your instance, without the Function parameter
gameScreen = new GameScreen();

// then you add an event listener for your custom event
gameScreen.addEventListener(MyGameEvent.GOTO_MAIN_MENU, gotoMainMenu);

Now note that this will pass an instance of your custom event as a parameter to the gotoMainMenu method, so the definition of your gotoMainMenu method should be something like this:

private function gotoMainMenu(e:MyGameEvent):void {...}
// or if you'd like to call this method on a click, use the parent of all events Event class:
private function gotoMainMenu(e:Event):void {...}

Note that we changed the MyGameEvent class with more general Event class.

Now you have to create MyGameEvent to add custom types and even custom data if needed. For this you have to extend Event class like this:


    public class MyGameEvent extends Event
        public static const GOTO_MAIN_MENU:String = "MyGameEvent.GOTO_MAIN_MENU";

        public function MyGameEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false)
            super(type, bubbles, cancelable);

When you need to return to main menu from the GameScreen just call the dispatchEvent method inherited from the EventDispatcher class (Sprites and MovieClips also have this method, since they inherit EventDispatcher):

dispatchEvent(new MyGameEvent(MyGameEvent.GOTO_MAIN_MENU));

If compiler gives you an error on this call, make sure your GameScreen class extends one of these: EventDispatcher, Sprite or MovieClip.

Passing references to your Hero class isn't that bad, it can become cumbersome in big projects as it couples your code, but you can use some design patterns as a solution, as Pier suggested.