Replace KeyboardEvent to work with Aid for Android - actionscript-3

I have made ann app that works fine on windows. The problem is that i use the following event listener:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
but when i compile my app for Android (Air 23) this event is not working.
So i would like to ask which event i will use for AIR in Android to replace that event?

You have to add your event listener to the Native Application instance:
import flash.desktop.NativeApplication;
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)

Related

libgdx: How to stop touchUp event?

As I can see from this article you can easily use "stop" method to stop event propagation, but this doesn't work for "touchUp/touchDown" event (event isn't stopped). I have also tested it for "mousemove" event and found that it works for it. Why? How can i stop "touchUp/touchDown" event?
PS: libgdx version: 1.2.0,
I'm using scene2d.
From github wiki:
The InputMultiplexer will hand any new events to the first InputProcessor that was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, the MyUiInputProcessor can handle any events that fall inside one of its widgets and pass on any other events to the MyGameInputProcessor.
TL;DR: Return true from your handle function if you want to stop it from propagation.
Also, what version of LibGDX are you using? I use 1.2.0 and the touchUp() method returns a boolean, unline that in the tutaril from your question.

AIR iOS app hangs when clicking button inside imported swf

Been bangin my head against a wall all day, thought I'd see if anyone can shed some light on this -
I have an iOS air app that imports a remote swf. Once imported, an event listener is added to a button inside the imported swf. Clicking the button causes the app to hang. Here's some code -
private function loadRemoteSWF():void{
var urlRequest:URLRequest = new URLRequest("http://www.domain.com/remote.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(urlRequest);
}
private function onLoaded(e:Event):void{
var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
var adPanel:MovieClip = loaderInfo.content as MovieClip;
adPanel.continueButton.addEventListener(TouchEvent.TOUCH_BEGIN, onContinueClicked);
addChild(adPanel)
}
private function onContinueClicked(e:TouchEvent):void{
trace("onContinueClicked");
}
I'm using Flash Builder 4.7 AIR SDK 3.5 ASC 2.0.
This only happens on a release build, debug builds work fine so near impossible to find the cause. It also works fine when using the legacy compiler on the same SDK version.
Dispatching a touch event programatically on the button also works fine. (thought I could do a try/catch to find an error)
adPanel.continueButton.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_BEGIN));
Touching the button just kills the app, it doesn't even hit the trace.
Anyone got any ideas how to debug this, or why this problem might be happening?
Thanks
Dave
can you do a test: instead of using the the native loader, try using an API, like greensocks api - SWFLoader.
Also, try setting the accepted domains through the security class prior to loading the swf file:
import flash.system.Security;
public class Main extends MovieClip{
public function Main(){
Security.allowDomain("*");
}
}
I would suggest trying to compile with the same setup on a different machine, just to make sure that the Builder install doesn't have some issues.
Also, have you tried on different devices, (could be an issue with the device you are using)?

Why does my EVENT.ACTIVE trigger twice?

When I add an EVENT.ACTIVATE listener to my project, and then alt-tab away and back to my project it triggers twice.
edit: shaunhusain and I seem to have found the cause of the problem, although without a solution. When running the standalone player version 11+ the event triggers 2x. When running standalone player version <11 or any version in the browser it triggers 1x. So it appears there may be a bug in recent versions of the flash player projector. I'm going to nail down the exact versions and report it to adobe and see what happens. Thanks to anyone who read this and tried to help!!
I want it to fire every time I change focus, I just don't want it to fire twice every time I change focus.
Why is this? Am I doing something wrong? What's the best way to prevent this behavior?
It seems like it would be a common question, but Google turned up nothing.
Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
stage.addEventListener(Event.ACTIVATE, test);
}
private function test(e:Event):void
{
trace(e.target);
}
}
}
Actual result:
[object Stage]
[object Stage]
Desired result:
[object Stage]
It doesn't seem to make a difference whether I add the listener to the stage or anything else, the behavior is the same.
The same thing also happens with EVENT.DEACTIVATE. Others such as mouse up work fine.
My goal is to pause a game on EVENT.DEACTIVATE and unpause it on EVENT.ACTIVATE. The problem is that when the event fires twice, it calls the unpause function twice which has unwanted consequences.
ActionScript® 3.0 Reference for the Adobe® Flash® Platform says about this event:
Dispatched when the Flash Player or AIR application gains operating
system focus and becomes active. This event is a broadcast event,
which means that it is dispatched by all EventDispatcher objects with
a listener registered for this event. For more information about
broadcast events, see the DisplayObject class.
For me it looks like you want to prevent its designed behavior? I suppose it was designed to fire every time you change focus, or am I wrong? What do you want to accomplish? However this is an answer, so based on what you wrote - you can do a workaround by just removing a listener after he fired once:
private function test(e:Event):void
{
stage.removeEventListener(Event.ACTIVATE, test);
trace(e.target);
}
But I would recommend you to write something more about why are you using it and what want to accomplish if this is not satisfactory.
I've had the same issue in my AIR Mobile app.
To correct this issue, I've stored the last event name triggered for an Activate / Deactivate event. If it is attempted twice in a row, it just gets skipped with a return;
private static function onAppStateChanged(e:Event):void {
if (_STATE == e.type) {
return;
}
_STATE = e.type;
switch(_STATE) {
case Event.ACTIVATE: whenActivated.dispatch(); break;
case Event.DEACTIVATE: whenDeactivated.dispatch(); break;
}
}
At first, the value of _STATE begins with null, so that should allow it to pass through the first time around.
I bind both Event.ACTIVATE and Event.DEACTIVATE to the same listener, onAppStateChanged in my App's initialization method, like so:
_STAGE.addEventListener(Event.ACTIVATE, onAppStateChanged);
_STAGE.addEventListener(Event.DEACTIVATE, onAppStateChanged);
You can replace the Switch-Statement however you like, I just personally prefer using Robert Penner's AS3Signals (GitHub) to broadcast the resume/suspend signal across the App.

