This site requires JavaScript, please enable it in your browser!
Greenfoot back
Gazzzah
Gazzzah wrote ...

2012/2/26

Random and not repeating

Gazzzah Gazzzah

2012/2/26

#
I have written a spawning mechanism for my game. It spawns in waves of enemies when spawnWave() is called. I want the position for each spawned enemy to be random from a few set choices, but not repeating, so I don't end up with two enemies in the same place. Here is my current spawn mechanism:
public void spawnWave()
    {
        if (waveSpawnDelay == 0)
        {
            waveTime = 0; //not relevant
            waveSpawnDelay = 50; //not relevant
            wave ++; //adds to the wavecount
            updateWaveCount(); //displays new count, not relevant
            spawnPower = wave; //sets the total amount of spawnPower that can be used in making this wave
            possition = 1; //starts at position 1, to be replaced with a random number 1-4
            while (spawnPower > 0 && possition <= 4)
            {
                spawn();
            }
        }
    }
    public void spawn()
    {
        objectID = (Greenfoot.getRandomNumber(2)+1); //choose a random enemy to spawn, at the moment there are only 2, adds 1 onto it to avoid 0
        while (objectID > spawnPower)
        {
            objectID = (Greenfoot.getRandomNumber(2)+1); //get a new object if over the quota (spawnPower)
        }
        switch(possition) //the set positions for spawn (the corners of the arena)
        {
            case 1: xPlacement = 25; yPlacement = 50; break;
            case 2: xPlacement = 825; yPlacement = 650; break;
            case 3: xPlacement = 25; yPlacement = 650; break;
            case 4: xPlacement = 825; yPlacement = 50; break;
        }
        possition++; //add 1 so that next time it uses the next position ie, 1, then 2, etc
        switch(objectID)
        {
            case 1: addObject(new blade(wave), xPlacement, yPlacement); break; //enemy 1
            case 2: addObject(new lineTank(wave), xPlacement, yPlacement); break; //enemy 2
            //case 3: addObject(new ...(wave), xPlacement, yPlacement)); break; //these enemies haven't been made yet
            //case 4: addObject(new ...(wave), xPlacement, yPlacement)); break;
            //case 5: addObject(new ...(wave), xPlacement, yPlacement)); break;
            //case 6: addObject(new ...(wave), xPlacement, yPlacement)); break;
            //case 7: addObject(new ...(wave), xPlacement, yPlacement)); break;
            //case 8: addObject(new ...(wave), xPlacement, yPlacement)); break;
        }
        spawnPower -= objectID; //subtract, the power used to make the object (objectID), from spawnPower
        enemies ++; //add to the enemy count, not relevant
        addObject (new flash(), xPlacement, yPlacement); //a pretty puff, not relevant
    }
So I need to make "position" set randomly, that'd be done with "Greenfoot.getRandomNumber(4)+1;" but then I need to make it that next time spawn is called, it wont use previous values until a whole new wave is called. Your assistance is greatly appreciated.
Gazzzah Gazzzah

2012/2/26

#
I could do this with 4 booleans I guess, but I figure there might be an easier way. It's kind of messy to have a massive "while" statement consisting of "(position == 1 && used1) || (position == 2 && used2) || etc...
You need to login to post a reply.