我想使用hitTestObject函數使子彈射擊和船舶崩潰到另一個太空入侵者類型遊戲,但我無法得到removeChild();函數在沒有上述錯誤的情況下工作。這是守則我該怎麼做。ArgumentError:錯誤#2025:提供的DisplayObject必須是調用者的子項。 AS3
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:
}
}
理想情況下,您應該使用lowerCamelCase命名變量以避免混淆。類名通常以大寫字母開頭。 – crooksy88
請考慮使用光柵(bitmapData)和copyPixels來「渲染」您的遊戲,從而大大提高性能。 – zehelvion