Author Topic: FlxEmitter / hurt animation  (Read 838 times)

lordkuragari

  • Guest
FlxEmitter / hurt animation
« on: Mon, Jul 12, 2010 »


so im having some serious problems with FlxEmitter and the hurt animation in my enemy
Code: [Select]
package com.Death
{
import org.flixel.*;

public class Enemy extends FlxSprite
{

[Embed(source="../../data/Guy8.png")] private var ImgEnemy:Class;

[Embed(source="../../data/Blood.png")] private var ImgBlood:Class;

private var _player:Player;
private var _move_speed:int = 400;
public var _max_health:int = 1;
public var _hurt_counter:Number = 0;
private var _move_counter:int = 0;
private var _can_move:int = 16;
private var move:int = 0;

private var _blood:FlxEmitter;

public function Enemy(X:Number,Y:Number,ThePlayer:Player)
{
super(X,Y);
loadGraphic(ImgEnemy, true, true, 16, 16);
_player = ThePlayer;


//Max speeds
maxVelocity.x = 64;
maxVelocity.y = 64;

//set the player health
health = 1;

//Friction
drag.x = 300;
drag.y = 300;

//bounding box tweaks
width = 8;
height = 14;
offset.x = 4;
offset.y = 2;

//Animation setup
addAnimation("down", [0,],1);
addAnimation("up", [1], 1);
addAnimation("right", [2], 1);
addAnimation("left", [2], 1);


addAnimation("hurt", [3],1);
facing = DOWN;

_blood = FlxG.state.add(new FlxEmitter(X, Y)) as FlxEmitter;
_blood.createSprites(ImgBlood, 50,16, true);
exists = true;

}
override public function update():void
{
//check if dead
if (dead)
{
kill();
//if done dieing then remove
if (finished) exists = false;
else
//else update
super.update();
return;
}

//health check

if (_hurt_counter > 0)
{
_hurt_counter -= FlxG.elapsed;
play("hurt");
}


//move randomly
// same movement as player
if (_move_counter <= 0 && _can_move <= 0)
{
move = Math.round((Math.random()*5));
_can_move = 64;
}
else
{
_move_counter -= FlxG.elapsed;
_can_move -= FlxG.elapsed;

}
if (move == 1)
{
facing = LEFT;
_move_counter = 16;
move = 0;
}
if (move == 2)
{
facing = RIGHT;
_move_counter = 16;
move = 0;
}
if (move == 3)
{
facing = UP;
move = 0;
_move_counter = 16;
}
if (move == 4)
{
facing = DOWN;
move = 0;
_move_counter = 16;
}

if(_move_counter > 0)
{

switch(facing)
{
case LEFT:
velocity.x += 8;
break;
case RIGHT:
velocity.x -= 8;
break;
case UP:
velocity.y -= 8;
break;
case DOWN:
velocity.y += 8;
break;
}

}
//animation assignments
if(FlxG.keys.X)
{
play("hurt");
_blood.x = x
_blood.y = y
_blood.start();
}

if (_hurt_counter > 0)
{
play("hurt");
_blood.x = x
_blood.y = y
_blood.start();

}
else           
{
if (facing == UP)
{
play("up");
}
else if (facing == DOWN)
{
play("down");
}
else if (facing == LEFT)
{
play("left");
}
else if (facing == RIGHT)
{
play("right");
}
}
super.update();
}


override public function hurt(Damage:Number):void
{
_hurt_counter = 100;
return super.hurt(Damage);
}   
override public function kill():void
{         
velocity.x = 0;
velocity.y = 0;
_blood.x = x
_blood.y = y
_blood.start();
play("hurt");
super.kill();
}

}

}

http://www.swfcabin.com/open/1278971418 
for  the latest build

zez

  • Active Member
  • ***
  • Posts: 203
  • Karma: +0/-0
    • View Profile
    • Devlog
Re: FlxEmitter / hurt animation
« Reply #1 on: Tue, Jul 13, 2010 »
blood = new FlxEmitter(X,Y);
blood.creatSprites((ImgBlood, 50 16, true);
playstate.flxgroupthatisastaticvariable.add(blood);

and that should do it.
For future reference, you can recycle the same emitter or sprites over and over again by using Flxgroup.getAvail()
Its handy, you can do stuff like
if (group.getAvail() == null)
{
thingy = new thingy(stuff);
thingy.dostufftomakeitathingy;
group.add(thingy);
}
else
{
thingy = group.getAvail();
thingy.dostufftoresetthethingy;
}

ofcourse, you need to make sure your group only has thingys for that to work, otherwise it will return some other thing in the group and try to assign it too a thingy, and either recycle it (like if thingy is a standard flxsprite, and the object getAvail() returns is also a flxsprite) or just freeze your program (if thingy is something other then what the other thing that getAvail() returned.)

Probably doesnt matter much in this game, but recycling objects is crucial if you want your game to have a bunch of stuff and run at a good fps.