AS3: Exit full screen event listener - actionscript-3

How to add an event listener that listens to the exit full screen event by pressing escape key ??
stage.addEventListener(Event.RESIZE, backtoresize) //doesn't work :(
Thanks :)

I had it like this.
mcVideoControls.btnFullscreen.addEventListener(MouseEvent.CLICK, fullscreenClicked);
function fullscreenClicked(e:MouseEvent):void {
//fullscreen works only with an internet browser
if (stage.displayState == StageDisplayState.NORMAL) {
stage.displayState = StageDisplayState.FULL_SCREEN;
}
else {
stage.displayState = StageDisplayState.NORMAL;
}
}
But you could rewrite it. It would then be something like this.... wait hang on...
package {
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class UserInputHandler{
//escape button var
public static var keyEscape:Boolean;
public function UserInputHandler(stage:Stage){
//this events are sending the value true when specific keyboard button is pressed to the stage.
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
//you can provide more key codes in the function
private function keyDownHandler(e:KeyboardEvent):void{
switch(e.keyCode){
case Keyboard.ESCAPE:
UserInputHandler.keyEscape = true;
break;
}
}
//function when key is released from pressing
private function keyUpHandler(e:KeyboardEvent):void{
switch(e.keyCode){
case Keyboard.ESCAPE:
keyEscape = false;
break;
}
}
}
}

stage.addEventListener(FullScreenEvent.FULL_SCREEN, etc ...)
This triggers whether you enter or leave fullscreen.

Try:
stage.nativeWindow.addEventListener(Event.RESIZE, backtoresize);

Related

AS3 VideoEvent Listener - Event.Ready

I am trying to catch READY event of FLV Video Player. I am not quite sure what is going on with this codes. FLV player is imported by Adobe Flash and I need to start readyHandler whenever video is ready to play (Function will erase the "Video is loading" text). I used video_oynattir object flawlessly. However, these codes brake the animations...
video_oynattir.addEventListener(Event.READY, readyHandler);
function readyHandler(event:VideoEvent):void
{
trace("hurray");
}
And produce this error:
Accesss of possibly undefined property READY through a refferance with static type class*
If I comment out code segment. It executes perfectly.
Full code of stage as follows:
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.net.URLRequest;
import flash.net.navigateToURL;
var clickTAG:String = loaderInfo.parameters.clickTAG;
var displayType:String = loaderInfo.parameters.displayType;
video_oynattir.autoPlay = false;
if(displayType == "collapse") {
gotoAndStop("kapali");
}else{
openButton.visible = false;
gotoAndStop("acik");
}
video_button.addEventListener(MouseEvent.CLICK, videodurdur);
function videodurdur(e:MouseEvent):void {
if(video_oynattir.state == "playing") {
video_oynattir.stop();
}else{
video_oynattir.play();
}
}
video_oynattir.addEventListener(Event.READY, readyHandler);
function readyHandler(e:VideoEvent):void {
trace("at");
}
//Accesss of possibly undefined property READY through a refferance with static type class
closeButton.addEventListener(MouseEvent.CLICK, closeRichMedia);
function closeRichMedia(e:MouseEvent):void {
video_oynattir.stop();
ExternalInterface.call("dopushunlock");
gotoAndPlay(3);
openButton.visible = true;
}
openButton.addEventListener(MouseEvent.CLICK, openRichMedia);
function openRichMedia(e:MouseEvent):void {
ExternalInterface.call("dopushlock");
gotoAndStop(2);
openButton.visible = false;
}
clickButton.addEventListener(MouseEvent.CLICK, gotoLink);
function gotoLink(e:MouseEvent):void {
navigateToURL(new URLRequest(clickTAG), "_blank");
}
stop();
Please try to change the event type the EventListener listens to to "VideoEvent":
video_oynattir.addEventListener(VideoEvent.READY, readyHandler);
Make sure to import "fl.video.VideoEvent".

Adding a symbol from the library to the stage using code

I'm new to coding and unfortunately my teacher is more programmer than teacher so he is very vague on how to do things. I'm aiming for something simple I have a symbol from my library dragged and dropped directly on the stage as my Background and in the code for the background object I'm trying to add a small arrow for the menu selector that you move with the mouse keys. I know its something simple I'm not understanding so if anyone can help that would be great!
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class BG extends MovieClip
{
public var select:Select = new Select ;
public function BG()
{
// constructor code
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
addChild(select);
select.x = 200;
select.y = 200;
}
private function addedToStage(ev:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(ev:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
select.y = 250;
}
if (event.keyCode == Keyboard.UP)
{
select.y = 200;
}
}
private function removedFromStage(ev:Event):void
{
removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
}
}
When you test your movie, you should be getting error messages like:
BG.as, Line 34 1120: Access of undefined property event.
The errors can give you a hint at what is going wrong. In this case, it's telling you that there is no property named "event". In your keyDownHandler function, you need to replace "event" with "ev" which is what you are naming your KeyboardEvent in the function declaration. So this should make your code work:
if (ev.keyCode == Keyboard.DOWN)
{
select.y = 250;
}
if (ev.keyCode == Keyboard.UP)
{
select.y = 200;
}
Also, you should use parenthesis when creating object instances. Even though it still compiles, you should be writing:
public var select:Select = new Select();

Intercepting EventListener ActionScript

I'm trying to extend the Button class and remove the default EventListener through the following:
removeEventListener(MouseEvent.CLICK, clickHandler);
And then add something like this:
protected function _clickHandler(event:MouseEvent):void{
Alert.show("Are you sure about this operation", "Alert", 3, this, execute);
function execute(e:CloseEvent):void{
if(e.detail == Alert.YES)
super.clickHandler(event);
}
}
This way, I'll have a default component which will trigger a message alert with YES or NO option and prevent me from having to write that on every button that trigger the server. Unfortunatelly, it doesn't work that way.
Tried removing the default function and adding the one I wrote on listener;
Tried overriding clickHandler directly, also doesn't work;
Edit: Here is what I want: When the users click a button that will make a service call in my application, I always pop a window for him to tell me if he's sure of that. What I wanted was to build an automatic component for that, like this:
package screen{
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import mx.controls.Alert;
import mx.controls.Button;
import mx.events.CloseEvent;
public class CommonButton extends Button{
public function CommonButton(){
super();
//removeEventListener(MouseEvent.CLICK, clickHandler)
//addEventListener(MouseEvent.CLICK, clickHandler);
addEventListener(KeyboardEvent.KEY_DOWN, function (e:KeyboardEvent):void{
if(e.altKey == Keyboard.ENTER)
dispatchEvent(new MouseEvent(MouseEvent.CLICK));
});
}
private var _saveEvent:MouseEvent;
override protected function clickHandler(event:MouseEvent):void{
_saveEvent = event;
event.stopImmediatePropagation();
Alert.show("Are you sure about this operation", "Alert", 3, this, execute);
}
private function execute(e:CloseEvent):void{
if(e.detail == Alert.YES)
super.clickHandler(_saveEvent);
}
}
}
And then:
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function test():void{
//if the user clicked No, this method will never be called.
Alert.show("You clicked YES");
}
]]>
</mx:Script>
<screen:CommonButton click="test()" />
Final Edit with Solution:
package screen{
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import mx.controls.Alert;
import mx.controls.Button;
import mx.events.CloseEvent;
public class CommonButton extends Button{
public function CommonButton(){
super();
addEventListener(KeyboardEvent.KEY_DOWN, function (e:KeyboardEvent):void{
if(e.altKey == Keyboard.ENTER)
dispatchEvent(new MouseEvent(MouseEvent.CLICK));
});
}
private var _stopProp:Boolean = true;
override protected function clickHandler(event:MouseEvent):void{
if(_stopProp){
event.stopImmediatePropagation()
Alert.show("Are you sure about this operation", "Alert", 3, this, execute);
}else
_stopProp = true;
}
private function execute(e:CloseEvent):void{
if(e.detail == Alert.YES){
_stopProp = false;
dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
}
}
}
Why even bother with this? Just add your custom event handler to the extended component and allow the normal event handler to fire regardless. You can have multiple handlers registered to the same event. The only reason I could see this being an issue is if you were releasing this as a library and wanted to prevent the user from attach a click handler themselves, but you're not. You don't need to remove or stop the default click handler at all here.
EDIT: Here is an example of what the asker wants to happen here.
public class AButton extends Button
{
private var stopProp:Boolean = true;
public function AButton()
{
super();
this.addEventListener(MouseEvent.CLICK,this.clickHandlers);
}
private function clickHandlers( e:MouseEvent ):void{
if ( stopProp ) {
e.stopImmediatePropagation();
trace( "clicked in button" );
Alert.show( "Continue with event", "Warning!", Alert.YES|Alert.NO, null, this.closeHandler );
}
}
private function closeHandler( e:CloseEvent ):void {
if ( e.detail == Alert.YES ) {
this.stopProp = false;
this.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
this.stopProp = true;
}
}
}
That will stop the event from going any further if the stopProp property is true. Then it will prompt the user. If the user selects "Yes", it will set stopProp to false, re-dispatch a click event from the button, and reset stopProp to false for next time. If they select "No", nothing happens.
I made a quick prototype to test this and it definitely worked.
Use stopImmediatePropagation() method of the Event class.
protected function _clickHandler(event:MouseEvent):void{
event.stopImmediatePropagation();
Alert.show("Are you sure about this operation", "Alert", 3, this, execute);
function execute(e:CloseEvent):void{
if(e.detail == Alert.YES)
super.clickHandler(event);
}
}

How to prevent Event.Resize to trigger twice on displayState change to FULL_SCREEN?

I have an application with an fullscreen button, when that button is clicked I change the displayState of stage to StageDisplayState.FULL_SCREEN_INTERACTIVE.
That makes Event.RESIZE fire twice if stage.scaleMode = StageScaleMode.NO_SCALE.
The event is only fireing once if i change back to stage.displayState = StageDisplayState.NORMAL.
Anyone know a good way to prevent the Event.RESIZE from fireing twice except calling the onResize function directly or implementing a custom event?
Sample code that reproduces the issue:
package test
{
import flash.display.Sprite;
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public function Main():void
{
if (stage)
this.init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(Event.RESIZE, onResize);
stage.scaleMode = StageScaleMode.NO_SCALE;
var button:Sprite = new Sprite();
button.mouseEnabled = true;
button.addEventListener(MouseEvent.CLICK, doResize);
button.graphics.lineStyle(3,0x00ff00);
button.graphics.beginFill(0x0000FF);
button.graphics.drawRect(10, 10, 100, 100);
button.graphics.endFill();
this.addChild(button);
}
private function doResize(e:MouseEvent) : void {
if (stage.displayState == StageDisplayState.NORMAL) {
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
else {
stage.displayState = StageDisplayState.NORMAL;
}
}
private function onResize(e:Event) : void {
trace("onResize", stage.displayState);
}
}
}
Further investigation shows that it seems like switching to fullscreen is two steps, first one resize to a StageDisplayState.NORMAL and then to StageDisplayState.FULL_SCREEN_INTERACTIVE.
It seems like this is a bug limited to the debug player.
Debug version of Flash Player 11.1 reproduces this bug, while the Chrome version 11.2 and browser plugin 11.1 does not.
Use a boolean. For example, make it true in the resize function and check if it's not true to execute it.

how to add a limited amount of objects to the stage

So I have a box exported as Box in my library. I have tried :
package {
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
private var _box:Box=new Box ;
private var boxAmount:Number=0;
private var boxLimit:Number=16;
private var _root:Object;
public function Main() {
addEventListener(Event.ENTER_FRAME, eFrame);
addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
}
private function eFrame(event:Event):void {
if (boxAmount <= boxLimit) {
boxAmount++;
_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);
} else if (boxAmount >= boxLimit) {
removeEventListener(Event.ENTER_FRAME, eFrame);
} else {
addEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
}
But it did not work as planned.
What I am trying to do is make my box stay on the screen at a random place on the stage and remove it when clicked (but that will come later). This code is for some reason adding the object to the stage and then removing it and adding it again up to 16 times.
Thanks
I it seems like you have created one _box, and re-add it to the timeline on enter frame. It should work if you create a new box instance inside the eFrame function rather than before it, then you keep reassigning to the same variable name, rather than reusing the one object eg:
package {
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
private var boxAmount:Number=0;
private var boxLimit:Number=16;
private var _root:Object;
public function Main() {
addEventListener(Event.ENTER_FRAME, eFrame);
addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
}
private function eFrame(event:Event):void {
if (boxAmount<=boxLimit) {
boxAmount++;
var _box:Box=new Box ;
_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);
} else if (boxAmount >= boxLimit) {
removeEventListener(Event.ENTER_FRAME, eFrame);
} else {
addEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
}
In your code you are only ever creating one box. Your enterFrame handler is just assigning it a new random position 16 times. If you want 16 boxes you'll need to create a new box each time in the enterFrame function.
But you don't need to use the ENTER_FRAME event here. You could just use a for loop or a while loop to create the 16 boxes.
Here's some code:
package {
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
private var boxAmount:Number=0;
private var boxLimit:Number=16;
public function Main() {
addBoxes();
}
private function addBoxes():void {
while (boxAmount<=boxLimit) {
boxAmount++;
var box:Box = new Box();
box.y=Math.random()*stage.stageHeight;
box.x=Math.random()*stage.stageWidth;
addChild(box);
// listen for mouse clicks
box.addEventListener(MouseEvent.CLICK, onBoxClick);
}
}
private function onBoxClick(e:MouseEvent):void {
var clickedBox:Box = e.target as Box;
removeChild(clickedBox);
}
}
}
I removed your enterFrame handler and just made a function called addBoxes. I'm using a while loop to crate the boxes. Notice that each time through the loop I'm creating a NEW box, not just reusing the old one. I'm also adding a mouse click event listener to each box so it can be removed from the stage when clicked.
You'll surely want to change some of this to get it to work for your purposes, but it should get you headed in the right direction.
What you have at the moment is just repositioning the same box over and over because you only ever create one Box instance. You need to create multiple instances of Box and add them to the stage individually.
package {
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
private var boxAmount:Number=0;
private var boxLimit:Number=16;
private var _root:Object;
public function Main() {
addEventListener(Event.ENTER_FRAME, eFrame);
addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
}
private function eFrame(event:Event):void {
if (boxAmount<=boxLimit) {
boxAmount++;
//create a new box instance
var _box:Box = new Box();
_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);
} else {
removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
}
Although the variable boxAmount suggests otherwise, you said you only want one box. So, to do this, you just need to move the following lines into the constructor (Main).
_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);
Then remove or disable the enter frame event. You don't need it in this case. To check if the box got clicked, attach the listener to the box itself, not to it's parent:
_box.addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
if (boxAmount<=boxLimit) {
// ...
} else if (boxAmount >= boxLimit) {
// ...
} else {
// ...
}
This part looks really strange. The first condition covers a case that is also covered by the second condition, together they already cover all possible cases. boxAmount is either less or equals to boxLimits or it is greater than it. Checking for equality twice is confusing. There is no need to include the last else statement. It actually has the same behaviour as the following code.
if (boxAmount<=boxLimit) {
// ...
} else if (boxAmount > boxLimit) {
// ...
}