i'm new in flash and as3 programming, and this is my first project. i found error on my project like this
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at src.char::Enemy/Remove()
at src.screen::Gameplay/Collision()
at src.screen::Gameplay/Routine()
I think the error occurs because there is no function Remove () on the gameplay, but I'm not sure that's true. here's the enemy.as
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Enemy extends MovieClip {
private var timer:Timer = new Timer(25);
public function Enemy(xPos:Number, yPos:Number) {
x = xPos;
y = yPos;
timer.addEventListener(TimerEvent.TIMER, MoveDown);
timer.start();
}
private function MoveDown(e:TimerEvent):void {
y += 3;
if (y>400) {
Remove();
}
}
public function Remove():void {
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, MoveDown);
parent.removeChild(this);
}
}
and here's the the gameplay.as
public class Gameplay extends MovieClip {
private var timer:Timer = new Timer(500);
private var player:Player;
public function Gameplay() {
addEventListener(Event.ADDED_TO_STAGE, InitKeyboard);
addEventListener(Event.ENTER_FRAME, Routine);
gameplayBack.addEventListener(MouseEvent.CLICK, GoToMap);
timer.addEventListener(TimerEvent.TIMER, OnTick);
timer.start();
InitPlayer();
InitLifePoint();
}
private function InitLifePoint():void {
lifePoint.gotoAndStop(1);
}
private function Routine(e:Event):void {
Collision();
}
private function Collision():void {
for (var i:int = 0; i < enemies.length; i++ ) {
if (player.hitTestObject(enemies[i])) {
PlayerHit();
enemies[i].Remove();
return;
}else {
for (var j:int = 0; j < bullets.length; j++ ) {
if (bullets[j].hitTestObject(enemies[i])) {
layerParticle.addChild(new Blast(bullets[j].x, bullets[j].y));
layerParticle.addChild(new Smoke(bullets[j].x, bullets[j].y));
bullets[j].Remove();
enemies[i].Remove();
scorePlay.text = int(scorePlay.text) + 10 + "";
trace(scorePlay.text);
return;
}
}
}
}
}
private var life:int = 1000;
private var currentLife:int = 1000;
private function PlayerHit():void {
currentLife -= 100;
if (currentLife <= 0) {
lifePoint.gotoAndStop(100);
GameOver();
}else {
lifePoint.gotoAndStop(100 - currentLife / life * 100);
}
}
private var result:Result = new Result();
private function GameOver():void {
result.youWin.alpha = 0;
result.ok.addEventListener(MouseEvent.CLICK, GoToMap);
result.x = 0;
result.y = 0;
addChild(result);
}
private function InitKeyboard(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, InitKeyboard);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
}
private function KeyDown(e:KeyboardEvent):void {
switch(e.keyCode) {
case Keyboard.LEFT: MoveLeft(); break;
case Keyboard.RIGHT: MoveRight(); break;
case Keyboard.SPACE: Fire(); break;
}
}
private var bullets:Array = new Array();
private function Fire():void {
var bullet:Bullet = new Bullet(player.x, player.y);
bullet.scaleX = 0.25;
bullet.scaleY = 0.25;
bullet.addEventListener(Event.REMOVED_FROM_STAGE, RemoveBulletArray);
layerParticle.addChild(bullet);
bullets.push(bullet);
}
private function RemoveBulletArray(e:Event):void {
removeEventListener(Event.REMOVED_FROM_STAGE, RemoveBulletArray);
var index:int = bullets.indexOf(Bullet(e.currentTarget), 0);
bullets.splice(index, 1);
}
private function MoveRight():void {
if (player.x < 550) {
player.x += 5;
}
}
private function MoveLeft():void {
if (player.x > 0) {
player.x -= 5;
}
}
private function InitPlayer():void {
player = new Player(550 * 0.5, 350);
layerChar.addChild(player);
}
private function OnTick(e:TimerEvent):void {
RandomEnemy();
}
private var enemies:Array = new Array();
private function RandomEnemy():void {
var enemy:Enemy = new Enemy(Math.random() * 550, 0);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
layerChar.addChild(enemy);
enemies.push(enemy);
}
private var remaining:int = 10;
private function RemoveFromArray(e:Event):void {
removeEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
var index:int = enemies.indexOf(Enemy(e.currentTarget), 0);
enemies.slice(index, 1);
remaining--;
if (remaining == 0) GameWin();
}
private function GameWin():void {
result.youLose.alpha = 0;
result.score.text = scorePlay.text;
result.ok.addEventListener(MouseEvent.CLICK, GoToMap);
result.x = 0;
result.y = 0;
addChild(result);
}
private function GoToMap(e:MouseEvent):void {
dispatchEvent(new ScreenEvent(ScreenEvent.MAP));
}
}
Your problem is a NPE (Null Pointer Exception/Error) inside the Enemy.Remove() method:
public function Remove():void {
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, MoveDown);
parent.removeChild(this);
}
Either your timer property is null (which I doubt, looking at your code) or the parent property are.
In a MovieClip the parent property are filled with a DisplayObject if your MovieClip is added to it, if not, this property is null.
Your problem probably is that you are removing (from its parent) this MovieClip more than once, or is trying to remove it without adding it first.
To make sure this is the problem, add a if statement to check the parent property first, like this:
if(parent != null)
{
parent.removeChild(this);
}
Note:
This may solve your NPE problem, but will not solve what is causing it, which may lead you into more and more inexplicable bugs.
Double check your logic to make sure you're removing a previously added MovieClip, or that you aren't removing it more than once.
Do you notice the flaw in the Collision function, if you observe like this:
for (var i:int = 0; i < enemies.length; i++) {
if (~) {
...
enemies[i].Remove();
...
} else {
for (~) {
if (~) {
...
enemies[i].Remove();
...
}
}
}
}
Apparently, in the second for loop you could be easily referencing the same Enemy Object.
And the problem comes after you call Remove function, because by doing parent.removeChild(this); you remove the only reference of the object to it's parent object.
You can try to do one of these:
Keep a reference to the parent object in the class, manually.
Keep a state variable in the class to check if it is to be a part of display list or not.
Move the enemies[i].Remove(); code into outermost loop.
If possile, recycle the object. Especially, when you are just
moving (x,y) the movieclip around.
Related
I have 2 issues in this code.
The first is:
5006: An ActionScript file can not have more than one externally visible definition: Sprayer, bugs
I've put multiple Actionscripts together to create this, i've seperated out the classes and am hoping to play this on a frame from a symbol.
and the second relates to:
Error #1006: hitTestObject is not a function
For this i'm trying to get the aagun/Sprayer to lose health then lives if the bugs touch it, but i'm not sure why it's saying it's not a function. Am I using the wrong words?
Thanks for your help, here's the code
package Shooter{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
class Sprayer extends MovieClip{
const speed:Number = 150.0;
var lastTime:int; // animation time
function Sprayer() {
// initial location of gun
this.x = 275;
this.y = 340;
// movement
addEventListener(Event.ENTER_FRAME,moveGun);
}
function moveGun(event:Event) {
// get time difference
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// current position
var newx = this.x;
// move to the left
if (MovieClip(parent).leftArrow) {
newx -= speed*timePassed/1000;
}
// move to the right
if (MovieClip(parent).rightArrow) {
newx += speed*timePassed/1000;
}
// check boundaries
if (newx < 10) newx = 10;
if (newx > 540) newx = 540;
// reposition
this.x = newx;
}
// remove from screen and remove events
function deleteGun() {
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,moveGun);
}
}
}
package BigBug{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.TextField;
import flash.display.MovieClip;
class bugs extends MovieClip {
var dx:Number; // speed and direction
var lastTime:int; // animation time
function bugs(side:String, speed:Number, altitude:Number) {
if (side == "left") {
this.x = -50; // start to the left
dx = speed; // fly left to right
this.scaleX = 1; // reverse
} else if (side == "right") {
this.x = -50; // start to the right
dx = -speed; // fly right to left
this.scaleX = 1; // not reverse
}
this.y = altitude; // vertical position
// choose a random plane
this.gotoAndStop(Math.floor(Math.random()*4+1));
// set up animation
addEventListener(Event.ENTER_FRAME,movePlane);
lastTime = getTimer();
}
function movePlane(event:Event) {
// get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// move plane
this.x += dx*timePassed/2000;
// check to see if off screen
if ((dx < 0) && (x < -50)) {
deletePlane();
} else if ((dx > 0) && (x > 350)) {
deletePlane();
}
}
}
}
package Missiles{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.TextField;
import flash.display.MovieClip;
class Bullets extends MovieClip {
var dx:Number; // vertical speed
var lastTime:int;
function Bullets(x,y:Number, speed: Number) {
// set start position
this.x = x;
this.y = y;
// get speed
dx = speed;
// set up animation
lastTime = getTimer();
addEventListener(Event.ENTER_FRAME,moveBullet);
}
function moveBullet(event:Event) {
// get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// move bullet
this.x += dx*timePassed/1000;
// bullet past top of screen
if (this.x < 0) {
deleteBullet();
}
}
// delete bullet from stage and plane list
function deleteBullet() {
MovieClip(parent).removeBullet(this);
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,moveBullet);
}
}
}
package MainGame{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.TextField;
import flash.display.MovieClip;
import Missiles.Bullets;
import Shooter.Sprayer;
import BigBug.bugs;
public class AirRaid extends MovieClip {
private var aagun:Sprayer;
private var airplanes:Array;
private var buggood:Array;
private var bullets:Array;
public var leftArrow, rightArrow:Boolean;
private var nextGbug:Timer;
private var nextPlane:Timer;
private var shotsLeft:int;
private var shotsHit:int;
public function startAirRaid() {
// init score
shotsLeft = 20;
shotsHit = 0;
showGameScore();
// create gun
aagun = new Sprayer();
addChild(aagun);
// create object arrays
buggood = new Array();
airplanes = new Array();
bullets = new Array();
// listen for keyboard
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
// look for collisions
addEventListener(Event.ENTER_FRAME,checkForHits);
// start planes flying
setNextPlane();
setNextGbug();
}
public function setNextPlane() {
nextPlane = new Timer(1000+Math.random()*1000,1);
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}
public function newPlane(event:TimerEvent) {
// random side, speed and altitude
if (Math.random() > .5) {
var side:String = "left";
} else {
side = "right";
}
var altitude:Number = Math.random()*50+20;
var speed:Number = Math.random()*150+150;
// create plane
var p:bugs = new bugs(side,speed,altitude);
addChild(p);
airplanes.push(p);
// set time for next plane
setNextPlane();
}
public function setNextGbug() {
nextGbug = new Timer(1000+Math.random()*1000,1);
nextGbug.addEventListener(TimerEvent.TIMER_COMPLETE,newGbug);
nextGbug.start();
}
public function newGbug(event:TimerEvent) {
// random side, speed and altitude
if (Math.random() > .5) {
var side:String = "left";
} else {
side = "right";
}
var altitude:Number = Math.random()*50+20;
var speed:Number = Math.random()*150+150;
// create Gbug
var p:Good_bug = new Good_bug(side,speed,altitude);
addChild(p);
buggood.push(p);
// set time for next Gbug
setNextGbug();
}
// check for collisions
public function checkForHits(event:Event) {
for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){
for (var airplaneNum:int=airplanes.length-1;airplaneNum>=0;airplaneNum--) {
if (bullets[bulletNum].hitTestObject(airplanes[airplaneNum])) {
airplanes[airplaneNum].planeHit();
bullets[bulletNum].deleteBullet();
shotsHit++;
showGameScore();
break;
}
}
for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){
for (var Good_bugNum:int=buggood.length-1;Good_bugNum>=0;Good_bugNum--) {
if (bullets[bulletNum].hitTestObject(buggood[Good_bugNum])) {
buggood[Good_bugNum].GbugHit();
bullets[bulletNum].deleteBullet();
shotsHit--;
showGameScore();
break;
}
}
}
if ((shotsLeft == 0) && (bullets.length == 0)) {
endGame();
}
}
if ((shotsLeft == 0) && (bullets.length == 0)) {
endGame();
}
}
// key pressed
public function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = true;
} else if (event.keyCode == 39) {
rightArrow = true;
} else if (event.keyCode == 32) {
fireBullet();
}
}
// key lifted
public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = false;
} else if (event.keyCode == 39) {
rightArrow = false;
}
}
// new bullet created
public function fireBullet() {
if (shotsLeft <= 0) return;
var b:Bullets = new Bullets(aagun.x,aagun.y,-300);
addChild(b);
bullets.push(b);
shotsLeft--;
showGameScore();
}
public function showGameScore() {
showScore.text = String("Score: "+shotsHit);
showShots.text = String("Shots Left: "+shotsLeft);
}
// take a plane from the array
public function removePlane(plane:bugs) {
for(var i in airplanes) {
if (airplanes[i] == plane) {
airplanes.splice(i,1);
break;
}
}
}
// take a Gbug from the array
public function removeGbug(Gbug:Good_bug) {
for(var i in buggood) {
if (buggood[i] == Gbug) {
buggood.splice(i,1);
break;
}
}
}
// take a bullet from the array
public function removeBullet(bullet:Bullets) {
for(var i in bullets) {
if (bullets[i] == bullet) {
bullets.splice(i,1);
break;
}
}
}
// game is over, clear movie clips
public function endGame() {
// remove planes
for(var i:int=airplanes.length-1;i>=0;i--) {
airplanes[i].deletePlane();
}
for(var i:int=buggood.length-1;i>=0;i--) {
buggood[i].deleteGbug();
}
airplanes = null;
buggood = null;
aagun.deleteGun();
aagun = null;
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
removeEventListener(Event.ENTER_FRAME,checkForHits);
nextPlane.stop();
nextPlane = null;
nextGbug.stop();
nextGbug = null;
gotoAndStop("gameover");
}
}
}
1.
5006: An ActionScript file can not have more than one externally visible definition: Sprayer, bugs
As it says exactly: you can't have more than one public definition in a file. You have to either split the code to several files or move definitions, that you don't need public, out of the package.
This would be Ok in one file:
package
{
import flash.display.MovieClip;
// public is the default access modifier
public class Test1 extends MovieClip
{
public function Test1()
{
trace("test1");
var t2:Test2 = new Test2();
var t3:Test3 = new Test3();
}
}
}
// Test2 and Test3 are defined outside of the package, otherwise it wouldn't compile.
// These definitions will only be visible to code in this file.
import flash.display.MovieClip;
class Test2 extends MovieClip
{
public function Test2()
{
trace("test2");
}
}
class Test3 extends MovieClip
{
public function Test3()
{
trace("test3");
}
}
2.
Error #1006: hitTestObject is not a function
This usually means that hitTestObject() is not defined on the object (or it's ancestors) you are trying to call it from (although there could be different kinds of errors for that).
hitTestObject() is accessed in two ways in your code: airplanes.hitTestObject() and bullets[bulletNum].hitTestObject(). You will have to debug your code to see what is actually airplanes and bullets[bulletNum], what types they are and whether they inherit hitTestObject() method. You could at least trace() them.
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?
Im making a game as a graduation project and have encountered a little issue. The goal of the game is to maneuver a ship and avoid asteroids as long as possible. I'm almost done but I'm now trying to solve the collision detection.
My problem is how do I reach the enemy in the loop variable so that it can check for collision with it? Here is the code:
package com.asgamer.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Engine extends MovieClip
{
private var numStars:int = 80;
private static var enemyList:Array = new Array();
private var ourShip:Ship;
public function Engine() : void
{
ourShip = new Ship(stage);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
stage.addChild(ourShip);
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1)
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, krash);
}
private function krash(e:Event) : void
{
function krash(e:Event):void
{
if (enemy.hitTestObject(ourShip)==true)
{
stage.removeChild(ourShip);
stage.removeEventListener(Event.ENTER_FRAME, krash);
}
}
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 10) == 5)
{
var enemy:Asteroid = new Asteroid(stage);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
}
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
}
}
As you can see I have a function for making enemies which is called loop, and a function for collision detection called krash. But since enemy is a variable inside loop, how can I check for collision with it in the krash function. (Sort of new to actionscript, so I dont know the terminology all to well)
In krash you will need to check each enemy in enemyList.
example:
private function krash(e:Event) : void
{
function krash(e:Event):void
{
for(var i:int = 0; i < enemyList.length; i++)
{
if (enemyList[i].hitTestObject(ourShip)==true)
{
stage.removeChild(ourShip);
stage.removeEventListener(Event.ENTER_FRAME, krash);
}
}
}
}
Unfortunately, if you have a lot of enemies on the stage the program will lag a bit because you're checking for collisions with all of them every frame.
Also, I'd reccommend you add something to remove you enemies from the screen :P
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
stage.removeChild(enemyList.indexOf(e.currentTarget));
}
Hope this helps.
I'm having some problems with trying to remove Event.ADD_TO_STAGE basically in this game ive developed once the lives is less than or equal to zero the game will end and switch to the game over screen however when I do that it kicks back with this error.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at States/changeState()
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
This then turns into..
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Enemy/StayOnScreen()
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
I'm not entirely sure why - could anyone clarify why it does and how I could fix it?
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;
public class AvoiderGame extends MovieClip
{
var theCallBackFunction:Function;
public static var enemyArray:Array;
public var enemy:Enemy
public var Background:gameBackground;
public var Lives:Number = 3;
public var avatar:Avatar;
public var gameTimer:Timer;
private var _collisionTest:CollisionTest;
private var numStars:int = 80;
private var fireTimer:Timer; //causes delay between fires
private var canFire:Boolean = true; //can you fire a laser
public function AvoiderGame(callBack)
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
theCallBackFunction = callBack;
}
private function init(e:Event):void
{
Background = new gameBackground();
addChild(Background);
enemyArray = new Array();
avatar = new Avatar(stage);
addChild(avatar);
avatar.x = stage.stageWidth / 2;
avatar.y = stage.stageHeight / 2;
_collisionTest = new CollisionTest();
gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1);
}
fireTimer = new Timer(1000, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
fireTimer.start();
}
public function onTick(timerEvent:TimerEvent):void
{
if (Math.random() < 0.1)
{
trace('array length: ', AvoiderGame.enemyArray.length);
enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
enemyArray.push(enemy);
addChild(enemy);
enemy.gotoAndStop("Enemy" + Math.round(1 + (5 - 1) * Math.random()))
}
canFire = avatar.UpdateAvatar(canFire);
if (canFire == false)
{
fireTimer.start();
}
avatar.StayOnScreen();
for each (var enemy:Enemy in enemyArray)
{
enemy.moveDown();
enemy.StayOnScreen();
if (_collisionTest.complex(enemy, avatar))
{
gameTimer.stop();
for each (var enemy:Enemy in enemyArray)
{
for(var i:int = 0; i < enemyArray.length; i++)
{
removeChild(enemyArray[i]);
enemyArray.splice(i, 1); //remove the i'th element as i'th element is the enemy containing the ID of hit enemy
}
}
Lives--;
trace('lives: ', Lives);
gameTimer.start();
}
}
if (Lives == 0)
{
removeEventListener(Event.ADDED_TO_STAGE, init)
theCallBackFunction(this, "over");
}
}
private function fireTimerHandler(e:TimerEvent) : void
{
//timer ran, we can fire again.
canFire = true;
}
}
}
//use weak references so the listener can be disposed of if necessary
this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
private function init(e:Event):void {
//remove this now as we have added the object to the stage
this.removeEventListener(Event.ADDED_TO_STAGE, init);
//add a new listener that will fire when the object is removed from stage
this.addEventListener(Event.REMOVED_FROM_STAGE, reset, false, 0, true);
...
private function reset(e:Event):void {
//this function is called when the object has been removed from the stage
this.removeEventListener(Event.REMOVED_FROM_STAGE, reset);
//now make sure all timers and stopped and nulled if required
//as well as all objects removed from stage and nulled if required. e.g.
fireTimer.stop();
fireTimer = null;
enemyArray = [];
enemyArray = null;
enemy
removeChild(Background);
Background = null;
removeChild(avatar);
avatar = null;
gameTimer.stop();
gameTimer = null;
_collisionTest = null;
}
//NB. You are adding stars to the stage and have no way of referencing them to remove them.
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1);
}
//you might want to add them to an array so you can remove them.
var starArray = [];
for (var i:int = 0; i < numStars; i++)
{
var newStar:Star = new Star(stage);
stage.addChildAt(newStar, 1);
starArray.push(newStar);
}
Then to remove them:
for (var i:int = starArray.length; i > 0; i--)
{
stage.removeChild(starArray[starArray.length-1]);
}
starArray = [];
starArray = null;
Ok, so I've been stuck on this for about three hours now and I finally feel like asking for help.
Basically, I am trying to remove all instances of an enemy object from the screen when my player ship makes contact with one of them, as he then loses a life and is put back into a safe position on the screen.
EDIT: This is all the code from my Enemy Dude .as file, a bit overboard maybe but nonetheless.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
public class Enemydude extends MovieClip
{
private var _root:Object;
private var speed:int = 6;
private var shipps = this
public function Enemydude()
{
addEventListener(Event.ADDED, beginclass);
addEventListener(Event.ENTER_FRAME, entFrame);
}
private function beginclass(event:Event):void
{
_root = MovieClip(root);
}
private function entFrame(event:Event):void
{
x -= speed;
if(this.x < -64)
{
removeEventListener(Event.ENTER_FRAME, entFrame);
_root.removeChild(this)
}
if(_root.gameover)
{
x = -700
removeEventListener(Event.ENTER_FRAME, entFrame);
removeEventListener(Event.ADDED, beginclass);
}
for (var i:int = 0; i<_root.playerBulletContainer.numChildren; i++)
{
var bulletTarget:MovieClip = _root.playerBulletContainer.getChildAt(i)
if (hitTestObject(bulletTarget))
{
removeEventListener(Event.ENTER_FRAME, entFrame);
_root.removeChild(this);
_root.playerBulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
_root.Score += 10
makeExplosion();
}
}
if(hitTestObject(_root.mcship))
{
makeExplosion();
shipPos();
removethis();
}
}
private function makeExplosion()
{
var sndExplode:snd_explosion1;
var sndExplodeChannel:SoundChannel;
sndExplode=new snd_explosion1();
sndExplodeChannel=sndExplode.play();
var newExplosion01:explosionEffect=new explosionEffect ;
newExplosion01.x=this.x;
newExplosion01.y=this.y;
_root.explosionContainer.addChild(newExplosion01);
}
private function shipPos()
{
_root.lives -= 1;
_root.mcship.x = 80;
_root.mcship.y = 225;
for each(var i:Enemydude in _root.enemies)
{
removethis();
}
_root.enemies.length = 0;
}
public function removethis():void
{
if(parent) parent.removeChild(this)
removeEventListener(Event.ENTER_FRAME, entFrame);
}
}
}
EDIT: And this is the code I now have that relates to the Enemydude in my main timeline, quite sorry about all this.
var enemies:Array = [];
var Shipheight:Number = 300;
var Enemytime:int = 0;
var Enemylimit:int = 16;
if (Enemytime<Enemylimit)
{
Enemytime ++;
}
else
{
var newEnemy01 = new Enemydude();
newEnemy01.y = Shipheight;
newEnemy01.x = stage.stageWidth + 64;
addChild(newEnemy01);
enemies.push(newEnemy01);
Enemytime = 0
function shipY(event:Event):void
{
Shipheight = Math.ceil(Math.random()* 250) + 80;
}
Thank you for your help in advance, any advice is appreciated.
I suggest storing your enemies in an Array.
For example, create the array enemies:
var enemies:Array = [];
And then amend your code to:
else
{
var newEnemy01 = new Enemydude();
newEnemy01.y = Shipheight;
newEnemy01.x = stage.stageWidth + 64;
addChild(newEnemy01);
enemies.push(newEnemy01);
Enemytime = 0;
}
That way you can remove all of the enemies using this new array:
for each(var i:Enemydude in enemies)
{
i.remove(); // Or whatever function Enemydude has to remove itself.
}
// Empty the enemies Array.
enemies.length = 0;
Here's the .remove() method you could make for Enemydude:
public function remove():void
{
if(parent) parent.removeChild(this);
// Remove any event listeners etc from this object.
}
A common and easy way of doing this is to create a subcontainer to hold the objects and destroy this object instead. It makes easy for some collision checks too, since you can use the holder object to make one check against the player.
If you don't want to create this, you can use an array or a vector to store references to this objects, what makes easy to traverse the list.
I persoally recommend the vector aprouch:
var enemyList:Vector.<Enemy> = new Vector.<Enemy>();
Then you can loop almost like an array (as Marty Wallace showed on his answer):
for each(var e:Enemy in enemyList) {
container.removeChild(e);
}
enemyList.length = 0; // empty vector
Vectors are a bit slower than normal arrays, but are type safe. The difference in performance is almost negligible in most cases.