Several errors "Access of undefined property" - actionscript-3

So I have this pretty basic code in my document class:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.*;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.MovieClip;
public class Main extends Sprite
{
//Properties
public var circle:Circle;
public var vx:Number;
public var vy:Number;
addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
addEventListener(Event.ENTER_FRAME, onEnter);
public function addedToStageHandler(event:Event):void
{
}
public function Main()
{
super();
init();
}
public function init():void
{
vx = 0;
vy = 0;
circle = new Circle(35, 0x0066FF);
stage.addChild(circle);
circle.x = 50;
circle.y = 50;
}
public function onKeyboardDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
vx = -5;
break;
case Keyboard.RIGHT:
vx = 5;
break;
case Keyboard.UP:
vy = -5;
break;
case Keyboard.DOWN:
vy = 5;
break;
}
}
public function onKeyboardUp(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
vx = 0;
break;
case Keyboard.RIGHT:
vx = 0;
break;
case Keyboard.UP:
vy = 0;
break;
case Keyboard.DOWN:
vy = 0;
break;
}
}
public function onEnter(event:Event):void
{
circle.x += vx;
circle.y += vy;
}
}
}
The problem is that I keep getting errors that to a beginner don't make any sense:
"Call to a possibly undefined method addEventListener." x 3
"Access of undefined property onEnter."
"Access of undefined property onKeyboardUp."
"Access of undefined property onKeyboardDown."
I really don't understand this issue. How can AS3 not recognize addEventListener? As well, I did have it so my event listeners were added to the stage "stage.addEventListener" and it wasn't recognizing the stage either. Can somebody push me in the right direction with this issue? Thanks!

It's logic because you'll have to place the eventListeners inside the ´init´ method or Class constructor.
public function init():void
{
addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
addEventListener(Event.ENTER_FRAME, onEnter);
vx = 0;
vy = 0;
circle = new Circle(35, 0x0066FF);
stage.addChild(circle);
circle.x = 50;
circle.y = 50;
}
If not the listeners are placed outside the class scope, and therefor not recognized.
Good luck!

All in all your code is almost there you just need a little bit better understanding on how the display list works.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.*;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.MovieClip;
public class Main extends Sprite
{
//Properties
public var circle:Circle;
public var vx:Number;
public var vy:Number;
// we can not do function calls like this in the class declaration area
// so we move these listeners to a function
// addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
// addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
// addEventListener(Event.ENTER_FRAME, onEnter);
public function Main()
{
super();
this.init();
}
public function init():void
{
// the "this" keyword means we are scoping it to this class instance
this.addEventListener( EVENT.ADDEDTOSTAGE, addedToStageHandler)
// using "this" is good practice and will help make your code more readable
this.vx = 0;
this.vy = 0;
this.circle = new Circle(35, 0x0066FF);
stage.addChild(circle);
this.circle.x = 50;
this.circle.y = 50;
}
public function addedToStageHandler(event:Event):void
{
// doing addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
// will set the scope for this listener to this class
// you want to target the stage. And since we are waiting for ADDEDTOSTAGE
// to trigger we know we are on the stage.
// the only time we can access stage is if we are on the display list.
// clean up the listener since we do not need it anymore
this.removeEventListener( EVENT.ADDEDTOSTAGE, addedToStageHandler)
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}
public function onKeyboardDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
this.vx = -5;
break;
case Keyboard.RIGHT:
this.vx = 5;
break;
case Keyboard.UP:
this.vy = -5;
break;
case Keyboard.DOWN:
this.vy = 5;
break;
}
}
public function onKeyboardUp(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
this.vx = 0;
break;
case Keyboard.RIGHT:
this.vx = 0;
break;
case Keyboard.UP:
this.vy = 0;
break;
case Keyboard.DOWN:
this.vy = 0;
break;
}
}
public function onEnter(event:Event):void
{
this.circle.x += this.vx;
this.circle.y += this.vy;
}
}
}

Related

How can make my game support touch screen?

