我有英雄投掷的剑(考虑它们是子弹),当它击中瓷砖地图时,剑(子弹)被使用kill()
方法杀死。但是意想不到的事情发生了。使用时kill()
,exists = false 或 visible = false ALL OF THE GROUP IS GONE (killed or invisible)。
这些是一些片段:
Sword.as(base class for all swords) shoot function :
public function shoot(playerPosition:FlxPoint, direction:uint):void{
if(!alive || !exists){
revive();
}
x = playerPosition.x;
y = playerPosition.y;
if(direction == FlxObject.RIGHT) {
angularVelocity = 900;
velocity.x = 400;
} else {
angularVelocity = 900;
velocity.x = -400;
}
}
的子类Sword(BasicSword)
只有剑图形,所以不值得一提。
PlayState.as
create (only part of it):
for(var i:int = 0; i < 15;i++) {
sword = new BasicSword(-200, -200);
swords.add(sword);
}
add(swords);
更新:
override public function update():void{
if(FlxG.keys.justPressed("X")) {
(swords.recycle(BasicSword) as BasicSword).shoot(new FlxPoint(player.x,
player.y),player.facing);
}
super.update();
FlxG.collide(level, player);//make the player stand on the level.
FlxG.collide(level, swords, swordsHitLevel);
}
swordsHitLevel(callback function):
public function swordsHitLevel(level:FlxTilemap, sword:FlxSprite):void {
sword.kill();
}