Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8523709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:29:12+00:00 2026-06-11T07:29:12+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:29:13+00:00Added an answer on June 11, 2026 at 7:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have something like this situation: $(document).on('click', 'a[data-link]', function () { var $this
I have this weird situation. I have these two classes: Public Class Entry End
I have a situation like this. declare array of char*; switch(id) { case 1:
I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it
We have this situation: Window Keyboard ^ ^ | / ApplicationWindow so class Window
I have this situation: interface MessageListener { void onMessageReceipt(Message message); } class MessageReceiver {
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
I have the following situation: typedef void (*F_POINTER)(ClassName*); class ClassName { public: F_POINTER f;
Let's say I have this program: class Foo { public: unsigned int bar ()
I have this situation: http://jsfiddle.net/bRDgK/3/ In this situation I have a modal dialog with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.