I have done a wheel game from a tutorial. The problem is the game works perfectly with a mouse but does not work on touch screens. I don't know how to manipulate it to transform the game.
How can I adapt the game to function with touch screens?
package
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
public final class Main extends Sprite
{
private var speed:Number = 0;
private var paddles:Vector.<Sprite> = new Vector.<Sprite>();
private var line:Shape;
private var lastPaddle:String;
public final function Main():void
{
paddles.push(wheel.p1, wheel.p2, wheel.p3, wheel.p4, wheel.p5, wheel.p6, wheel.p7, wheel.p8, wheel.p9, wheel.p10);
listeners('add');
}
private final function listeners(action:String):void
{
if(action == 'add')
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.addEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
else
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.removeEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
}
private final function startDraw(e:MouseEvent):void
{
line = new Shape();
addChild(line);
line.graphics.moveTo(mouseX, mouseY);
line.graphics.lineStyle(8, 0x000000, 0.3);
stage.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
}
private final function drawLine(e:MouseEvent):void
{
line.graphics.lineTo(mouseX, mouseY);
}
private final function spinWheel(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawLine);
listeners('rm');
speed = line.height * 0.1;
removeChild(line);
line = null;
stage.addEventListener(Event.ENTER_FRAME, spin);
}
private final function spin(e:Event):void
{
/* Rotate Wheel */
wheel.rotationZ += speed;
/* Detect Value */
for(var i:int = 0; i < 10; i++)
{
if(indicator.hArea.hitTestObject(paddles[i]))
{
lastPaddle = paddles[i].name;
}
}
/* Decrease speed */
speed -= 0.1;
/* Remove lIstener and reset speed when wheel stops */
if(speed <= 0)
{
stage.removeEventListener(Event.ENTER_FRAME, spin);
speed = 10;
run(lastPaddle);
listeners('add');
}
}
function run(action:String):void
{
switch(action)
{
case 'p1':
myText.text = "text 10";
break;
case 'p2':
myText.text = "text 25";
break;
case 'p3':
myText.text = "text 20";
break;
case 'p4':
myText.text = "text 50";
break;
case 'p5':
myText.text = "text 30";
break;
case 'p6':
myText.text = "text 75";
break;
case 'p7':
myText.text = "text 40";
break;
case 'p8':
myText.text = "text 100";
break;
case 'p9':
myText.text = "text 50";
break;
case 'p10':
myText.text = "text 125";
break;
}
}
}
}
first of all set in stage :
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT
after that
just replace MouseEvent to TouchEvent and replace MOUSE_DOWN to TOUCH_BEGIN and
MOUSE_UP to TOUCH_END. after that you must change MouseEvent in event handler to TouchEvent
and if you have using MOUSE_MOVE chenge that to TOUCH_MOVE
private final function listeners(action:String):void
{
if(action == 'add')
{
stage.addEventListener(TouchEvent.TOUCH_BEGIN, startDraw);
stage.addEventListener(TouchEvent.TOUCH_END, spinWheel);
}
else
{
stage.removeEventListener(TouchEvent.TOUCH_END, startDraw);
stage.removeEventListener(TouchEvent.TOUCH_BEGIN, spinWheel);
}
}

AS3 How to remove objects from the stage from various classes

