event-handlingprogram-entry-pointstarling-frameworkactionscript-3

Starling / ActionScript 3 signaling events up to Main


Hi I have a little design Issue with Starling/ActionScript 3.

What I want to do is simple

 dispatchEvent( new flash.events.Event("CloseApp",true)); //send closing bubble events

while i have following methods in Main.as (main extends flash.display.Sprite)

 public Main():void //constructor
 {
     addEventListener( "CloseApp" , onCloseApp);
     _starling = new Starling(Game, stage); //everythings work ok
     _starling.start(); //nice 3d stuff and interactive menu ok.
 }

 //closing event handling: close the APP
 public function onCloseApp( e:flash.events.Event): void
 {
     NativeApplication.nativeApplication.exit();
 }

seems there's no way to send events to "Main" while all other classes are correctly catching events. basically the "exit" button is the only thing that does not work in my application.


Solution

  • Your event will only travel up to your Game instance. I think it should be the _starling.root

    so based on what I see you should call _starling.root.addEventListener("CloseApp", onCloseApp);

    BTW, if your button is a Starling display object it should dispatch a starling Event, not a flash one. You can use the newly created dispatchEventWith('CloseApp')

    and lastly I recommend you change the callback signature to public function onCloseApp( e:Object):void to prevent event class collisions.

    [EDIT] here is how I would handle your case. Provided you use starling >1.2 it should work :

    1. I would change Main like this :

      public static const EVT_CLOSEAPP:String = 'close app';
      public Main():void //constructor
      {
        _starling = new Starling(Game, stage); //everythings work ok
        _starling.start(); //nice 3d stuff and interactive menu ok.
        _starling.addEventListener(EVT_CLOSEAPP , onCloseApp);
      }
      //closing event handling: close the APP
      public function onCloseApp( e:Object): void
      {
        NativeApplication.nativeApplication.exit();
      }
      
    2. then, in whatever class you are creating your close butto

      //... i assume your button is in a variable btn
      btn.addEventListener(Event:TRIGGER, onCloseBtn);//this is starling.event.Event
      //...
      protected function onCloseBtn(_e:Event)
      {
        Starling.current.dispatchEventWith(Main.EVT_CLOSEAPP);
      }