Event.ADD_TO_STAGE
基本的に、ライフがゼロ以下になると開発されたこのゲームiveを削除しようとすると、ゲームが終了してゲームオーバー画面に切り替わりますが、そうすると、このエラーでキックバックします。
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()
その後、これはに変わります。
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()
理由は完全にはわかりません-なぜそうなるのか、どうすれば修正できるのかを誰かが明確にできますか?
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;
}
}
}