So I'm Creating a game that generates different types of fish. I have the main class initilize everything then the fish are generated with another class. I then have another class listen for when they are added to the stage that controls their movement. After the fish leaves the scene I have gotten them to be deleted but I cannot get them all to be deleted once the fish is clicked. I have a listener that gets when a fish is clicked but I am having trouble deleting all the other generated fish so that I can load the fish information.
I probably have tons of problems but here is my Main class:
package
{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import Crappie;
/*import Bass;
import Bluegill;
import LongGar;
import Muskellunge;
import Perch;
import PumpkinSeed;
import ShortGar;
import SpotGar;*/
import GenerateFish;
public class FishGame extends MovieClip
{
private var randomFish:Number;
public function FishGame() //First function to be called
{
startMenu();
}
public function startMenu() //Loads the start menu
{
gotoAndStop("Start Menu");
StartButton.addEventListener(MouseEvent.CLICK, gotoStartGame);
}
private function gotoStartGame(evt:MouseEvent) //links to the start of th game
{
StartButton.removeEventListener(MouseEvent.CLICK, gotoStartGame);
gotoAndStop("Game");
game();
}
public function game() /generates fish
{
var genFish:GenerateFish = new GenerateFish();
this.addChild(genFish);
var genFish2:GenerateFish = new GenerateFish();
this.addChild(genFish2);
}
//This is where I need help. All the fish are children of other class. I tried deleting them in their own class but it seems like it would work best if it was in this class but I'm having trouble calling this class.
public function removeFish()
{
trace("Here");
}
}
}
Here is the generating fish class:
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.display.*;
import Crappie;
//import Bass;
//import Bluegill;
//import LongGar;
//import Muskellunge;
//import Perch;
//import PumpkinSeed;
//import ShortGar;
//import SpotGar;
import FishGame;
public class GenerateFish extends MovieClip {
private var randomFish:Number;
public function GenerateFish() //When this is added via addchild it adds a fish
{
this.addEventListener(Event.ADDED_TO_STAGE, addFish, false, 0, true);
//addFish();
}
private function addFish(e:Event) //adds fish to scrence
{
this.removeEventListener(Event.ADDED_TO_STAGE, addFish);
randomFish = Math.floor(Math.random() * 2);
randomFish = 0; //for testing purposes
switch(randomFish)
{
case 0:
var crappie:Crappie = new Crappie();
this.addChild(crappie); //Calls the crappy funciton
break;
case 1:
var longgar:LongGar = new LongGar();
this.addChild(longgar);
break;
case 2:
var bluegill:Bluegill = new Bluegill();
this.addChild(bluegill);
break;
case 3:
var spotgar:SpotGar = new SpotGar();
this.addChild(spotgar);
break;
case 4:
var muskellunge:Muskellunge = new Muskellunge();
this.addChild(muskellunge);
break;
case 5:
var pumpkinseed:PumpkinSeed = new PumpkinSeed();
this.addChild(pumpkinseed);
break;
case 6:
var bass:Bass = new Bass();
this.addChild(bass);
break;
case 7:
var perch:Perch = new Perch();
this.addChild(perch);
break;
case 8:
var pike:Pike = new Pike();
this.addChild(pike);
break;
default:
break;
}
}
}
}
And here is one of the specific fish classes:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.*;
import FishGame;
import GenerateFish;
public class Crappie extends MovieClip
{
private var Speed:Number;
private var randomBoolean:Boolean = (Math.random() > .5) ? true : false;
private var fishOnScreen:Boolean = false;
//public var remFish:FishGame = new FishGame();
public function Crappie() //Adds event listeners to control the fish
{
this.addEventListener(Event.ADDED_TO_STAGE, Setup, false, 0, true);
this.addEventListener(MouseEvent.CLICK, GoToFishInfo);
fishOnScreen =true;
}
public function GoToFishInfo(evt:MouseEvent):void //When fish is clicked Info pops up. It is here i want every fish to be deleted.
{
this.removeEventListener(Event.ADDED_TO_STAGE, Setup);
/*while (MovieClip(root).numChildren >= 1) //What I tried to delete all objects
{
MovieClip(root).removeChildAt(1);
}*/
var remFish:FishGame = new FishGame(); //Creates The SWF file file:///FishGame/classes/FishGame.swf contains invalid data.
remFish.removeFish();
MovieClip(root).gotoAndStop("FishInfo", "Scene 1");
var crappieinfo:CrappieInfo = new CrappieInfo();
stage.addChild(crappieinfo);
crappieinfo.x = 512;
crappieinfo.y = 384;
crappieinfo.alpha = 100;
}
private function Setup(e:Event) //Setup the fish position and adds info for movement
{
var randomBoolean:Boolean = (Math.random() > .5) ? true : false;
//true forwards
//false is backwards
if (randomBoolean)
{
this.x = 1030;
}
else
{
this.scaleX = -1;
this.x = -10;
}
this.y = this.GetRandomYPosition();
this.alpha = 100;
this.addEventListener(Event.ENTER_FRAME, MoveCircle);
Speed = GetRandomSpeed();
if (randomBoolean)
{
Speed *= -1
}
}
private function GetRandomSpeed():Number
{
return (Math.floor(Math.random() * 5) +5);
}
private function GetRandomYPosition():Number
{
//
//basic formula: Math.floor(Math.random()*(1+High-Low))+Low;
//
return (Math.floor(Math.random() * (650-this.height)) + 230);
}
public function MoveCircle(e:Event) //Moves the fish
{
if (fishOnScreen)
{
this.x += Speed;
if (this.x >1024 || this.x < -10 || this.y >768 || this.y < 200)
{
var genExtraFish:GenerateFish = new GenerateFish();
stage.addChild(genExtraFish);
this.parent.removeChild(this);
fishOnScreen = false;
this.removeEventListener(MouseEvent.CLICK, GoToFishInfo);
}
}
}
}
}
Thanks I'd appreciate all the help I can get!
////////
//Edit//
////////
I have modified my main class to package
{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import BaseFish;
import GenerateFish;
public class FishGame extends MovieClip
{
private var randomFish:Number;
private var basefish:BaseFish = new BaseFish();
public function FishGame()
{
startMenu();
}
public function startMenu()
{
gotoAndStop("Start Menu");
StartButton.addEventListener(MouseEvent.CLICK, gotoStartGame);
}
private function gotoStartGame(evt:MouseEvent)
{
StartButton.removeEventListener(MouseEvent.CLICK, gotoStartGame);
gotoAndStop("Game");
basefish.StartFish();
}
}
}
And I Created the basefish class as so:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.*;
import FishGame;
public class BaseFish extends MovieClip
{
public var FishOnScreen:Array = new Array();
public var FishSpeed:Array = new Array();
private var Speed:Number;
private var randomBoolean:Boolean = (Math.random() > .5) ? true : false;
public var NumFish:int = -1;
private var randomFish:Number;
public function BaseFish()
{
}
public function StartFish()
{
addFish(5);
this.addEventListener(Event.ENTER_FRAME, MoveCircle);
}
public function addFish(NumAdd:Number)
{
for (var i:Number = 0; i < NumAdd; i++)
{
randomFish = Math.floor(Math.random() * 2);
randomFish = 0;
switch(randomFish)
{
case 0:
var crappie:Crappie = new Crappie();
this.addChild(crappie);
crappie.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(crappie);
trace(FishOnScreen[1]);
Setup(crappie);
NumFish += 1;
break;
case 1:
var longgar:LongGar = new LongGar();
this.addChild(longgar);
longgar.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(longgar);
Setup(longgar);
NumFish += 1;
break;
case 2:
var bluegill:Bluegill = new Bluegill();
this.addChild(bluegill);
bluegill.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(bluegill);
Setup(bluegill);
NumFish += 1;
break;
case 3:
var spotgar:SpotGar = new SpotGar();
this.addChild(spotgar);
spotgar.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(spotgar);
Setup(spotgar);
NumFish += 1;
break;
case 4:
var muskellunge:Muskellunge = new Muskellunge();
this.addChild(muskellunge);
muskellunge.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(muskellunge);
Setup(muskellunge);
NumFish += 1;
break;
case 5:
var pumpkinseed:Pumpkinseed = new Pumpkinseed();
this.addChild(pumpkinseed);
pumpkinseed.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(pumpkinseed);
Setup(pumpkinseed);
NumFish += 1;
break;
case 6:
var bass:Bass = new Bass();
this.addChild(bass);
bass.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(bass);
Setup(bass);
NumFish += 1;
break;
case 7:
var perch:Perch = new Perch();
this.addChild(perch);
perch.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(perch);
Setup(perch);
NumFish += 1;
break;
case 8:
var pike:Pike = new Pike();
this.addChild(pike);
pike.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(pike);
Setup(pike);
NumFish += 1;
break;
default:
this.addChild(crappie);
crappie.addEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.push(crappie);
Setup(crappie);
NumFish += 1;
break;
}
}
}
private function Setup(mc: MovieClip)
{
var randomBoolean:Boolean = (Math.random() > .5) ? true : false;
//true forwards
//false is backwards
if (randomBoolean)
{
MovieClip(mc).x = 1030;
}
else
{
MovieClip(mc).scaleX = -1;
MovieClip(mc).x = -10;
}
MovieClip(mc).y = GetRandomYPosition();
MovieClip(mc).alpha = 100;
FishSpeed[NumFish] = GetRandomSpeed();
if (randomBoolean)
{
FishSpeed[NumFish] *= -1
}
}
private function GetRandomSpeed():Number
{
return (Math.floor(Math.random() * 5) +5);
}
private function GetRandomYPosition():Number
{
//
//basic formula: Math.floor(Math.random()*(1+High-Low))+Low;
//
return (Math.floor(Math.random() * (650-this.height)) + 230);
}
public function MoveCircle(e:Event)
{
//trace(FishOnScreen.length);
for (var i:Number = 0; i < FishOnScreen.length; i++)
{
if (FishOnScreen[i]!=null)
{
MovieClip(FishOnScreen[i]).x += FishSpeed[i];
if (MovieClip(FishOnScreen[i]).x >1024 || MovieClip(FishOnScreen[i]).x < -10 || MovieClip(FishOnScreen[i]).y >768 || MovieClip(FishOnScreen[i]).y < 200)
{
addFish(1);
this.removeChild(MovieClip(FishOnScreen[i]));
MovieClip(FishOnScreen[i]).removeEventListener(MouseEvent.CLICK, GoToFishInfo);
FishOnScreen.splice(i, 1);
FishSpeed.splice(i,1);
}
}
//else trace("null");
}
}
public function GoToFishInfo(evt:MouseEvent):void
{
dispose();
MovieClip(root).gotoAndStop("FishInfo", "Scene 1");
var crappieinfo:CrappieInfo = new CrappieInfo();
stage.addChild(crappieinfo);
crappieinfo.x = 512;
crappieinfo.y = 384;
crappieinfo.alpha = 100;
}
public function dispose():void
{
for (var i : int = this.numChildren-1 ; i >= 0 ; i--)
{
if(this.getChildAt(i) is MovieClip)
{
this.removeChildAt(i);
MovieClip(FishOnScreen[i]).removeEventListener(Event.ADDED_TO_STAGE, Setup);
MovieClip(FishOnScreen[i]).removeEventListener(MouseEvent.CLICK, GoToFishInfo);
}
}
}
}
}
It doesn't give me any errors but it doesn't display any fish. If you could take a look at my updated code I would be grateful!
To make things better and avoid duplication of code, extend all your classes such as Crappie, Bass etc from a Base Class, call it BaseFish for example. You should create a dispose() method in this base class. That way this dispose method will be automatically available to all classes extending it. In fact, you can use this method to write common code at one place and avoid duplication.
Also, if you feel it suits the need, extend GenerateFish also from the base class, or create a similar method called dispose() in this class.
Then when you call your removeFish() method, you can call these dispose methods for your individual classes.
Create the method like this (this is just to get you started):
public function dispose():void
{
for (var i : int = this.numChildren-1 ; i >= 0 ; i--)
{
if(this.getChildAt(i) is MovieClip)
{
this.removeChildAt(i);
}
}
}
This code removes every child which is a movie clip. You may also want to remove any Event Listeners which are added in your class inside this method.
Then, move your variables genFish and genFish2 to class variables, and not declare them inside your method game()
Now in your removeFish() method do this:
public function removeFish()
{
genFish.dispose();
genFish2.dispose();
}
Follow the same logic inside your GenerateFish class to dispose all the children as they will now be instances of the same base class.
Another thing, which could be very important from a performance perspective is the ENTER_FRAME event that you are adding. Notice that this event is being added to each instance that you create inside GenerateFish. So, every instance will have its own enter frame. Maybe it will work ok in your current situation as the number of instances may be less, but this approach is not scalable, and tomorrow if you have many instances, it will certainly hinder performance.
Ideally, you should try to have only one ENTER_FRAME event listener in your main class and use the handler to change things inside your child classes.
Hope this helps you.

