我正在尝试使用 hitTestObject 函数制作一个带有子弹射击和船只相互碰撞的太空入侵者类型游戏,但我无法获得 removeChild(); 功能可以在没有上述错误的情况下工作。这是我应该怎么做的代码。
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
var count:int = 1;
//adding the components
var NewBullet:MovieClip = new Bullet;
var Ship_M:MovieClip = new Ship;
Ship_M.x = 270;
Ship_M.y = 470;
addChild(Ship_M);
var Ship_E:MovieClip = new E_Ship;
Ship_E.x = 270;
Ship_E.y = 5;
addChild(Ship_E);
stage.addEventListener(Event.ENTER_FRAME , Rec);
function Rec(e:Event):void{
if (NewBullet.hitTestObject(Ship_E))
{
removeChild(Ship_E);
removeChild(NewBullet);
}
if (Ship_E.hitTestObject(Ship_M))
{
removeChild(Ship_E);
removeChild(Ship_M);
}
}
function Moves(e:Event):void{
NewBullet.y -= 30;
if (NewBullet.y < 0 )
{
removeChild(NewBullet);
count++;
removeEventListener(Event.ENTER_FRAME, Moves);
}
trace (count);
}
//For Moving the Spaceship
stage.addEventListener(KeyboardEvent.KEY_DOWN, Move);
function Move (event:KeyboardEvent):void{
switch(event.keyCode)
{
case 37:
if (Ship_M.hitTestObject(Stop_L1))
{
Ship_M.x -= 0;
}
else
{
Ship_M.x -= 10;
}
break;
case 38:
if (Ship_M.hitTestObject(Stop_U1))
{
Ship_M.x -= 0;
}
else
{
Ship_M.y -= 10;
}
break;
case 39:
if (Ship_M.hitTestObject(Stop_R1))
{
Ship_M.x -= 0;
}
else
{
Ship_M.x += 10;
}
break;
case 40:
if (Ship_M.hitTestObject(Stop_D1))
{
Ship_M.x -= 0;
}
else
{
Ship_M.y += 10;
}
break;
case 32:
addChild(NewBullet);
NewBullet.x = Ship_M.x;
NewBullet.y = Ship_M.y;
addEventListener(Event.ENTER_FRAME, Moves);
break;
default:
}
}