Randomly choosing Entities/ Spawn Times


(Gil) #1
public function enemySpawn():void
{
	var class:Class = FP.choose(Car, Boat, Plane);
	var entity:Entity = new class();
			
	add(entity);		
}

I try to call this function in my world’s update function, and I get a runtime error, its very frustrating. The error message is,

[Fault] exception, information=ArgumentError: Error #1063: Argument count mismatch on Car(). Expected 1, got 0. (Currently testing the code with only the Car entity, but for all intents and purposes (thank you jacobalbano) lets say that there exists a Boat and Plane entity).

If there is another method to randomly spawning from a pool of different entities, please enlighten me. Also an added bonus, I also want to spawn these entities at random times but also failed to do so. I am aware of the fact that Zach makes that possible in http://www.youtube.com/watch?v=vcfV1kovaRA. But I was also unsuccessful in randomizing entity spawns.

If there is a genius out there who can somehow successfully implement both random entity selection, and randomized(controlled) entity spawn rates, please I am in desperate need of your assistance!


(Bora Kasap) #2

check this out


(Gil) #3

I actually modeled my code from that example, close but no cigar! I got a runtime error


(Bora Kasap) #4

are you sure your Car() constructor doesn’t require any parameters?


(Sparrow) #5

I tried your code, using the variable name “class” gave errors, try renaming “class” to “c” or something like this:

var c:Class = FP.choose(Car, Boat, Plane);

This works when I compile it.             On a sidenote, the error you got is about Car not getting the parameters it is expecting in it’s constructor like AlobarNon said.


(Jacob Albano) #6

If naming your variable class is causing a compile error, renaming it to something that isn’t a reserved keyword would definitely help.

Your runtime error is related to the fact that your Car class takes one parameter in its constructor, so you need to pass it in:

var c:Class = FP.choose(Car, Boat, Plane);
var e:Entity = new c(param);

This might cause trouble if the constructors for each class you’re choosing between don’t match; for example, Car takes one string parameter, Boat takes two Numbers, and Plane takes nothing at all. You’ll want to make sure they all have the same parameter requirements before trying to switch between them like this.

Also, I’ve just got to be pedantic here:

It’s “intents and purposes” :stuck_out_tongue:


(Jacob Albano) #7

To randomize spawn times, try something like this:

public class Spawner extends Entity
{
    private var classes:Array;
    private var spawner:Alarm;

    public function Spawner(delay:Number, ...classes)
    {
        this.delay = delay;
        this.classes = classes;
    }

    override public function added():void
    {
        spawner = new Alarm(delay, onSpawn, LOOPING);
        world.addTween(spawner, true);
    }

    override public function removed():void
    {
        spawner.cancel();
    }

    private function onSpawn():void
    {
        var c:Class = FP.choose(classes);
        var e:Entity = new c();
        // set position, etc
        world.add(e);
    }
}

Use it like this:

// new spawner that adds a new entity every 5 seconds
world.add(new Spawner(5, Car, Boat, Plane));

(John Andersson) #8

I sometimes like to use Math.random :stuck_out_tongue:

var sum:int = Math.floor(Math.random() * 5)

If (sum == 5) //slap yourself

If (sum == 4) //slap me

If (sum == 3) //slap everyone

etc

It’s a pretty bad way, I guess.


(Jacob Albano) #9

It’s not too bad. The advantage to using Flashpunk’s random utility functions is that everything is seeded, so you can replicate the same results every time if you want. There’s also a lot less boilerplate code to worry about.


(Gil) #10

Okay, I’m actually not sure what you mean specifically by parameters because my entities’ constructor is very small. Here is the code for that:

public function Car(position:Point) 
{
	carSprite.add("stand", [0], 0, false);
			
	graphic = carSprite;
			
	x = position.x;
	y = position.y;
			
	name = "enemy1";
}

Update: Sorry, so I got it to work, by deleting the position: Point, and the x and y declarations for that from the constructor. It actually works like a charm and I was going to delete that code anyways to make a hitbox for my entity. Going to analyze/ try your code for random spawn times now, you’re the best!

Thank you to everyone else for chiming in as well, all input is noted and appreciated!


(Gil) #11

Here I tried to use the Spawner class you gave me but I got some syntax errors regarding the delay parameter, so I changed that to a variable. Its fixed for now. On the other hand however…

public class Spawner extends Entity
    {
    	private var classes:Array;
    	private var delay:Number;
    	private var spawner:Alarm;
    		
    	public function Spawner(classes:Array) 
    	{
    		this.delay = delay;
    		this.classes = classes;
    	}
    		
    	override public function added():void 
    	{
    		super.added();
    		spawner = new Alarm(delay, onSpawn, LOOPING);
    		FP.world.addTween(spawner, true);
    	}
    		
    	override public function removed():void
    	{
    		spawner.cancel();
    	}
    		
    	private function onSpawn():void
    	{
    		var c:Class = FP.choose(classes);
    		var e:Entity = new c();
    			
    		world.add(e);
    	}

I still also get an error here when I try to call on that class, I’m told "implicit coercion of value of type class to unrelated type Array.

public function randomEnemySpawn():void
{
    var Etime:Number; //Private variable time-step;
    var minTime:Number = 0.5; //Fastest spawn rate;
    var maxTime:Number = 2;  //Slowest spawn rate;
    			
    Etime += FP.elapsed;
    			
    if (Etime >= 0)
    { 
    	add(new Spawner(Car)); //Spawn when Timer is positive **Error here;
    				
    	Etime -= FP.random * (maxTime * minTime) + minTime;
        //Timer -= randomly .5 or 1.5 seconds;
    }	

}

Scratch all that, after massive headache I solved all of my issues on my own! Disregard this, and thanks again!


(Jacob Albano) #12

I used varargs in my constructor, but you used an array parameter.

public function Spawner(...classes)
{
    // call: new Spawner(Car, Boat, Plane);
    // classes is turned into an array: [Car, Boat, Plane]
}

Typing the parameter as an array and then passing a series of parameters won’t have the same result.