Recycle/AnimationCallback problem


(Mihael Međan) #1

Hello, I have a very strange issue. http://imageshack.com/a/img690/8800/5s8o.png (imageshack seem to be struggling) https://www.dropbox.com/s/uhn738myu60uxox/demonstration.png

package entities.effects.explosions 
{
import entities.effects.Explosion;
import net.flashpunk.FP;
import net.flashpunk.graphics.Spritemap;
import net.flashpunk.Entity;
/**
 * ...
 * @author ...
 */
public class ExplosionAirstrike extends Entity 
{
	public var sprite:Spritemap;
	public var damage:int;
	
	public function ExplosionAirstrike() 
	{
		super();
		
		sprite = new Spritemap(Assets.imgSteamExplosion, 100, 100, destroy);
		sprite.add("explode", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 12, false);
		// sprite.smooth = true;
		// sprite.scale = 1.3;
		sprite.centerOrigin();
		graphic = sprite;
					
		setHitbox(sprite.scaledWidth, sprite.scaledHeight, sprite.scaledWidth / 2, sprite.scaledHeight / 2);
		
		type = "explosion";
		layer = -1000;
		damage = 150;
	}	

	public function init(posX:int, posY:int):void
	{
		this.x = posX;
		this.y = posY;
		
		sprite.play("explode");
	}
	
	private function destroy() :void
	{
		trace("called");
		world.recycle(this);
	}
}
}

What happens, if i use world.remove(this), everything works fine. If I, on the other hand, use world.recycle(this), destroy gets called 3-5 times (based on the traces) and I get that strange artefacts you can see on the picture (which are basically what left of the objects because destroy didn’t get called). What am I doing wrong?

I’m spawning this entity like this:

ExplosionAirstrike(world.create(ExplosionAirstrike)).init(200 + strike * 80, 390 + FP.rand(50));

(Jonathan Stoler) #2

I’m not 100% sure about this, but I think you want

world.create(ExplosionAirstrike)).init(200 + strike * 80, 390 + FP.rand(50));

Right now, you’re passing FlashPunk’s created/recycled ExplosionAirstrike object to the constructor of ExplosionAirstrike (I’m surprised this compiles and runs without error). Not sure if this is the cause of your problem, though. I don’t see the correlation, but it’s worth a shot.


(Mihael Međan) #3
(world.create(ExplosionAirstrike) as ExplosionAirstrike).init(200 + strike * 80, 390 + FP.rand(50));

I’ve tried using this now, same result. Thing before was just casting the entity as my object, it’s not constructor as new was not called.


(Mihael Međan) #4
if (sprite.frame == 12) {
    world.recycle(this);
}

If i try tricking him (i added one extra frame, and this in the update), nothing happens once it should recycle.

I’m probably doing something stupid, I have almost the same class for another particle, it works as long as I don’t need to use recycled object, then it just shows last frame of the animation. Any suggestions where to look?

It works if I do sprite.play(“explode”, true, 0); Someone who is better than me care to explain? :slight_smile: