public function access fail

I have this situation where I declare inside my main class a function that looks like this:

public class Main extends MovieClip
{

    public static var instance:Main;

    public function Main()
    {
        // constructor code
        welcomeScreen();
        instance = this;
    }

    public final function welcomeScreen():void
    {
        //some code in here
    }

    public final function startLevelOne():void 
    {
        //some other code here
    }


}

In some other class I use this statement to fire a reset:

restart.addEventListener('click', function() {                                      
     Main.instance.welcomeScreen();
});

Somehow in another class I try to use the same statement for 'startLevelOne' but it seems it is not working and gives the fallowing error:

1195: Attempted access of inaccessible method startLevelOne through a reference with static type Main.

Any ideas?

UPDATE #1

The class where I try to access the function is in full this one:

public class LevelBrief extends MovieClip
{

    public function LevelBrief()
    {
        // constructor code
        startBut.addEventListener('click', function() {
            Main.instance.startLevelOne();
        });
    }
}

UPDATE #2

I have pasted the full code of the main definition here http://pastebin.com/s6hGv7sT

Also the other class could be found here http://pastebin.com/s6h3Pwbp

UPDATE #3

Even though the problem was solved with a workaround, I still cannot understand where was the problem.

Answers


I would recommend to leave the static instance (singleton), and work event-based. Now you make all functions public, which is not desirable. It's not that hard to use custom events. See this is how your Main class could look:

public class Main extends MovieClip
{
    public function Main()
    {
        this.addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
    }

    public function handleAddedToStage(event:Event)
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);

        this.showWelcomeScreen();

        stage.addEventListener(ScreenEvent.SHOW_WELCOME_SCREEN, handleScreenEvent);
        stage.addEventListener(ScreenEvent.SHOW_LEVEL, handleScreenEvent);
    }


    private function handleScreenEvent(event:ScreenEvent):void
    {
        switch (event.type)
        {
            case ScreenEvent.SHOW_WELCOME_SCREEN:
            {
                this.showWelcomeScreen()
                break;
            }
            case ScreenEvent.SHOW_LEVEL:
            {
                // event.data contains level number
                this.startLevel(event.data);
                break;
            }
            default:
            {
                trace("Main.handleScreenEvent :: Cannot find event.type '" + event.type + "'.");
                break;
            }
        }
    }

    private function showWelcomeScreen():void
    {
        trace("show WelcomeScreen")
        //some private code in here
    }

    private function startLevel(level:int):void 
    {
        trace("start level: " + level)
        //some other private code here
    }
}

This is how the custom event class should look (ScreenEvent.as). Note it has an optional parameter called data. You can pass any value (objects, numbers, strings etc) into this. To the example as clear as possible, I used one event-class for both actions, you can also choose to make more specific custom events for other actions with more detailed parameters, you would have names like ScreenEvent, LevelEvent, PlayerEvent, GameEvent etc etc..

At the top of the class the (static constant) types are defined. An event should only have getters.

package 
{
    import flash.events.Event;

    public class ScreenEvent extends Event
    {
        public static const SHOW_WELCOME_SCREEN:String = "ScreenEvent.showWelcomeScreen";
        // event.data contains level number
        public static const SHOW_LEVEL:String = "ScreenEvent.showLevel";

        private var _data:String;

        public function ScreenEvent(type:String, data:String):void 
        { 
            super(type);
            this._data = data;
        }

        public function get data():String
        {
            return this._data;
        }

        override public function clone():Event 
        { 
            return new ScreenEvent(this.type, this._data);
        }
    }
}

.. Anywhere in your code you can dispatch the event to the stage.

// dispatch event to Main (stage). Should show welcome screen in our case
stage.dispatchEvent(new ScreenEvent(ScreenEvent.SHOW_WELCOME_SCREEN));

// show level 2
stage.dispatchEvent(new ScreenEvent(ScreenEvent.SHOW_LEVEL, 2));

I know, its a bit more code, it looks more difficult at first but if the project grows, it will help a lot. The difference with events is 'this could happen, and when it happens, do this' instead of 'do this here, do that over there' The advantage is that if you remove the event listener in the Main class, nothing will break (loosely coupled). This makes it easier to maintain, it saves a singleton, and you have the ability to extend the Main class if you want to.


Need Your Help

Connect from a client to a sails based server

cordova socket.io sails.js sails.io.js

I am trying to connect to my sails based server from my cordova client using the library sails.io.