这是我的播放器类
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class Player extends MovieClip
{
//private var stageRef:Stage;
//var xx:Number;
//var yy:Number;
private var BG:MovieClip;
var speed:Number = 7;
public function Player(xx:Number,yy:Number,BG:MovieClip) : void
{
x = xx;
y = yy;
this.BG = BG;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
var yDistance:Number = BG.mouseY - y;
var xDistance:Number = BG.mouseX - x;
var radian:Number = Math.atan2(yDistance, xDistance);
rotation = radian * 180 / Math.PI;
}
}
}
子弹类
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
public class Bullet extends MovieClip
{
private var dx,dy:Number;
private var lastTime:int;
public function Bullet(mX,mY:Number, rotate:Number, speed:Number)
{
// set start position
var GunLength:Number = 70;
this.x = 600 + GunLength*Math.cos(2*Math.PI*rotate/360);
this.y = 450 + GunLength*Math.sin(2*Math.PI*rotate/360);
this.rotation = rotate;
// get speed
dx = speed*Math.cos(2*Math.PI*rotate/360);
dy = speed*Math.sin(2*Math.PI*rotate/360);
// set up animation
lastTime = getTimer();
addEventListener(Event.ENTER_FRAME,moveBullet);
}
public function moveBullet(event:Event)
{
// get time passed
var timePassed:int = getTimer() - lastTime;
lastTime += timePassed;
// move bullet
this.x += dx * timePassed / 1000;
this.y += dy * timePassed / 1000;
}
}
}
敌人等级
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class Enemy1 extends MovieClip
{
private var BG:MovieClip;
private var speed:Number = 0.5;
public function Enemy1(BG:MovieClip) : void
{
var RandomX:Array = new Array(100,200,300,400,800,900,1000,1100);
var RandomY:Array = new Array(100,200,300,600,700,800);
var r:int = (Math.random() * 8);
var s:int = (Math.random() * 6);
x = RandomX[r];
y = RandomY[s];
this.BG = BG;
addEventListener(Event.ENTER_FRAME, moveEnemy); //false, 0, true);.
}
private function moveEnemy(e:Event):void
{
if (this.x > 660)
{
this.x -= speed;
this.rotation = 180;
}
if (this.x < 540)
{
this.x += speed;
this.rotation = 0;
}
if (this.y > 510)
{
this.y -= speed;
this.rotation = 270;
}
if (this.y < 390)
{
this.y += speed;
this.rotation = 90;
}
if (this.x > 660 && this.y > 510)
{
this.x -= speed;
this.y -= speed;
this.rotation = 225;
}
if (this.x < 540 && this.y < 390)
{
this.x += speed;
this.y += speed;
this.rotation = 45;
}
if (this.x < 540 && this.y > 510)
{
this.x += speed;
this.y -= speed;
this.rotation = 315;
}
if (this.x > 660 && this.y < 390)
{
this.x -= speed;
this.y += speed;
this.rotation = 135;
}
}
}
}
主班
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.TextField;
import flash.ui.Mouse;
public class Main extends MovieClip
{
// Player Var
private var player:MovieClip;
private var bunker_mc:MovieClip;
// Bullet var & Array
private var b:MovieClip;
private var bullets:Array;
private var gunFire:Boolean = false;
// Enemies, deadAnimation var & Array
private var enemy1:MovieClip;
private var tempEnemy1:MovieClip;
private var dead1:MovieClip;
private var enemies1:Array;
private var deadArray1:Array;
private var dustArray:Array;
// Effect var
private var dust:MovieClip;
// Score & other Helper var
private var Level:int = 1;
private var Score:int = 0;
public function startMain()
{
// Timers Initialisation
var GameLevelTimer:Timer = new Timer(1000,15);
var EnemyTimer:Timer = new Timer(1000,10);
//
var bg_mcX = bg_mc.width/2;
var bg_mcY = bg_mc.height/2;
// Variable initialisation
bunker_mc = new bunker();
bunker_mc.x = stage.stageWidth/2;
bunker_mc.y = stage.stageHeight/2;
addChild(bunker_mc);
Lvl_txt.text = String(Level);
Scr_txt.text = String(Score);
if(Level == 1)
{
player = new Player(bg_mcX,bg_mcY,bg_mc);
bg_mc.addChild(player);
}
// Objects arrays
bullets = new Array();
enemies1 = new Array();
deadArray1 = new Array();
dustArray = new Array();
// Event Listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
stage.addEventListener(Event.ENTER_FRAME,updateBullet);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveBG);
// Timer Events Start
EnemyTimer.addEventListener(TimerEvent.TIMER,EnemyTimerStart);
EnemyTimer.start();
GameLevelTimer.addEventListener(TimerEvent.TIMER_COMPLETE,GameLevelTimerEnd);
GameLevelTimer.start();
}
//Mouse Down Event Handler
private function mouseDownFun(event:MouseEvent):void
{
fire();
}
private function moveBG(e:KeyboardEvent):void
{
}
private function EnemyTimerStart(e:TimerEvent):void
{
if(Level == 1)
{
enemy1 = new Enemy1(bg_mc);
enemies1.push(enemy1);
bg_mc.addChild(enemy1);
}
}
private function updateBullet(e:Event):void
{
var i:int;
var tempBullet:MovieClip;
// loop for _bullets array
for (i = 0; i < bullets.length; i++)
{
// save a reference to current bullet
tempBullet = bullets[i];
// check for collision with Enemies.
if (checkCollision(tempBullet.x,tempBullet.y))
{
destroyBullet(tempBullet);
}
}
}
private function fire()
{
player.play();
if(Level == 1)
{
b = new Bullet(mouseX,mouseY,player.rotation,500);
bullets.push(b);
bg_mc.addChild(b);
}
}
private function checkCollision(testX:Number,testY:Number):Boolean
{
for (var i = 0; i < enemies1.length; i++)
{
tempEnemy1 = enemies1[i];
if(tempEnemy1.hitTestPoint(testX,testY,true))
{
enemies1.splice(i, 1);
//tempEnemy1.parent.removeChild(tempEnemy1);
bg_mc.removeChild(tempEnemy1);
dead1 = new Dead1(tempEnemy1.x,tempEnemy1.y,tempEnemy1.rotation);
deadArray1.push(dead1);
bg_mc.addChild(dead1);
Score += 10;
Scr_txt.text = String(Score);
return true;
}
}
//removeChild(b,enemy1);
return false;//}
}
private function destroyBullet(bullet:MovieClip):void
{
var tempBullet:MovieClip;
for (var i = 0; i<= bullets.length;i++)
{
bg_mc.tempBullet = bullets[i];
if (tempBullet == bullet)
{
dust = new dust_mc();
dust.x = bullet.x;
dust.y = bullet.y;
dust.rotation = (Math.random() * 360);
// remove from array
bullets.splice(i, 1);
bullet.parent.removeChild(bullet);
//bg_mc.removeChild(bullet);
dustArray.push(dust);
bg_mc.addChild(dust);
return;
}
}
}
private function GameLevelTimerEnd(e:TimerEvent):void
{
var tempDead:MovieClip;
var tempDust:MovieClip;
removeChild(player);
removeChild(bunker_mc);
stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownFun);
stage.removeEventListener(Event.ENTER_FRAME,updateBullet);
for (var i = 0; i < deadArray1.length; i++)
{
tempDead = deadArray1[i];
removeChild(tempDead);
}
for (var j = 0; j < dustArray.length; j++)
{
tempDust = dustArray[j];
removeChild(tempDust);
}
Level += 1;
gotoAndStop("end");
}
//Mouse Up Event Handler
/*private function mouseUpFun(event:MouseEvent):void
{
}*/
}
}
我面临的问题是我的命中测试属性不起作用,还有错误 2025
"ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Main/GameLevelTimerEnd()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.utils::Timer/tick()"
我是否缺少任何代码,以便该属性可以工作..?
请帮我解决这个问题.. 提前致谢