EventDispatcher and Function Issues - actionscript-3

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

Related

Toggling Full Screen view on Key press in Flash via Actionscript3

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

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

Error 1009 : Cannot access a property of method of a null object reference

I don't understand what is going on
This is my Main.as
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var pirkles:Circles = new Circles()
public function Main() {
gotoAndStop(1)
playbtn.addEventListener(MouseEvent.CLICK, playscreen)
}
public function playscreen(event:MouseEvent):void {
gotoAndStop(2)
addChild(pirkles)
}
}
}
And this is my Circles.as
package {
import flash.display.MovieClip
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
import flash.events.MouseEvent;
public class Circles extends MovieClip{
public function Circles():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
this.y = 175
this.x = 10
}
public function MOVE(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
this.x = this.x+10
}
else if (event.keyCode == Keyboard.LEFT) {
this.x = this.x-10
}
else if (event.keyCode == Keyboard.UP) {
this.y = this.y-10
}
else if (event.keyCode == Keyboard.DOWN) {
this.y = this.y+10
}
}
}
}
Now I get an error that says there is a problem at line 11 of my Circles.as and at line 8 of my Main.as. However, at those lines I don't understand what is causing the problem. I added a event listener at line 11, but when I take it out it works. Also, at line 8, I Just defined a variable.
you can't access the stage in a class constructor.
So the line
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
is causing the error.
If you need to access the stage, add a listener in the constructor for the ADDED_TO_STAGE event, and in the callback function you will be able to access the stage
So:
public function Circles():void {
this.addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
this.y = 175
this.x = 10
}
private function onAddedToStage (evt:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
}

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

Flash as3 class wont call ADDED_TO_STAGE event when using addChild()

I don't know why, but for some reason I can't get my InputEngine class to listen for ADDED_TO_STAGE.
package Input{
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
public class InputEngine extends Sprite{
public function InputEngine() {
addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
leftPressed = false;
rightPressed = false;
upPressed = false;
downPressed = false;
}
public function stageAddHandler(e:Event)
{
trace("worke");
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyIsPressed, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyIsReleased, false, 0, true);
}
public function keyIsPressed(e:KeyboardEvent)
{
switch(e.keyCode)
{
case Keyboard.LEFT : leftPressed = true; break;
case Keyboard.RIGHT : rightPressed = true; break;
case Keyboard.UP : upPressed = true; break;
case Keyboard.DOWN : downPressed = true; break;
}
}
public function keyIsReleased(e:KeyboardEvent)
{
switch(e.keyCode)
{
case Keyboard.LEFT : leftPressed = false; break;
case Keyboard.RIGHT : rightPressed = false; break;
case Keyboard.UP : upPressed = false; break;
case Keyboard.DOWN : downPressed = false; break;
}
}
public var leftPressed:Boolean = new Boolean;
public var rightPressed:Boolean = new Boolean;
public var upPressed:Boolean = new Boolean;
public var downPressed:Boolean = new Boolean;
}
}
This is the main game file:
package {
import Input.InputEngine;
import flash.display.*;
import flash.events.*;
public class System extends Sprite{
public function System() {
trace("System created");
addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addChild(inputEngine);
// constructor code
}
public function gameLoop(e:Event)
{
if(inputEngine.leftPressed == true)
{
trace("Left pressed");
}
}
public var inputEngine:InputEngine = new InputEngine();
}
}
for some reason I cannot get InputEngine to initialize the keyboard listeners. What am I doing wrong here?
Your problem based on your comments of the error is that your instance of the System class has not been added to the display list. Without seeing all your code, it's hard to help you.
You didn't mention there was a runtime error in your question. But if we assume that you actually have an instance of inputEngine and things are happening as you 'expect' them to, the problem is that your instance of System class has not been added to the stage. Therefore... inputEngine is never attached to the display list of the stage.