Milkduds
Milkduds

Reputation: 25

AS3: Access my Main.as from a movieclip on stage

Using Adobe Flash Professional CS6, AS3
Please let me know if I need to provide any more info
I am trying to set up a navigation menu and my question is, "How do I get a MovieClip to call a function in my Main.as file from the Stage?" There is only one frame (I do not want more) and the different menu screens, which are just MovieClips, are added to the stage with AS when needed. I have a public function in the file Main.as called _About(), that my MovieClip, "MenuScreen", cannot access. I can successfully have _Menu() add a MovieClip to the stage with eventListeners, but when the mc makes the call back to change screens I get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@51ca0d1 to flash.display.MovieClip. at MenuScreen/clickAbout()[MenuScreen::frame1:32]

Frame1:32's code is:

MovieClip(parent)._About();

Line 51 in my Main.as is:

public function _About():void
{
trace("The About Function");
}

Below I have detailed more about Main.as with most of the fat trimmed.

package 
{
import stuff

public class Main extends MovieClip
{
    //Load the screen MCs onto the stage
    public var _menu:MenuScreen = new MenuScreen();
    public var _about:AboutScreen = new AboutScreen();
    public var isMenu:Boolean = true;
    public var isAbout:Boolean = false;

    public function Main()
    {
        _Menu();            
    }

    public function _Menu():void
    {
        isMenu = true;
        stage.addChild(_menu);
    }

    public function _About():void
    {
        trace("The About Function");
    }

Upvotes: 0

Views: 1639

Answers (3)

Larusso
Larusso

Reputation: 1151

An easy solution to your problem would be to add the menu items not to the stage! Instead add them to your main class. This way the parent of your items is instead main.as

But then you need to cast the parent to Main

Main(parent)._About();

Also not very nice. The items should not now what is behind them.

The best way is to do it, is to dispatch events from the different screens. Means: you create your screen objects an there are dispatching custom events when a screnn change should happen.

dispatchEvent(new Event("showAbout"));

in your main class you handle the events like:

public function Main()
{
    _Menu(); 
    _menu = new MenuScreen();
    _menu.addEventHandler("showAbout", showAboutHandler);
}

public function showAboutHanlder(e:Event):void
{
     _About();
}

Even more better is a custom event with a screen identifier as a param. This way you just add one handler and decide in the handler code which screen to be displayed.

With the event handling in place, your menu items have no direct connection to the main. Also the main needs no further information about the screen classes.

Upvotes: 1

turbosqel
turbosqel

Reputation: 1542

Set static property :

public static var instance:Main;
public function Main(){
        instance = this;
        _Menu();            
    }

and then from anywhere You can use code :

Main.instance._About();

Upvotes: 0

Carl Miller
Carl Miller

Reputation: 142

It would help to see MenuScreen's complete class, but here's what's probably going on:

You're instantiating at public var _menu:MenuScreen = new MenuScreen(); and when this happens it's probably making the call to MovieClip(parent)._About() before you've added MenuScreen to the stage at stage.addChild(_menu); - It doesn't have a parent yet when this occurs and the error incurs.

Two ways to get around this:

  • Add a required parameter in MenuScreen's constructor that references your Main class. Your constructor in MenuScreen would start with public function MenuScreen($main:Main){ and when you instantiate MenuScreen from Main class you would write public var _menu:MenuScreen = new MenuScreen(this); now you can use $main._About() in MenuScreen's constuctor.
  • In MenuScreen's constructor add a listener that checks when it's been added to the stage: addEventListener(Event.ADDED_TO_STAGE, addedStage). In the function addedStage you can properly call for a parent or the stage, and MovieClip(parent)._About() will probably work here.

Upvotes: 0

Related Questions