I am trying to make a fullscreen program in air that does not become windowed when you press escape. Why is my program not working as it should, and how should make it work correct?
Code:
package
{
import flash.display.*;
import flash.events.*;
import flash.desktop.*;
import flash.text.*;
public class Main extends Sprite
{
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandeler);
}
private function keyDownHandeler(e:KeyboardEvent):void
{
if (e.keyCode == 27)
{
trace("Hello");
e.preventDefault();
}
}
}
}
I created a new project (FlashDevelop) targeting AIR 14 (also successfully tried AIR 3.9) with the following document class file:
package
{
import flash.desktop.NativeApplication;
import flash.display.NativeWindow;
import flash.display.NativeWindowInitOptions;
import flash.display.NativeWindowRenderMode;
import flash.display.NativeWindowSystemChrome;
import flash.display.Sprite;
import flash.display.StageDisplayState;
import flash.events.Event;
import flash.events.FullScreenEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
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
{
if(e) removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyUpHandler);
var content:Sprite = new Sprite();
content.graphics.beginFill(0xFFFF00);
content.graphics.drawRect(0, 0, 400, 500);
content.graphics.endFill();
addChild(content);
this.addEventListener(MouseEvent.CLICK, fullScreen);
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
private function fullScreen(e:Event):void {
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
protected function keyUpHandler(event:KeyboardEvent):void {
switch(event.keyCode) {
case Keyboard.ESCAPE:
trace("ESCAPE");
event.preventDefault();
break;
default:
trace("UNHANDLED KEY: ", event.keyCode);
}
}
}
}
It worked as expected. When hitting escape, the preventDefault() method on the key event successfully kept the application in full-screen.
Notes:
It has to be on the key down. Key up had no effect. The result was the same with the key down listener on the stage or the nativeApplication.
Related
I am trying to do this by clicking on button using 'A' key in keyboard. I created two frames for this button but the code doesn't work, although there is no error.
Do I need to put anything in my main class? Can anyone help to fix this?
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class controlButton extends MovieClip {
public function controlButton() {
// constructor code
this.addEventListener(KeyboardEvent.KEY_DOWN,clickDown);
this.addEventListener(KeyboardEvent.KEY_UP,clickUp);
}
public function clickDown(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(2);
}
}
public function clickUp(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(1);
}
}
public function changelabel(newLabel:String):void{
this.label.text = newLabel;
}
}
}
Your button will never receive any KeyboardEvent. You should add your event listeners directly to the stage. Of course, you have to obtain a link to the stage. Anyways:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class controlButton extends MovieClip {
public function controlButton() {
// constructor code
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage (e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
//stage is no longer null here
stage.addEventListener(KeyboardEvent.KEY_DOWN,clickDown);
stage.addEventListener(KeyboardEvent.KEY_UP,clickUp);
}
public function clickDown(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(2);
}
}
public function clickUp(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(1);
}
}
public function changelabel(newLabel:String):void{
this.label.text = newLabel;
}
}
}
As you can see, you should add KeyboardEvent listeners to the stage right after the Event.ADDED_TO_STAGE fires.
Function X1 simply does not run- no trace result and programme so basic.
Board.as is called- I checked.
Simple sprite display not working.
Main.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.*;
import flash.text.TextField;
import flash.ui.Keyboard;
import Start;
import Board;
/**
* ...
* #author Michael
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init():void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var Board1:Sprite = new Board();
stage.addChild(Board1);
Board1.visible = true;
var Start1:Sprite = new Start();
Start1.x = 32;
Start1.y = 32;
addChild (Start1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode ==Keyboard.SPACE)
{
removeChild(Start1);
Start1 = null;
}
}
}
Board.as
package
{
import flash.display.Bitmap;
import flash.display.Graphics;
import flash.display.Sprite;
/**
* ...
* #author Michael
*/
public class Board extends Sprite
{
[Embed(source="../lib/Board.jpg")]
private var BoardClass :Class
public function X1():void
{
var boardclass:Bitmap = new BoardClass () as Bitmap;
trace("Project is running fine!");
this.addChild(boardclass);
}
}
}
You are not calling the function X1(). Change the code part in your Main.as class where you create the Board to this:
var Board1:Sprite = new Board();
Board1.X1();
stage.addChild(Board1);
A few more tips for your code:
1) You don't need Board1.visible = true;, it's visible by default
2) Change the name of Board1 to board1 or just board. It's a standard to call classes with first capital letter.
EDIT:
If you want X1() to be run as you create the object, call this function in the constructor of Board.as. Constructor is a function that is run when you create an object. For Board.as it would be like this:
public function Board():void
{
X1(); // this function will be called when you create a new Board object
}
I did the following
package classes
{
// imports removed
public class Main extends MovieClip {
// vars removed
public function Main():void {
trace('--> Main Function started ...');
/**************************************
Preloader
**************************************/
var preLoader:PreLoader = new PreLoader(); // init
stage.addChild(preLoader); // to Stage
stage.loaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoader.preloaderProgress); // calling the function
doThis();
}
}
With the PreLoader Class looking like this:
package classes {
// imports removed
public class PreLoader extends MovieClip {
public var totalBytes:uint;
public var loadedBytes:uint;
public function PreLoader() {
trace('--> PRELOADER Started ...');
this.addEventListener(Event.ADDED_TO_STAGE, addedHandler);
this.loaderBar.scaleX = 0;
}
private function addedHandler(e:Event):void {
trace('Adding Preloader ...');
totalBytes = this.stage.loaderInfo.bytesTotal;
trace('PL: Total ' + totalBytes + ' / ' + loadedBytes);
this.removeEventListener(Event.ADDED_TO_STAGE, addedHandler);
preloaderProgress(e);
}
public function preloaderProgress(e:Event):void {
trace('Progress...');
loadedBytes = this.stage.loaderInfo.bytesLoaded;
this.loaderBar.scaleX = loadedBytes / totalBytes;
this.loaderBar.alpha = 1 - this.loaderBar.scaleX;
if (totalBytes == loadedBytes) {
trace('Removing PreLoader ...');
this.parent.removeChild(this);
}
}
}
}
The Problem: The Preloader is not displayed BEFORE the inital SWF-file has finished loading. Even all the tracing outputs start when the main movie is finished - i did some profiling on 'slow connections'.
If you wonder: doThis() is loading data from a xml-file, from here everything is fine, tracing outputs are at the right time (but too late at all :D)
Thank you in advance!
Edit: Maybe the question is: Is there a way to determine when the main movie is starting to load?
Make sure that you don't have any content other than the preloader on stage in the first frame. Otherwise all that content will have to load before the preloader starts up.
If you are working in FlashIDE you have to put your preloader into the first frame. If you prefer to use another IDE you should use Frame metatag. Example:
Main.as
package com.sample.preloader
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author Author
*/
[Frame(factoryClass="com.sample.preloader.Preloader")]
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
}
}
}
Preloader.as
package com.sample.preloader
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;
/**
* ...
* #author Author
*/
public class Preloader extends MovieClip
{
public function Preloader()
{
if (stage) {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO show loader
}
private function ioError(e:IOErrorEvent):void
{
trace(e.text);
}
private function progress(e:ProgressEvent):void
{
// TODO update loader
}
private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
stop();
loadingFinished();
}
}
private function loadingFinished():void
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO hide loader
startup();
}
private function startup():void
{
var mainClass:Class = getDefinitionByName("com.sample.preloader.Main") as Class;
addChild(new mainClass() as DisplayObject);
}
}
}
I want to use the Preloader provided by FlashDevelop but it does not react as it should.
My loader tells me 100% when no file has been downloaded.
The code should display a trace() containing the percent intermediary but does not
Could you help me?
Mains.as
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
[Frame(factoryClass="Preloader")]
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
var imgRequest:URLRequest = new URLRequest("http://next-web.be/actionscript/0.jpg");
var img:Loader = new Loader();
img.load(imgRequest);
addChild(img);
}
}
}
Preloader.as
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;
import flash.display.Sprite;
public class Preloader extends MovieClip
{
private var bar:Sprite;
public function Preloader()
{
if (stage) {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO show loader
bar = new Sprite();
bar.graphics.lineStyle(1, 0x4444ff, 1, true);
bar.graphics.drawRect(0, 0, 100, 6);
bar.x = stage.stageWidth / 2 - bar.width / 2;
bar.y = stage.stageHeight / 2 - bar.height / 2;
addChild(bar);
}
private function ioError(e:IOErrorEvent):void
{
trace(e.text);
}
private function progress(e:ProgressEvent):void
{
// TODO update loader
bar.graphics.lineStyle(0, 0, 0);
bar.graphics.beginFill(0x8888ff);
bar.graphics.drawRect(1, 1, (e.bytesLoaded / e.bytesTotal) * 98 , 4);
bar.graphics.endFill();
trace( "loading:" + (e.bytesLoaded / e.bytesTotal) * 100 );
}
private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
stop();
loadingFinished();
}
}
private function loadingFinished():void
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO hide loader
removeChild(bar);
bar = null;
startup();
//stop();
}
private function startup():void
{
var mainClass:Class = getDefinitionByName("Main") as Class;
addChild(new mainClass() as DisplayObject);
}
}
}
You need to register your listener on the contentLoaderInfo property of the Loader you use to download the data (in your case img.contentLoaderInfo).
In your code, you register progress on loaderInfo, which is a field of your Preloader class (inherited from MovieCLip), and will give you the progress on loading the SWF that contains the Preloader class.
public class Preloader extends MovieClip
{
public function Preloader()
{
// This is wrong.
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
// Function `progress` will show the progress of loading your SWF file,
// *not* the JPEG you're loading in class Main.
}
}
You need to register your listener on the contentLoaderInfo property of the Loader you use to download the data (in your case img.contentLoaderInfo).
img.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
Of course, then it will show only the progress of loading the JPEG file.
You'll also need to either pass the Loader object (or just the contentLoaderLinfo) to the Preloader somehow, or include the event handler in your Main class.
After a continued search, I realized my mistake.
Until now, I always use "loader" but this function does not include my pictures to swf file. So I use a library swc allowing me to generate a complete swf.
Here's a very simple AS3 project. The stage is not null in the main class, but it is in the AppMan class, and that's where I want to access it. Why?
Here's my main class, called StageText.as:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
stage.scaleMode = StageScaleMode.NO_SCALE; //here, the stage is not null.
stage.align = StageAlign.TOP_LEFT;
public class StageText extends Sprite
{
private var appMan:AppMan = new AppMan();
public function StageText()
{
appMan.startApp();
}
}
}
Then, in the same folder I've got the AppMan.as class.
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
public class AppMan extends Sprite
{
public var textField:TextField;
// Application Width, Height
public var appW:Number;
public var appH:Number;
public function AppMan()
{
super();
}
public function startApp():void {
// create textfield
textField = new TextField();
textField.wordWrap = true;
textField.width = 540;
textField.height = 400;
textField.text = "Hello World";
addChild(textField);
//if I try to run init in response to Event.ADDED_TO_STAGE, it never runs
this.addEventListener(Event.ADDED_TO_STAGE, init);
//Or, if I run init() without the eventListener, I get a runtime error
//indicating that the stage is null
//init();
}
private function init(e:Event):void {
//private function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.HIGH;
appW = stage.stageWidth;
appH = stage.stageHeight;
}
}
}
I'm guessing, but is the appMan instance ever added to the stage?
public function StageText()
{
this.AddChild(appMan);
appMan.startApp();
}