'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 1:[1]
I'm not expiriance developer with starling but I have similar problem and I solved it by adding a static var and call methods from there. In your case you can use something like this:
in your Game class add: public static var mainDoc: MovieClip;
in your main method add: Game.mainDoc = this;
then in your Game class instead of rising an events you can call a method mainDoc.onCloseApp()
also you can try _starling.addEventListener or _starling.stage.addEventListener as say Antoine Lassauzay
Solution 2:[2]
This is because Starling has its own version of event dispatching.
What should work is
- Dispatch a
starling.events.Eventinstead of aflash.events.Event - Add a listener directly on the
_starlingor_starling.stageobject.
Solution 3:[3]
@Boris already explained that.
I have resolved problem.
Example I made File from flash.filesystem.File in App.as (My application was created by Moonshine) and TextInput from feathers.controls.TextInput; in Main.as with Starling's events
Like that:
package
{
import Main;
import feathers.utils.ScreenDensityScaleFactorManager;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageOrientation;
import flash.display.StageScaleMode;
import flash.display3D.Context3DProfile;
import flash.display3D.Context3DRenderMode;
import flash.events.Event;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.system.Capabilities;
import flash.utils.ByteArray;
import starling.core.Starling;
import flash.net.FileFilter;
[SWF(width="640",height="480",frameRate="60",backgroundColor="#4a4137")]
public class NewFeathersSDKDesktopProject extends Sprite
{
public function NewFeathersSDKDesktopProject()
{
if(this.stage)
{
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
}
this.mouseEnabled = this.mouseChildren = false;
this.showLaunchImage();
this.loaderInfo.addEventListener(Event.COMPLETE, loaderInfo_completeHandler);
}
private var _starling:Starling;
private var _scaler:ScreenDensityScaleFactorManager;
private var _launchImage:Loader;
private var _savedAutoOrients:Boolean;
...
private function loaderInfo_completeHandler(event:Event):void
{
Starling.multitouchEnabled = true;
this._starling = new Starling(Main, this.stage, null, null, Context3DRenderMode.AUTO, Context3DProfile.BASELINE);
this._starling.supportHighResolutions = true;
this._starling.skipUnchangedFrames = true;
this._starling.start();
if(this._launchImage)
{
this._starling.addEventListener("rootCreated", starling_rootCreatedHandler);
}
this._scaler = new ScreenDensityScaleFactorManager(this._starling);
this.stage.addEventListener(Event.DEACTIVATE, stage_deactivateHandler, false, 0, true);
// dispatch from Main.as dispatchEventWith();
this._starling.addEventListener("trigger", button_triggeredHandler)
}
...
private var fileopen:File;
public function button_triggeredHandler(event:Object):void
{
fileopen = new File();
var txtFilter:FileFilter = new FileFilter("Text", "*.as;*.css;*.html;*.txt;*.xml");
try
{
fileopen.browseForOpen("Open", [txtFilter]);
fileopen.addEventListener(Event.SELECT, fileSelected)
}
catch (error:Error)
{
trace("Failed:", error.message);
}
}
public function fileSelected(event:Event):void
{
Main.textinput.text = fileopen.nativePath;
}
}
}
And Main.as
package
{
import feathers.controls.Button;
import feathers.controls.TextCallout;
import feathers.themes.MetalWorksDesktopTheme;
import starling.display.Sprite;
import starling.core.Starling;
import starling.events.Event;
import feathers.core.Application;
import feathers.controls.TextInput;
public class Main extends Application
{
public function Main()
{
new MetalWorksDesktopTheme();
super();
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
protected var button:Button;
public static var textinput:TextInput;
protected function addedToStageHandler(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
this.button = new Button();
this.button.label = "Choose";
textinput = new TextInput();
//an event that tells us when the user has tapped the button.
this.button.addEventListener(Event.TRIGGERED, button_triggeredHandler);
this.addChild(this.button);
this.addChild(textinput);
this.button.x = this.button.y = 10;
textinput.x = 90;
textinput.y = 10;
textinput.width = 400;
}
private function button_triggeredHandler(event:Event):void
{
// Call to Application's class ( App or created by Moonshine IDE )
Starling.current.dispatchEventWith("trigger");
}
}
}
I hope you know how do you get like your air app works fine like charm :D
PS: I am really surprised why do Actionscript 3 developers continue until now? I thought Flash Player is died. Since I exchange to Net developer - I don't expect there AS3 developers skip with Harman Air. I checked news - I am shocking why does company Harman continue and works hardly. I don't understand why does Harman release Linux with Commincial version :(. And successor Moonshine IDE. I find okay. but I wish dark mode on Moonshine IDE. Becuase my eyes are sensitive.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Azzy Elvul |
| Solution 2 | |
| Solution 3 |
