views:

966

answers:

4

Hi,

I've got a AS3 program with a Main.as custom class.

In this class I load an instance of a 'menu' movieclip which has simpleButton instances inside... How do I access the Main class public functions by the menu movieclip buttons?

I.e. Menu button -> gotoPage(5); (which is a Main public function)

If I try to access the Main function with the above statement, it gives

"1180: Call to a possibly undefined method gotoPage.

+1  A: 

Create a static method called GetMain() on the Main class that would return the instance of Main (Main should be a singleton).

package whatever
{
  public class Main
  {
    private static var _instance:Main = null;

    public static function getMain():Main
    {
      return _instance;
    }

    // Main constructor
    function Main(..):void
    {
      _instance = this;
    }
  }
}

To refer to the instance of Main() from your Menu class, you could use:

Main.getMain().gotoPage(5);
Lior Cohen
Awesome, works perfectly! Thanks!
PJ Palomaki
A: 

Uhh, suddenly it stopped working... It gives this:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Main/init()
    at Main/onAddedToStage()

Here are the (relevant) Main class info:

     private static  var _instance:Main=null;

            public static  function getMain():Main {
      return _instance;
     }

     function Main():void {
      addEventListener(Event.ADDED_TO_STAGE,onAddedToStage,false,0,true);
      _instance = this;
     }

        private function init():void { ... }

     private function onAddedToStage(e:Event):void {
      init();
      removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
     }

What have I messed up here? I'm still not fully clear on the class structure...

PJ Palomaki
Full Main.as here: http://paste.ideaslabs.com/show/tHMi86GFKK
PJ Palomaki
Found the culprit: for (var i:Number=0; i < 10; i++) { _searchResults.addItem({data:_my_images[i].@id,label:_my_images[i].@description}); } callThumbs(_searchResults);This was under init() and was sending null to callThumbs which flipped it out.. _searchResults is a XMLList class and it doesn't work with .addItem so it remained null.
PJ Palomaki
A: 

You want to do this with events. If your menu movieclip is a child of Main.as as you say, name the instance buttons inside of the menu movieclip, and set up the listeners in Main.as:

1) Put the below code in the constructor: public function Main(){...

menu.button_a.addEventListener(MouseEvent.CLICK, onButtonClick);
menu.button_b.addEventListener(MouseEvent.CLICK, onButtonClick);

2) and then write the onButtonClick function in Main.as

private function onButtonClick(e:MouseEvent):void{
   switch(e.currentTarget.name){
       case "button_a":
          //call the Main.as function you want here
          break;
       case "button_b":
          //call a different Main.as function
          break;
}
ruedaminute
Uhm, I've got loads of buttons already created, what would be the best way to link to specific frames (gotAndStop(frame);) from multiple pages with multiple buttons?
PJ Palomaki
A: 

ruedaminute's answer on dispatching events from the buttons and having main process those events is by far the best way to handle this, but there are many ways to do this in as3 - but try to use the aforementioned technique. Some of the other techniques.

  • Make a function in Main such as public function GotoPage(iPageNum:int):void{}

from a button - try this._parent.GotoPage(1);

but this._parent might not be main, do a trace(this._parent), and keep trying it might end up being this._parent._parent._parent.GotoPage(1) depending on your display tree hierachry.

Again, this is REALLY bad OOP practices, but well, it will work.

Another tecnique - use a singleton for main- looks like u already are - add that same public method, then from the button click, you could do Main.getMain().GotoPage(1);

That is a bit better, in that you can change the display tree and not have to figure out where the heck Main is in the display tree, but singletons also are discouraged for a variety of reasons, but in this case I would say it makes since.

Good Luck! ~ JT

JTtheGeek