Toggling Full Screen view on Key press in Flash via Actionscript3 - actionscript-3

Below is my code aimed at getting my flash content to toggle between full screen view and normal view on press of the spacebar key, so far no compiler errors yet does not work.
import flash.events.KeyboardEvent;
stage.addEventListener (KeyboardEvent.KEY_DOWN, toggleScreenview);
function toggleScreenview(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){
stage.displayState = StageDisplayState.FULL_SCREEN;
} else {
stage.displayState = StageDisplayState.NORMAL;
}
}

use StageDisplayState.FULL_SCREEN_INTERACTIVE,this example work with space key, if full screen go to normal and if normal go to full screen
your solution is:
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, toggleScreenview);
function toggleScreenview(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.SPACE)
{
if(stage.displayState == StageDisplayState.NORMAL)
{
stage.displayState=StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
else
{
stage.displayState=StageDisplayState.NORMAL;
}
}
}
Good Luck

Related

AS3: Exit full screen event listener

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);

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".

EventDispatcher and Function Issues

I am very new to AS3 and I am trying to make a simple flash game prototype. Right now, all I am trying to do is get flash to tell me it is actually receiving the key input of the user, but I have run into the following 2 issues:
On my lines where I am adding event listeners to the Stage, I am getting an Error1061: Call to Possibly Undefined method addEventListener through a reference with static type Class.
My research has led me to believe that this is because my Backlayer class does not extend EventDispatcher, but I cannot extend that because Backlayer must extend MovieClip
On those same lines, I am trying to tell the code that when such an event occurs, to perform the named function, but I get an error1120 telling me that both are undefined properties.
I think this may because the class is not extending event dispatcher yet?
My understanding of AS3 is all self-taught and I am still trying to learn the etiquette of the language, so I apologize if this is a really simple question, but I haven't been able to find an answer just from googling that works yet.
Here is my code:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.events.EventDispatcher;
public class Backlayer extends MovieClip
{
Stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
Stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
public function keyDownHandler(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.LEFT)
{
trace("left pressed");
}
else if(e.keyCode == Keyboard.RIGHT)
{
trace("right pressed");
}
else if(e.keyCode == Keyboard.UP)
{
trace("up pressed");
}
else if(e.keyCode == Keyboard.DOWN)
{
trace("down pressed");
}
}
public function keyUpHandler(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.LEFT)
{
trace("left released");
}
else if(e.keyCode == Keyboard.RIGHT)
{
trace("right released");
}
else if(e.keyCode == Keyboard.UP)
{
trace("up released");
}
else if(e.keyCode == Keyboard.DOWN)
{
trace("down released");
}
}
}
}
MovieClip does inherit from EventDispatcher, thus your class should as well.
See: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html
Following rows have two problems:
Stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
Stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
1) They are not in body of a function.
2) Stage.addEventListener means that you're trying to call static function of Stage class. It should be written stage.addEventListener (calling function of instance of Stage class stored in property stage).

Space bar and arrow keys on Radio buttons trigger TypeError: Error #1009

