Duplicate entity when added to world


(Zouhair Elamrani Abou Elassad) #1

… I’m trying to add an entity to my world and at the same time add a few clones of it just like this :

i created a class with a function to generate a copy of it, but i don’t know how to use it once added to the world :

public class LineTrap extends Entity 
{
	[Embed(source = "../../assets/images/trap/ball.png")]
	protected static const LINETRAP_ART:Class;	
	protected var _lineTrap:Image;
	
	protected var enemyNumber:Number;
	
	
	private const spaceBetween:Number = 10;
	
	
	
	public function LineTrap(x:Number=0, y:Number=0, repeated:Number=5) 
	{
		this.x = x;
		this.y = y;
		
		_lineTrap = new Image(LINETRAP_ART);
		
		enemyNumber = repeated;
		
		layer = 1;
		type = "lineTrap";
		
	}
	
	override public function added():void 
	{
		graphic = _lineTrap;
		
	}
	
	override public function update():void 
	{
		x -= Globals.lineTrapSpeed * FP.elapsed;
		
		if (x < - _lineTrap.width) {
			world.recycle(this);
		}
	}
	
	public function addLineTrap():void {
		for (int i = 0; i < enemyNumber; i++ ) {
			aCopyOf(this);
		}
	}
	
	public function aCopyOf(original:Entity) {
		
		this.x = original.x + spaceBetween;
		this.y = y;
		
		spaceBetween += _lineTrap.width;
	}
	
	
	
	
}

}


(Martí Angelats i Ribera) #2

I think the easiest way to do this is crating a static function that creates n enitities. The class should be something like this:

//package, imports, etc.
public class LineTrap extend Entity
{
	
	//graphic
	[Embed(source = "../../assets/images/trap/ball.png")]
	protected static const LINETRAP_ART:Class;	
	protected var _lineTrap:Image;
	
	public static var spaceBetween:Number = 10;
	
	public LineTrap(x:Number, y:Number)
	{
		_lineTrap = new Image(LINETRAP_ART);
		super(x, y, _lineTrap);
		
		layer = 1;
		type = "lineTrap";
	}
	
	//All your custom functions
	
public static function createHoritzontalLine(x:Number, y:Number, number:Number, world:World)
	{
		for(var i=0; i < number; i++)
		{
			world.add(new LineTrap(x + (spaceBetween+graphic.width)*i, y));
		}
	}
	
	public static function createVerticalLine(x:Number, y:Number, number:Number, world:World)
	{
		for(var i=0; i < number; i++)
		{
			world.add(new LineTrap(x, y + (spaceBetween+graphic.height)*i));
		}
	}
	
}

Then in the world class to call this function you have to do:

LineTrap.createHoritzontalLine(x, y, n, this);

Edit: added the vertical one


(Zouhair Elamrani Abou Elassad) #3

Great work, many thanks :smile: , just one thing the ‘graphic’ is not defined inside the static function i tried to use : _lineTrap.width but _lineTrap is not yet initialized when passing it to the constructor, i had to go with a const value, once again thank you.


(Jacob Albano) #4

To clarify why this isn’t working, all you’re doing in your aCopyOf() function is setting the original entity’s based on its own position. You never create a new instance.

public function aCopyOf(original:Entity):void {
    var copy = new LineTrap(original.x + spaceBetween, y, enemyNumber);
    world.add(copy);
}

(Zouhair Elamrani Abou Elassad) #5

I forgot about it , thank you


(Martí Angelats i Ribera) #6

Oh, true. Didn’t test it actually.