Can't remove object from stage

I have a problem. I am trying to make a copter game in Adobe Flash with ActionScript 3.0, but now the game works, but the obstacles can't be removed from stage. The obstacles are still going miles out of the stage. How can I remove the obstacles?? and same problem if you are game over, if you are game over the event end, but the last spawned obstacles you see still and aren't removed. And how can I make the obstacles go faster after a period of time??
There are standing some dutch words in it, such as 'hoeSpelen' that is for instructions text en 'af' is for the gameover text and 'tijd' = time and 'balkje' = obstacles.
I hope you can help me.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.getTimer;
import flash.events.Event;
public class iCopter extends MovieClip
{
private var copter : Copter = null;
private var gameover : GameOver = null;
private var balkje : Balkje = null;
private var tijd : int
var score = 0;
var highscore = 0;
public function onStartButton(event:MouseEvent)
{
startiCopter()
}
public function iCopter()
{
startButton.addEventListener(MouseEvent.CLICK, onStartButton);
af.visible = false
output.visible = false
hscore.visible = false
}
public function startiCopter()
{
removeChild(startButton);
removeChild(hoeSpelen);
removeChild(af);
score = 0
icopterlogo.visible = false
output.visible = true
hscore.visible = true
copter = new Copter();
copter.x = 100;
copter.y = 200;
addChild(copter);
tijd = getTimer();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event)
{
var now:int = getTimer();
if (now - tijd > 1250)
{
var balkje = new Balkje();
balkje.x = 350;
balkje.y = Math.random() * 150;
addChild (balkje);
tijd = now
score = score + 10;
output.text = "score: "+score;
if (balkje.x <= -10) //don't work.
{ //don't work.
removeChild (balkje); //don't work.
} //don't work.
}
addEventListener(Event.ENTER_FRAME, botsing);
}
function botsing (event:Event)
{
for (var i = 0; i < numChildren; i++)
{
if (getChildAt(i) is Balkje || getChildAt(i) is Vloer)
{
var b = getChildAt(i) as MovieClip;
if (b.hitTestObject(copter))
{
removeChild (copter);
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
var gameover = new GameOver();
addChild(af);
af.visible = true
addChild(hoeSpelen);
addChild(startButton);
if (score > highscore)
{
highscore = score
hscore.text = "highscore: "+highscore;
}
}
}
}
}
}
}
Here are the scripts for the copter and obstacle
copter:
muisKlik = mouseClick
muisDruk = mousePush
muisOmhoog = mouseUp
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class Copter extends MovieClip
{
var vy : Number = 0;
var muisKlik : Boolean = false;
public function Copter()
{
vy = 5;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(event:Event)
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, muisDruk);
stage.addEventListener(MouseEvent.MOUSE_UP, muisOmhoog);
}
public function onEnterFrame(event:Event)
{
if (muisKlik == true)
{
y -= vy;
}
else
{
y += vy;
}
}
public function muisDruk (event:MouseEvent)
{
muisKlik = true
}
public function muisOmhoog (event:MouseEvent)
{
muisKlik = false
}
}
}
obstacle:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Balkje extends MovieClip
{
var vx : Number = 1;
public function Balkje()
{
vx = 5;
addEventListener( Event.ENTER_FRAME, onEnterFrame );
}
public function onEnterFrame( event:Event )
{
x -= vx;
}
}
}
Not tested - probably full of errors, and I haven't done AS3 in a while:
When you initialize obstacles, pass in the stage object (not sure if this is the best practice)
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Balkje extends MovieClip
{
var vx : Number = 1;
public function Balkje()
{
vx = 5;
if(!stage){
//if stage isn't populated yet, wait for it
this.addEventListner(Event.ADDED_TO_STAGE,addedToStage);
}else{
init();
}
}
private function addedToStage(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
init();
}
protected function init():void {
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame( event:Event )
{
x -= vx;
//check x
if(this.x + this.width <= 0 || this.x >= stage.stageWidth) {
if(this.parent) this.parent.remmoveChild(this);
removeEventListener( Event.ENTER_FRAME, onEnterFrame );
}
}
}
}
Again, not tested (is stage.stageWidth correct to get the stage width?) but you get the idea. Check the x of the object, if it is outside the visible stage remove it from its parent (I just put this.parent because you didn't add the obstacle to the stage).
Also, why do you add event listener to ENTER_FRAME in a function called by ENTER_FRAME?

MovieClip timeline animations wont play across classes - Flash Game

First question. Please be gentle.
I’m making a simple (I'd hoped) keyboard controlled movieclip of a game character. The Movieclip has five frames, each with a simple animation inside for when the player presses the arrow keys, walking up, down, stable etc.
I made a simple prototype in the main application class file and it works PERFECTLY. I also traced currentFrame and totalFrames and that is correct too. When key is pressed it plays correct frame and it says the totalFrames are 5. Great!
However I want to make a multi level game, so have used the application class as a level switcher and each level and object is its own class. During this migration the MovieClip stops working. It is added to the stage fine and can be moved with the arrow keys but the individual animations wont play. It just flickers through each of the five keyframes.
Also when I trace it, it says currentFrame is 0 (?) and totalFrames is 1 (?)
It wont be controlled by .stop or .gotoAndPlay either.
Please help!
Here is the single class version that works fine.
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
[SWF(width="650", height="450",backgroundColor="#FFFFFF", frameRate="60")]
public class CharacterMovieclip extends MovieClip
{
//Create and initialize the vx and vy variables
public var vx:int = 0;
public var vy:int = 0;
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
public var Character:Class;
public var character:MovieClip = new Character();
public function CharacterMovieclip()
{
//Add Character
stage.addChild(character);
character.x = 200;
character.y = 100;
character.gotoAndStop(1);
//Add the event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
character.gotoAndStop(3);
}
}
public function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
vx = 0;
character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
vy = 0;
character.gotoAndStop(1);
}
}
public function enterFrameHandler(event:Event):void
{
//Move the player
character.x += vx
character.y += vy;
trace(character.currentFrame);
trace(character.totalFrames);
trace(character.x);
}
}
}
But that all goes to hell once I try it across a few classes. Here is the character class.
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
public class Character extends MovieClip
{
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
private var CharacterImage:Class;
//Private properties
private var _characterImage:DisplayObject = new CharacterImage();
private var _character:MovieClip = new MovieClip();
//Public properties
public var vx:int = 0;
public var vy:int = 0;
public function Character()
{
_character.addChild(_characterImage);
this.addChild(_character);
}
}
}
The character object is then added to the LevelOne class
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class LevelOne extends Sprite
{
//Declare the variables to hold
//the game objects
private var _character:Character;
private var _background:Background;
//A variable to store the reference
//to the stage from the application class
private var _stage:Object;
public function LevelOne(stage:Object)
{
_stage = stage;
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(event:Event):void
{
startGame();
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function startGame():void
{
//Create Game Objects
_character = new Character();
_background = new Background();
//Add them to stage
this.addChild(_background);
_background.x = 0;
_background.y = 0;
this.addChild(_character);
_character.x = 300;
_character.y = 50;
_character.gotoAndStop(1);
//Event listeners
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
//Move the game character and check its stage boundaries
_character.x += _character.vx;
_character.y += _character.vy;
checkStageBoundaries(_character);
trace(_character.currentFrame);
trace(_character.totalFrames);
trace(_character.x);
}
private function checkStageBoundaries(gameObject:MovieClip):void
{
if (gameObject.x < 50)
{
gameObject.x = 50;
}
if (gameObject.y < 50)
{
gameObject.y = 50;
}
if (gameObject.x + gameObject.width > _stage.stageWidth - 50)
{
gameObject.x = _stage.stageWidth - gameObject.width - 50;
}
if (gameObject.y + gameObject.height > _stage.stageHeight - 50)
{
gameObject.y = _stage.stageHeight - gameObject.height - 50;
}
}
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_character.vx = -5;
_character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
_character.vx = 5;
_character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
_character.vy = -5;
_character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
_character.vy = 5;
_character.gotoAndStop(3);
}
}
private function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
_character.vx = 0;
_character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
_character.vy = 0;
_character.gotoAndStop(1);
}
}
}
}
And levelOne is added to the stage by the Main application class.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
[SWF(width="650", height="450",
backgroundColor="#FFFFFF", frameRate="60")]
public class Main extends Sprite
{
private var _levelOne:LevelOne;
public function Main()
{
_levelOne = new LevelOne(stage);
stage.addChild(_levelOne);
}
}
}
Somewhere in all this my MovieClip stops playing the individual animations. Its on the stage, can be moved around with arrow keys, but wont play the keyframe animations. (And trace seems to think its playing frame 0, and it only has 1 frame too.)
Any help would be hugely appreciated. My head hurts.
The problem is in your new Character class.
Let's see on the first class that works perfectly, you embed symbol="Character" from the characterRes.swf file and it's your 5 frame movie clip.
Now see on the second class, that is broken: you do the same embedding and now your 5 frame movie clip is _characterImage rather than Character class, that you create. It's right decision from the OOP point of view to hide implementation (the direct MovieClip from the library or custom class) inside the same API (in both cases Character is outside a simple MovicClip) and there can be two solutions for the problem:
First - try to embed symbol for the hole class and remove your current code it's not needed here:
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
public class Character extends MovieClip
{
//Public properties
public var vx:int = 0;
public var vy:int = 0;
public function Character()
{
}
}
Second: make you Character class wrapper for the Character from swf - pass all MovieClip API from it to the _character MovieClip:
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
public class Character extends MovieClip
{
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
private var _characterClass:Class;
//Private properties
private var _character:MovieClip = new _characterClass();
//Public properties
public var vx:int = 0;
public var vy:int = 0;
public function Character()
{
addChild(_character)
}
override public function gotoAndPlay(frame:Object, scene:String=null):void
{
_character.gotoAndPlay(frame, scene);
}
override public function gotoAndStop(frame:Object, scene:String=null):void
{
_character.gotoAndStop(frame, scene);
}
override public function play():void
{
_character.play();
}
override public function stop():void
{
_character.stop();
}
//and other methods and getters for currentFrame, totalFrames
}
}
The first method is more simple, the second one is more flexible, because it's separate graphics and logic - so you in future you will be able for instance pass this link in Character Constructor and load assets dynamically or use Character class as animation controller for the different MovieClips from the loaded swfs.