Handle an Event in an AS file that was dispatched from a Mobile Spark Component

I have an mxml file called HomeView.mxml in a mobile app built using Flash Builder 4.6. In that HomeView.mxml I have an HSlider, which is dispatching events when the thumb is moved. What I'm trying to do is to listen for that valueCommit event in an .as file elsewhere in the project.
Whenever the thumb moves, an event is dispatched in HomeView.mxml:
hSlider.dispatchEvent(new FlexEvent("valueCommit", true));
I can handle that event in HomeView.mxm, but not in an .as class called view.as
In view.as I import the FlexEvent and I then have, in addedToStageHandler:
stage.addEventListener(FlexEvent.VALUE_COMMIT, commitHandler);
Then there's a simple Handler:
private function commitHandler(e:FlexEvent):void
{
trace("committed!");
}
But this code never runs. I do have a handler in the HomeView.mxml file which handles the event, but for other reasons I need to handle it in view.as as well.
Thanks.
One of the cause is you have anywhere stopPropagation or stopImmediatePropagation methods. It is possible that this methods located in a code of the HSlider component. You may try to dispatch a custom event which doesn't extends the FlexEvent.

flash filereference select event not firing after using browse() and after selecting a file

This is the first time i am working in action script. I have seen a flash programmer working with it. since i know javascript i was able to work in action script.
I am creating a file upload component for my php projects.
Until now i have advanced to show the file dialog box using the FileReference api.
The select event is not firing for me. and i did not get any error.
import fl.controls.Button;
import flash.events.*
import flash.net.FileReference
var fr:FileReference = new FileReference();
fr.addEventListener(Event.SELECT,sico);
var myButton:Button = new Button();
addChild(myButton);
myButton.label = "Click Me";
myButton.addEventListener(MouseEvent.CLICK, function(e:MouseEvent)
{
myButton.label = "clicked";
fr.browse();
trace('clicked');
})
function sico(event:Event):void
{
//var file:FileReference = FileReference(e.target);
myButton.label = "selected";
trace('selected');
//trace("progressHandler: name=" + file.name + " bytesLoaded=" + e.bytesLoaded + " bytesTotal=" + e.bytesTotal);
}
The documentation said that file references should be called only during an event like mouse or keyboard.
what am i missing in the above code?
The example you posted works in Flash CS5 and Flash Player 10.3 which means the problem is not with the code.
Other potential causes of the issue could be:
The version of Flash Player which you are using is too old. FileReference requires Flash Player 9 or better. Right-click on the Flash Player or use trace(Capabilities.version) to see the version number.
Another part of the application is using a FileReference method (eg: FileReference.browse(), FileReference.upload(), FileReference.download(), FileReference.load(), FileReference.save()). Flash player 10+ allows only one file operation at a time.
If you are using a Mac, then FileReference may only dispatch the SELECT event when the SWF is run inside of a browser.
IT was like as stated by user:lukevanin in the comment section of his answer. and me too found the same. The problem was i had many frames in the timeline. since i was new to action script while toggling between F9 i was trying F5 so it created frames and it was looping continuously between that.