I've just discovered a bug in my app, it looks like a Flash player bug, i would like to know if anyone has found a workaround or something.
I've some radio buttons groups in my app. If I press the arrow keys while holding the spacebar pressed, it ends up triggering
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.controls::LabelButton/drawIcon()
at fl.controls::LabelButton/draw()
at fl.controls::RadioButton/draw()
at fl.core::UIComponent/callLaterDispatcher()
If found this thread that describes my same situation.
Do you think there's any workaround for this? Is it really a flash bug?
So, I've investigated this problem and found, that there is an error in standard fl components. It is not only about RadioButton. If you hold SpaceBar and then switch current UI element by clicking "TAB" button - you will receive similar error.
To solve it, you should fix source of the component: in class LabelButton, method keyUpHandler replace code
setMouseState(oldMouseState);
with
if (oldMouseState!==null) {
setMouseState(oldMouseState);
}
and add override:
override protected function focusOutHandler(event:FocusEvent):void {
if (oldMouseState) {
keyUpHandler(new KeyboardEvent(KeyboardEvent.KEY_UP, true, true, 0, Keyboard.SPACE));
}
super.focusOutHandler(event);
}
If you don't know, how you can do it:
just copy Adobe Flash CS5 install folder\Common\Configuration\Component Source\ActionScript 3.0\User Interface\fl\controls\LabelButton.as to another directory with the same package structure \fl\controls\LabelButton.as
then you need include this duplicated structure to project.
It is not very good way, but in this case it really fixes this error. BTW, I'll send it to Adobe.
actually its not a flash bug its component's bug. if space bar is no need for your radio buttons(i think its useless) you can disable it with creating your own MyRadioButton which extends RadioButton class this will be disable space key input
package {
import flash.events.KeyboardEvent;
import fl.controls.RadioButton;
import flash.ui.Keyboard;
public class MyRadioButton extends RadioButton{
public function MyRadioButton() {
super();
}
override protected function keyUpHandler(event:KeyboardEvent):void {
if(event.keyCode != Keyboard.SPACE){
super.keyUpHandler(event);
}
}
override protected function keyDownHandler(event:KeyboardEvent):void {
if(event.keyCode != Keyboard.SPACE){
super.keyDownHandler(event);
}
}
}
}
just you need to change class property fl.controls.RadioButton to MyRadioButton from your library element:"RadioButton". (i assume MyRadioButton is near fla)
------>edit
here is another solution without disabling spacebar. this time when user makes anykeydown before spacebar keyup code runs spacebar keyup before anykeydown. and focus get removed from radio button also you can add anyother solutions like if spacebar keystate is down donot let any keydown.
package {
import flash.events.KeyboardEvent;
import fl.controls.RadioButton;
import flash.ui.Keyboard;
import flash.events.Event;
public class MyRadioButton extends RadioButton{
private var _isSpaceDown:Boolean = false;
public function MyRadioButton() {
super();
}
override protected function handleChange(event:Event):void {
if (_isSpaceDown) {
keyUpHandler(new KeyboardEvent(KeyboardEvent.KEY_UP, true, true, 0, Keyboard.SPACE));
setMouseState('up');
}
super.handleChange(event);
}
override protected function keyUpHandler(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.SPACE){
if(_isSpaceDown){
_isSpaceDown = false;
}else{
return;
}
}
super.keyUpHandler(event);
}
override protected function keyDownHandler(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.SPACE){
_isSpaceDown = true;
}else{
if(_isSpaceDown){
var e:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN);
e.keyCode = Keyboard.SPACE;
super.keyUpHandler(e);
_isSpaceDown = false;
}
}
super.keyDownHandler(event);
}
}
}

FLEX - Disable spacebar

I've got an application, which is tabbed (using TabNavigator).
When it starts, I switch tab to nr.2 (default is tab 1). I need to be able to hold down spacebar and drag the mouse to pan, but when I do this it switches back to the first tab. So the spacebar is triggering it to switch.
I've tried using a custom lass that extends tabNavigator, like the code below, but it's not working. Also tried setting focusEnabled = false without luck.
Any idea how I would solve this?
Thanks a lot in advance,
Stian Berg Larsen
package components
{
import mx.containers.TabNavigator;
import flash.events.KeyboardEvent;
public class myTabNavigator extends TabNavigator
{
public function myTabNavigator()
{
super();
}
protected override function keyDownHandler(e : KeyboardEvent) : void {
if (e.keyCode == 32) { // Spacebar
return;
}
super.keyDownHandler(e);
}
}
}
That is probably caused because you are not stopping your event from bubbling.
Try this:
protected override function keyDownHandler(e : KeyboardEvent) : void {
if (e.keyCode == 32) { // Spacebar
e.preventDefault();
e.stopImmediatePropagation();
return;
}
super.keyDownHandler(e);
}