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.
Related
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.
I have the problem with Actionscript 3.0 in Adobe Flash. I can't run "gotoAndStop" from a class (not document class).
With the help of the Internet I tried several things, but none of them worked:
1)
MovieClip(root).gotoAndStop(3);
2)
package
{
import flash.display.MovieClip;
public class CustomClassName extends MovieClip
{
public static var mainTimeline:MovieClip;
public function CustomClassName()
{
// constructor code
}
}
}
3)
public class np extends SimpleButton {
var _root:MovieClip;
public function np() {
this.addEventListener(Event.ADDED_TO_STAGE,init);
this.addEventListener(MouseEvent.CLICK,nextF);
}
private function init(e:Event):void{
_root = MovieClip(this.root);
}
private function nextF(e:MouseEvent):void{
_root.addEventListener(Event.RENDER,renderF);
stage.invalidate();
_root.nextScene();
}
private function renderF(e:Event):void {
_root.gotoAndStop(5);
}
}
I have these imports:
import flash.display.MovieClip;
import flash.display.Graphics;
import flash.display.Stage;
import flash.events.Event;
And if I run these lines of code:
trace('frame:',currentFrame);
super(this).gotoAndPlay(2);
trace('frame:',currentFrame);
... I get 0 as currentFrame as a result.
I have a class where I want to run gotoAndStop(2).
And in my .fla file I have these in the first frame:
stop();
import Buzzer.*;
var buzzerClip:Buzzer = new Buzzer();
stage.addChild(buzzerClip);
But the code doesn't run the gotoAndStop function. And actually no error will be returned. Does someone has another idea?
The property root is null until the display object has been added to the display list.
So to adjust your first attempt:
public function MyDisplayObject()
{
init();
}
private function init():void
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler()
{
MovieClip(root).gotoAndStop(3);
}
Can someone tell me why I get this error?
ReferenceError: Error #1069: Property roll_mc not found on com.usmanzubairi.theAges.TheAges and there is no default value.
at com.usmanzubairi.theAges::PirGame/rolling()
package com.usmanzubairi.theAges
{
import flash.display.MovieClip;
import flash.events.*;
import flash.media.*;
public class TheAges extends MovieClip
{
private var game:PirGame;
private var game2:PreGame;
private var game3:SupGame;
public function TheAges()
{
stage.addEventListener(MouseEvent.CLICK, startGame);
}
private function startGame(event:Event):void
{
if (event.target != player_btn)
{
removeEventListener(MouseEvent.CLICK, startGame);
game = new PirGame();
addChild(game);
}
else
{
addChild(player_mc);
player_mc.visible = true;
player_mc.play();
}
if (event.target == player_mc.tom_mc)
{
removeEventListener(MouseEvent.CLICK, startGame);
game2 = new PreGame();
addChild(game2);
}
if (event.target == player_mc.pete_mc)
{
removeEventListener(MouseEvent.CLICK, startGame);
game = new PirGame();
addChild(game);
}
if(event.target == player_mc.sam_mc)
{
removeEventListener(MouseEvent.CLICK, startGame);
game3 = new SupGame();
addChild(game3);
}
}
public function gameOver():void
{
removeChild(game);
game = null;
stage.addEventListener(MouseEvent.CLICK, startGame);
}
}
}
Here's the PirGame document class code:
package com.usmanzubairi.theAges
{
import flash.utils.Timer;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.net.SharedObject;
public class PirGame extends MovieClip
{
public function PirGame()
{
addEventListener(MouseEvent.CLICK,rolling);
}
private function rolling (event:Event):void
{
if (event.target == MovieClip(root).roll_mc)
{
addChild(roll)
roll.visible = true;
runner_mc.visible = false;
roll.play();
}
}
}
}
Thanks.
If your roll_mc is in your PirGame symbol, and i think Pirgame class is well associated with the symbol, try modify your PirGame class a s follow:
package com.usmanzubairi.theAges
{
import flash.utils.Timer;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.net.SharedObject;
public class PirGame extends MovieClip
{
public function PirGame()
{
addEventListener(MouseEvent.CLICK,rolling);
}
private function rolling (event:Event):void
{
trace( event.target, roll_mc );
// change MovieClip(root).roll_mc to this.roll_mc as roll_mc is on this symbol and not on the root.
if( event.target == this.roll_mc )
{
trace( 'roll_mc clicked' );
addChild( roll )
roll.visible = true;
runner_mc.visible = false;
roll.play();
}
}
}
}
if you just want the roll_mc be clickable try the following:
package com.usmanzubairi.theAges
{
import flash.utils.Timer;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.net.SharedObject;
public class PirGame extends MovieClip
{
public function PirGame()
{
if( roll_mc ) roll_mc.addEventListener( MouseEvent.CLICK, rolling );
else addEventListener( Event.ADDED_TO_STAGE, _onStage );
}
private function _onStage( e:Event ):void
{
removeEventListener( Event.ADDED_TO_STAGE, _onStage );
roll_mc.addEventListener( MouseEvent.CLICK, rolling );
}
private function rolling( e:Event ):void
{
trace( 'roll_mc clicked' );
addChild( roll )
roll.visible = true;
runner_mc.visible = false;
roll.play();
}
}
}
if you don't have to re display the runner_mc you can remove it from the display list instead of make it not visible:
removeChild( runner_mc );
I have this AS3 script that worked so far...
stop();
b1.addEventListener(MouseEvent.CLICK, Info001);
function Info001(event:MouseEvent):void {
gotoAndPlay(2);
}
X.addEventListener(MouseEvent.CLICK, Exit001);
function Exit001(e:MouseEvent) {
NativeApplication.nativeApplication.exit();
}
then, I added a class in the .fla file...
package
{
import flash.system.System;
import flash.system.Capabilities;
import flash.display.Sprite;
import flash.events.Event;
import flash.desktop.NativeApplication;
import flash.utils.setTimeout;
import com.hdi.nativeExtensions.NativeAds;
import com.hdi.nativeExtensions.NativeAdsEvent;
public class Main extends Sprite
{
public var na : NativeApplication;
private var admobId:String = 'a1514b5ef85e336';
public function Main()
{
na = NativeApplication.nativeApplication;
na.addEventListener('exiting',exit,false,0,true);
na.addEventListener('deactivate',exit,false,0,true);
if ( stage ){
stage.scaleMode = 'noScale';
stage.align = 'TL';
}
if ( loaderInfo ){
loaderInfo.addEventListener( Event.INIT, init, false, 0, true );
} else {
init(null);
}
}
(the class is not complete here...)
and the buttons stopped working... :-/
I tried adding
import flash.events.MouseEvent;
but that's not enough... how can you make it work?
If you're trying to run gotoAndPlay in Main, it will throw that error because Sprites don't have a timeline. If so, try extending MovieClip instead.
I'm new to Actionscript 3 and I'm wanting to allow a circle to move down using the down arrow on the keyboard. Here's my code:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y = y+=speed;
}
}
}
}
}
When I test it, nothing happens when I press the down key. Anyone know what's wrong with the code?
Try adding KeyBoard events to the stage instead of to the class. Additionally, I would not nest functions like that, bad practice in general. Also the line y = y+=speed; is confusing, shouldn't it just be y += speed; ?
EDIT: Sorry, I guess stage will be null in the constructor, I've added a ADDED event listener.
Try this:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(Event.ADDED, onAdded);
}
private function onAdded(event:Event) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
}
private function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y += speed;
}
}
}
}