How to stock in array when we got multiple entities in the Class


(Enzo M) #1

Hi,

I write this message as a SOS. I tried everything I could and searched everywhere… I am doing a little game jam with a friend and got stucked near the end…

I got an Entity class Tornado. Those one are created when the player left click and they are two types of tornadoes : blue and red. We did a red animation and a blue animation. Everything is good, when I click, a tornado pops on my mouseX/Y and the animation goes on.

BUT, I would like to be able to create multiple tornado, I mean, when I click again, I don’t wan’t the last one to get deleted. The two conditions for them to be deleted would be : their animation is over (like 7 images) or a collide is detected between it and another entity I determined.

I tried everything to create an Array and stock them in, but nothing I do is the right thing :frowning:

The last thing I tried is :

public class Wave extends Entity 
	{
		[Embed(source="Assets/Wave[ANIM].png")]
		private const wave:Class;
		
		public var sprWave:Spritemap = new Spritemap(wave, 72, 72);
		
		public var redarray:Array = new Array();
		public var e:Spritemap;
		
		public function Wave() 
		{
		sprWave.add("red", [0, 1, 2, 3, 4, 5, 6, 7], 10, false);
		sprWave.add("blue", [8, 9, 10, 11, 12, 13, 14, 15], 10, false);
			
			type = "onde";
		}
       override public function update():void 
        	{	
        	if (Input.mouseReleased && GameVariables.mouseState == true) {
        				
        				e = sprWave;
        				e.x = Input.mouseX;
        				e.y = Input.mouseY;
        				e.play("red", true);
        				addGraphic(e);
        				redarray.push(e);
        			} 
          }

(Zachary Lewis) #2

You’ll want to check for mouse click in your game world and add them there.


(Enzo M) #3

I tried this, but how do you manage Spritemaps in the World ?

I was creating something like :

	public var onde:Wave = new Wave();
	public var e:Entity;
	public var s:Spritemap;
	public var redarray:Array = [];

	override public function update():void 
	{
		
		if (Input.mouseReleased && GameVariables.mouseState == true) {
			
			e = onde;
			s = onde.sprWave;
			s.x = Input.mouseX;
			s.y = Input.mouseY;
			s.play("red", true);
			s.scrollX = s.scrollY = 0;
			add(e);
			getType("e", redarray);
		}

But the problem is the same, each new thing “ecrase” the last one… By the way, I guess they are added to the array since I need one click before the others create entities. I guess that’s the first case of the array which is empty ?


(JP Mortiboys) #4

You’ve only got one instance of the Wave entity, so you’ll only ever have one on screen. Try this:

public class Wave extends Entity {
	[Embed(source="Assets/Wave[ANIM].png")]
	private const wave:Class;

	public var sprWave:Spritemap = new Spritemap(wave, 72, 72, casseToiSaleVagueDeMerde);

	public function Wave() {
		sprWave.add("red", [0, 1, 2, 3, 4, 5, 6, 7], 10, false);
		sprWave.add("blue", [8, 9, 10, 11, 12, 13, 14, 15], 10, false);
		graphic = sprWave;
		type = "onde";
	}

	override public function added():void {
		sprWave.play(FP.choose("red", "blue"));
	}

	// This is called when the animation has finished
	private function casseToiSaleVagueDeMerde():void {
		world.recycle(this);
	}

	public static function spawn(x:Number, y:Number):Wave {
		var newWave = world.create(Wave);
		newWave.x = x;
		newWave.y = y;
		return newWave;
	}
}

public class LeMondeDesVagues extends World {
	// ...

	override public function update():void {
		if (Input.mouseReleased) {
			Wave.spawn(Input.mouseX, Input.mouseY);
		}
	}
}

(Enzo M) #5

Nice ! Thanks you, it worked. I used what NotARaptor said, and everything is clear now so I could redo this later, thanks :slight_smile:

The only problem remainin about this is the setHitBox. It doesn’t follow the scrolling. I used graphic.scrollX = graphic.scrollY = 0 so the waves follow the scrolling, but the setHitBox function doesn’t have a scrollX/Y parameter.


(JP Mortiboys) #6

In Wave's constructor, add this line before the animation definitions:

public function Wave() {
    sprWave.scrollX = sprWave.scrollY = 0;
    // animations here...
}

(JP Mortiboys) #7

Also be aware the Input.mouseX (or Y) refer to the screen position, not accounting for the camera (scrolling). If you want the world position (including scrolling), use world.mouseX (and Y) instead.