Actionscript 3 Making the character to Jump

I am making a platformer game. But I am having issue because whenever I pressed the spacebar to jump, the character will stuck in the mid-air. However, I can resolved the problem by holding spacebar and the character will land.
The issue is at mainJump() located inside Boy class.
I seen many people solved the problem by using action timeline, but my main problem is, are there anyway I can solve the problem by using an external class?
Main class
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.*;
public class experimentingMain extends MovieClip
{
var count:Number = 0;
var myTimer:Timer = new Timer(10,count);
var classBoy:Boy;
//var activateGravity:gravity = new gravity();
var leftKey, rightKey, spaceKey, stopAnimation:Boolean;
public function experimentingMain()
{
myTimer.addEventListener(TimerEvent.TIMER, scoreUp);
myTimer.start();
classBoy = new Boy();
addChild(classBoy);
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressTheDamnKey);
stage.addEventListener(KeyboardEvent.KEY_UP, liftTheDamnKey);
}
public function pressTheDamnKey(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftKey = true;
stopAnimation = false;
}
if (event.keyCode == 39)
{
rightKey = true;
stopAnimation = false;
}
if (event.keyCode == 32)
{
spaceKey = true;
stopAnimation = true;
}
}
public function liftTheDamnKey(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftKey = false;
stopAnimation = true;
}
if (event.keyCode == 39)
{
rightKey = false;
stopAnimation = true;
}
if (event.keyCode == 32)
{
spaceKey = false;
stopAnimation = true;
}
}
public function scoreUp(event:TimerEvent):void
{
scoreSystem.text = String("Score : "+myTimer.currentCount);
}
}
}
Boy class
package
{
import flash.display.*;
import flash.events.*;
public class Boy extends MovieClip
{
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 5;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 40;
//the current speed of the jump;
var jumpSpeed:Number = 0;
var theCharacter:MovieClip;
var currentX,currentY:int;
public function Boy()
{
this.x = 600;
this.y = 540;
addEventListener(Event.ENTER_FRAME, boyMove);
}
public function boyMove(event:Event):void
{
currentX = this.x;
currentY = this.y;
if (MovieClip(parent).leftKey)
{
currentX += mainSpeed;
MovieClip(this).scaleX = 1;
}
if (MovieClip(parent).rightKey)
{
currentX -= mainSpeed;
MovieClip(this).scaleX = -1;
}
if (MovieClip(parent).spaceKey)
{
mainJump();
}
this.x = currentX;
this.y = currentY;
}
public function mainJump():void
{
currentY = this.y;
if (! mainJumping)
{
mainJumping = true;
jumpSpeed = jumpSpeedLimit * -1;
currentY += jumpSpeed;
}
else
{
if (jumpSpeed < 0)
{
jumpSpeed *= 1 - jumpSpeedLimit / 250;
if (jumpSpeed > -jumpSpeedLimit/12)
{
jumpSpeed *= -2;
}
}
}
if (jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit)
{
jumpSpeed *= 1 + jumpSpeedLimit / 120;
}
currentY += jumpSpeed;
if (currentY >= stage.stageHeight - MovieClip(this).height)
{
mainJumping = false;
currentY = stage.stageHeight - MovieClip(this).height;
}
}
}
}
First of all, formalize your code, eliminating sassy things like 'pressTheDamnKey,' which doesn't even describe the function very well because a function cannot press a key. That is an event handler and should be named either keyDownHandler or onKeyDown, nothing else.
Secondly, you rarely want to do any actual work in event handlers beyond the immediate concerns of the event data. Instead call out to the function which does the actual work. A handler handles the event, then calls the code which does the work. This separates out concerns nicely for when you want something else to be able to also make the little boy animate besides the enterFrameHandler, like perhaps a mouse.
I can imagine your trace log is getting filled up pretty quickly with "Score" lines since your timer is firing 100 times a second (10 milliseconds per). I would change that to not fire on a timer, but to be refreshed when the score actually changes.
The problem with the jumping, aside from spaghetti code, is that you are basing his movements upon whether the key is pressed or not by saving the state of the key press in a variable and having him continually inspect it. This is bad for a couple of reasons: 1. he should not need to reach out to his environment for information, it should be given to him by whatever object owns him or by objects that are responsible for telling him and 2. It requires you to continually hold down the spacebar or he will stop moving, since he checks to see if it is being held down (see problem 1).
I will address all these issues below, leaving out the scoring, which is another matter altogether.
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
// Sprite is preferred if you are not using the timeline
public class Application extends Sprite
{
private var boy:Boy;
public function Application()
{
boy = new Boy();
addChild(boy);
boy.x = 600; // set these here, not in the boy
boy.y = 540;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler );
}
public function keyDownHandler(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case 32: boy.jump();
break;
case 37: boy.moveLeft();
break;
case 39: boy.moveRight();
break;
default:
// ignored
break;
}
}
public function keyUpHandler(event:KeyboardEvent):void
{
switch(event.keyCode)
{
// ignored for jumping (32)
case 37: // fall through
case 39: boy.stop();
break;
default:
// ignored
break;
}
}
}//class
}//package
package
{
import flash.display.*;
import flash.events.*;
// It is assumed that there is an asset in the library
// that is typed to a Boy, thus it will be loaded onto
// the stage by the owner
public class Boy extends Sprite
{
private var horzSpeed :Number = 0;
private var vertSpeed :Number = 0;
private var floorHeight :Number;
private var jumpHeight :Number;
private var amJumping :Boolean = false;
public function Boy()
{
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function moveLeft():void
{
horzSpeed = -1;
}
public function moveRight():void
{
horzSpeed = 1;
}
public function stop():void
{
horzSpeed = 0;
}
public function jump():void
{
if (amJumping) return;
floorHeight = y;
jumpHeight = floorHeight + 20;
vertSpeed = 2;
amJumping = true;
animateJump();
}
private function enterFrameHandler(event:Event):void
{
animate();
}
private function animate():void
{
x += horzSpeed;
if( amJumping )
{
animateJump();
}
}
// Doing a simple version for this example.
// If you want an easier task of jumping with gravity,
// I recommend you employ Greensock's superb
// TweenLite tweening library.
private function animateJump():void
{
y += vertSpeed;
if( y >= jumpHeight )
{
y = jumpHeight;
vertSpeed = -2;
}
else if( y <= floorHeight )
{
y = floorHeight;
amJumping = false;
}
}
}//class
}//package
Another way to approach this, and probably the better way long-term, is for the boy to not even be responsible for moving himself. Instead, you would handle that in the parent, his owner or some special Animator class that is responsible for animating things on schedule. In this even more encapsulated paradigm, the boy is only responsible for updating his own internal look based upon the outside world telling him what is happening to him. He would no longer handle jumping internally, but instead would be responsible for doing things like animating things he owns, like his arms and legs.
You've got a mainJumping variable that is only true while the jump is running. Why not just use that?
if (MovieClip(parent).spaceKey || mainJumping)
{
mainJump();
}