Limiting the number of spawned items


(Nate ) #1

Hey guys, I have a game clock and every X amount of time, random items spawn.

I would like to limit the amount of spawned items but I am not sure how to do so. I have been messing around with variables that don’t seem to work exactly how I want it to.

I think I need to use the Clamp method, but am not sure how to do it.

Essentially, I want to spawn a bunch of the same pickup, randomly on the screen. However if the player isn’t picking up anything, I don’t want the game to keep spawning and spawning. Is there a way I can track how many entities are spawned within the code? Outside of the console of course.


How can I add enemies randomly?
(Justin Wolf) #2
world.classCount(Class);
world.typeCount("type");

Either one of these will work for you. Calling them will simply return an integer.

if (world.typeCount("Pickups") <= 10)
{
     // spawn more pickups until we reach 10
}

(Nate ) #3

Worked wonderfully! Thank justin!


(Nate ) #4

How would I go about making it so the rocks do not spawn and overlap one another?

	var r:int = 0;
		for (var i:int = 0; i < rockCount; i ++)
		{
			//Generate random X value
			r = FP.rand(512);
			FP.world.add(new theRock(r, 0));
		}

(Justin Wolf) #5

Not sure if this is what you mean, but the layer property of Entity is what I think you’re asking about.

FlashPunk’s layering system is very simple. An Entity with the a layer of 0 will render behind an Entity with the layer of -1. The lower your layer, the higher (couldn’t think of a better word?) it renders on stage. So, somewhere in your theRock class, you’ll have to determine what layer is appropriate for it.


(Nate ) #6

The layering is fine I meant on the X axis. How can I make it so the rocks do not overlap? I am generating their X values randomly. But for example of two X values are within a certain distance from each other I would like them to not overlap.


(christopf) #7

You could try to check for collision around the stone directly after added and move it based on the collision direction. I had a kinda problem with entities spawning on the grid but in my case i had the possibility to check for tiles if they solid.

maybe with something like FP.distance and world.nearestToEntity(“stonetyp”, newlyAddedStone)?


(Willard Torres) #8

You can have a variable called “spawnX”, and also a list or array of all the rocks in the scene. In a do while statement, you generate a new spawnX value. You then cycle through all entities in the array of rocks and check if their distance (Math.abs(rock.x - spawnX)) is less than a certain threshold. If it is, then generate a new spawnX.

This can cause an infinite loop, however, if the rocks are positioned in such a way that there is no possible x where its distance from any rock is > threshold.

I would post code but I have no clue on two format multiline, tabbed code here.


(Zachary Lewis) #9

(Willard Torres) #10

Thanks for that, and sorry for my ignorance.

Here is my take on it:

var spawnX: int;
var valid: Bool = true;
var threshold: int = 256; //Min distance between rocks
do{
    valid = true;
    spawnX = FP.rand(512);
    for(var rock: Rock in rocks){
        if(Math.abs(x - spawnX) <= threshold){
            valid = false;
            break;
        }
    }
} while(!valid);

Something like that, I guess.


(Nate ) #11

I like this solution. Line 7, where is ‘rocks’ coming from? Is that an array of the rocks? If so where / how should I set this up? This is how I am currently spawning them so I might have to do this differently.

    for (var i:int = 0; i < rockCount; i ++)
            {
                t = FP.rand(512);
                FP.world.add(new theRock(t, 0));
            }

(Willard Torres) #12

Oh, so you’ll be spawning them all in one go? In that case, my solution above can be tweaked for that.

And all you have to do is to declare an Vector containing rocks

var rocks: Vector<theRock> = new Vector();

And every time you spawn a new rock, you add it into the vector of rocks

var r: theRock = new theRock(yadda);
FP.world.add(theRock);
rocks.push(theRock);

Inside the for loop where you spawn the rocks, you can incorporate the one I posted above. There’s probably a better solution, though.


(Nate ) #13

Okay thanks I will look into trying this :smiley: