I have swords (consider them bullets) that the Hero throws, when it hits the tilemap the sword (bullet) is killed using kill()
method.
But unexpected thing happened. When using kill()
, exists = false or visible = false ALL OF THE GROUP IS GONE (killed or invisible).
these are some snippets:
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;
}
}
The subclass of Sword(BasicSword)
has only sword graphics so it doesn't worth mentioning.
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);
Update:
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();
}