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

2018/8/28

Limiting Object Spawn

horde23242526 horde23242526

2018/8/28

#
Hello, I have only started using Greenfoot recently and was wondering if anyone would be able to help me out with some coding for object spawning. Currently in my game, I have a playable character (Spy) that can pick up a stationary 'egg'. When this happens, I want the game to add 5 enemies I have named 'Alien1's in random locations throughout the world. This is the code I have currently in the egg class, however once the playable character picks up the egg, the 'Alien1's just continuously begin spawning. How can I change this code to put a limit on the number of 'Alien1's that are added into the world? Thank you in advance
/**
     * this is the method whch tells the world to spawn aliens
     */
    public void act() 
    {
        if(isTouching(Spy.class))
        {
            Spy c = (Spy) getOneIntersectingObject(Spy.class);
            setLocation(c.getX(),c.getY());
            createNewAlien1();
        }
    }    

    private void createNewAlien1()
    /**
     * once the egg has been located and picked up, 5 Alien1's will spawn as an added threat
     */
    {
        Alien1 newAlien1;
        newAlien1 = new Alien1();
        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newAlien1, x, y);
    }
}
danpost danpost

2018/8/28

#
You could count the number of aliens spawned. That is, you can add an int field to the Egg class to track the number of aliens each egg spawns:
// field
private int aliensSpawned;

// in act (replacing line 10 above)
if (aliensSpawned < 5) createNewAlien1();

// in createNewAlien1 method
aliensSpawned++;
horde23242526 horde23242526

2018/9/3

#
Apologies for the late reply, it is working great now thanks so much!
horde23242526 horde23242526

2018/9/3

#
I also have a counter in my game that I want to increase by 5 points every time an 'AreaGuard' is hit with a bullet (fired by the Spy) and removed from the world. This is my current code for the AreaGuards in regards to this issue.
private Counter counter; // this is a reference to the counter in the AreaGuard class

    public AreaGuard(Counter pointCounter)
    {
        counter = pointCounter;
    }

//the code that tells the world to remove the AreaGuard if it is hit by a bullet
        if (isTouching(Bullet.class)) getWorld().removeObject(this);

danpost danpost

2018/9/4

#
Just increase the counter at the same time as you remove the AreaGuard object from the world.
horde23242526 horde23242526

2018/9/4

#
Like this?
 if (isTouching(Bullet.class)) getWorld().removeObject(this); counter.add(5);
I currently have this code in my game but it will not run.
danpost danpost

2018/9/4

#
horde23242526 wrote...
Like this? << Code Omitted >> I currently have this code in my game but it will not run.
You need to group the commands together into one if block. Where did you put that line within the class?
horde23242526 horde23242526

2018/9/4

#
In this section
public void act() 
    {
        move(3);
        turnAtEdge();
        tryToEatSpy();
        //the code that tells the world to remove the AreaGuard if it is hit by a bullet
        if (isTouching(Bullet.class)) getWorld().removeObject(this); 
        

    }    
danpost danpost

2018/9/5

#
horde23242526 wrote...
In this section << Code Omitted >>
Looks good. Add the counter command back in as suggested in my last post.
horde23242526 horde23242526

2018/9/6

#
Ok so this is the my code currently in the AreaGuard class. It says it is compiled and there are no errors but whenever the AreaGuard gets hit by a bullet the game stops. Once I press 'run' again, the game continues but no points have been added to the counter.
public void act() 
    {
        move(3);
        turnAtEdge();
        tryToEatSpy();
        //the code that tells the world to remove the AreaGuard if it is hit by a bullet
        if (isTouching(Bullet.class)) 
        {
            getWorld().removeObject(this); 
            counter.add(5);
        }
    }    
danpost danpost

2018/9/7

#
It probably has something to do with the code in your World subclass.
You need to login to post a reply.