你好!
我真的不明白您所说的“匿名”是什么意思,但解决方案之一是创建所谓的hitboxes。
让我们假设你有一个 Enemy 类:
public class Enemy {
public var hitBox:Sprite = new Sprite() ;
public function Enemy() {
hitBox.graphics.clear() ; /* Not filled by a color, as needs to be invisible */
hitBox.graphics.drawRect(x,y,width,height) ; /* Adjust the parameters manually */
this.addChild(hitBox) ;
}
}
有很多敌人,所以只需创建enemyArray 并将敌人推入其中。这是玩家的子弹:
public class PlayerBullet extends Sprite {
private var stageReference:Stage ;
public function PlayerBullet(coord:Point, stageReference:Stage){
this.x = coord.x ;
this.y = coord.y ;
this.stageReference = stageReference ;
this.stageReference.addChild(this) ;
this.addEventListener(Event.ENTER_FRAME, loop) ;
}
private function loop(e:Event){
/*Provide some movement for bullet by changing or incrementing
this.x and this.y as you wish */
for (var i:Number = 0 ; i < enemyArray.length ; i++){
if (this.hitTestObject(enemyArray[i].hitBox)) {
enemyArray.splice(i,1) ; /* Remove the enemy from enemy array on collision */
this.stageReference.removeChild(this) ; /* Do not display bullet anymore on collision with enemy */
}
}
}
}
注意:hitboxes 确实只提供了一些正方形区域。如果您需要一些确切的碰撞细节,请创建几个碰撞箱。
我希望这个能帮上忙 !:)