Flash Cs6 AS3 - trap ESC key to prevent exiting fullscrenn - actionscript-3

I am building a kiosk game for a trade show and want to prevent users from exiting fullscreen with ESC.
I am using Flash CS6 IDE.
I can trap the ESC key with a onKeyDown event, but I can't prevent the ESC key from exiting fullscreen on the Windows executable.
I saw a post with a solution for AIR and Flex and saw your note about stage.nativeWindow as the thing to use in Flash IDE, but I am getting a "Access of possibly undefined property nativeWindow through a reference with static type flash.display:Stage." error.
Here is the code I am using:
function init():void
{
stage.nativeWindow.addEventListener(KeyboardEvent.KEY_DOWN, escapeTrap);
stage.nativeWindow.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
function escapeTrap(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.ESCAPE)
{
event.preventDefault();
}
}
Thanks!

Related

How to hide debug Adobe Player popup window in Flex?

When I run a Flex application in the debug flash player I get an exception pop up as soon as something unexpected happened. I would like to catch any exception that can happen anywhere in the Flex code and if it happens don't allow Adobe Debug Flash Player open his pop up window with this exception or error.
I tried:
[PostConstruct]
public function init():void
{
FlexGlobals.topLevelApplication.systemManager.stage.loaderInfo.
uncaughtErrorEvents.
addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,uncaughtErrorHandler);
}
private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
event.preventDefault();
event.stopImmediatePropagation();
}
but it don't work, general runtime error dialog will still pop up. I tried to prevent this, call event.preventDefault() inside my global error handler, but it don't work to me too. I'm using Flex 4.
If you add this line to your mm.cfg file: SuppressDebuggerExceptionDialogs=1 the exception dialogs will not appear any longer. The exception will still be in your flash logs.
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7fc9.html

SWF Loader -> TypeError: Error #1009 (Null reference)

I've read the previous posts of SO and it seems that some people's had the same issue as me - but none came up with a solution that helped my problem :)
I've designed 3 games that are their own SWF file, and I'm trying to load them upon a keypress in a container-program, sort of :).
I'm getting the TypeError 1009 null reference on all my SWF files though :( and they run perfectly when I load them from Windows. I've tried loading a SWF made by someone else, and that did work. So maybe my way of designing flash games is a bit off?
Everything takes place in frame 1 - that's maybe something that's off.
My loader code is:
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeysDown);
function loaderFunction(swfFile:String):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest(swfFile));
}
function onComplete(e:Event):void {
var movie:* = LoaderInfo(e.currentTarget).content;
//Adding content to the stage
stage.addChild(movie);
}
function reportKeysDown(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.NUMBER_1) {
stage.removeChildren(1);
loaderFunction("Card.swf");
}
if(event.keyCode == Keyboard.NUMBER_2){
stage.removeChildren(1);
loaderFunction("Puzzle.swf");
}
if(event.keyCode == Keyboard.NUMBER_3){
stage.removeChildren(1);
loaderFunction("BallGame.swf");
}
}
And the error is
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at BallGame_fla::MainTimeline/frame1()
(I get these for everyone of the SWF's)
I'm not sure how to debug those games I'm loading since there's nothing wrong when I play separately.
Any ideas :)? Thanks!
Edit:
It started telling me which lines were acting up, and it seems to be the keyboard-listener, at least for one of the games. I'm reading keyboard into much like I am with the loader swf.
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
Solution-Edit:
The problem is that the loaded SWFs are using the "stage" property, and when they're loaded into another SWF the stage isn't theirs to own. That's what's causing the errors :) #BotMaster
Solution-Solution Edit:
You can still look for keyboard inputs (like I was doing) in a nested SWF, if you're using the
Event.ADDED_TO_STAGE
Like this, for instance :)
this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
function onAdded(e:Event):void{
trace("added");
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
}
#null
stage only becomes available (it's not null) when the DisplayObject is added to the display list.
The only exception to that rule is the main file or the first file that is opened in the flash player.
That's why each of your games plays fine individually.
You should only use stage property if it is available. To do this, listen for the Event.ADDED_TO_STAGE event. This will be dispatched when something is added to the stage. In the handler function, you can safely use stage.
You still should add the listener to the stage but only if it is available to you.
Please read the documentation about addChild(). You should basically never add anything to stage. In general, take a look at the basics of the display list and how it works.

How to capture RIGHT_CLICK in FlashDevelop?

I wish to add an event listener for the MouseEvent.RIGHT_CLICK event in FlashDevelop.
In Flash CS6's editor, I can simply do:
stage.addEventListener(MouseEvent.RIGHT_CLICK, onMouse);
But MouseEvent.RIGHT_CLICK does not seem to exist in FlashDevelop.
I tried 'disabling' the context menu:
stage.showDefaultContextMenu = false;
But still to no avail. How can I get this to work?
Make sure your FlashDevelop project settings target a Flash Player version high enough to support the RIGHT_CLICK event; that being at least Flash Player 11.2.

Flash CS5.5 AS3 bug - button works everywhere except via HTML page

I have encountered a strange bug. I have buttons advancing one frame forward and back.
It works in the SWF on its own, it works in both the .app and .exe projector files, but it does not work accessing the SWF via the HTML generated page.
Does anyone have an inkling what's going on?
The code on the two buttons is:
import flash.events.MouseEvent;
stage.displayState = StageDisplayState.FULL_SCREEN;
function next1Click(event:MouseEvent): void {
gotoAndStop(this.currentFrame + 1);
}
function back1Click(event:MouseEvent): void {
gotoAndStop(this.currentFrame - 1);
}
//EVENT LISTENER AND VARIABLE DECLARATION
balancingNext.addEventListener(MouseEvent.CLICK, next1Click);
balancingBack.addEventListener(MouseEvent.CLICK, back1Click);
Thanks in advance for any help!
It's not a bug, but the intended bevahior. For this to work, allowFullScreen must be true, and you can't activate it without a user event, otherwise the program will throw an exception (and crash, since you don't catch it).
The ActionScript that initiates full-screen mode can be called only in
response to a mouse click or keypress. If it is called in other
situations, it will be ignored (in ActionScript 2.0) or throw an
exception (in ActionScript 3.0).
See Adobe's documentation.

How to detect if the delete key was pressed in Actionscript 3?

How do I determine if the delete key was pressed using actionscript?
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
...
function onKeyUp(event:KeyboardEvent):void
{
trace(event.keyCode);
}
The above code yields no value when delete, backspace, enter, and other command keys are pressed. However, arrow keys do yield values.
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
....
function onKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode==Keyboard.DELETE) {
.....
}
}
it's workin nice...
But if you test movie from Flash, it will not work, so export to swf and test....
Just guessing you are using the TEXT_INPUT event, this doesn't work for delete and backspace. To catch those ones you can add an eventListener on the stage and listen to a KeyboardEvent.
Code will work fine if the display object that you attached the listener is in focus. For global listening, as Theo said, you have to attach the listener to the stage. Accessing stage from an object that's not yet added to the display list will result in null error. Do it in the ADDED_TO_STAGE event handler to be safe.
Old thread, but if anyone gets this far: in the Flash Player inside the IDE, these keys are associated with shortcuts. When you test your movie, choose Control>disable keyboard shortcuts in the player and you'll get the events back.