Help using create/destroy


(Nate ) #1

Hey guys! It’s been a while!

I am getting back into FlashPunk and working on a very simple project. I am attempting to use create/destroy/recycle as I know that is the best way to handle things.

I have a player and I want him to throw coins up on the Y axis.

Here is the relevant player code in the player update:

		if(Input.pressed(Key.SPACE))
		{
			
			var _coin:theCoin = FP.world.create(theCoin) as theCoin;
			_coin.setMovement(this.y);
			
			trace("coin created");
		}

Here is the coin.as class:

package entities {
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.graphics.Image;

public class theCoin extends Entity
{
	[Embed(source = '../assets/coin.png')]
	
	public static const COIN:Class;

	private var moveY:Number = 0;
	
	public function theCoin() 
	{
		graphic = new Image(COIN);
		
		setHitbox(8, 8);
		
		type = "coin"; 
		
		layer = 0;
		
	}
	
	public function setMovement(_y:Number = 0):void
	{
		moveY = _y;
	}
	
	override public function update():void
	{
	
		y -= moveY * FP.elapsed;
		
	}
	
	public function destroy():void
	{
		FP.world.recycle(this);
	}
	
	
	
	
}}

Not sure what I am doing incorrectly, sorry in advance for sloppy code.

Thank you!


(Ultima2876) #2

What is the actual problem you’re having? Is the object spawning at all, or is it that it’s not recycling correctly? Your code looks OK to me at first glance but I’m not 100% sure what kind of issue I’m looking for!

One thing to make sure of is that your destroy() function is actually called somewhere (like if the coin goes off the top of the screen). If destroy() is never called, recycle() is never called and you’ll never have any ‘ready to recycle’ entities in the recycle bin.