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

2016/11/2

Enemy not spawning with current method, no syntax but no spawn

JWK3986 JWK3986

2016/11/2

#
No syntax error* I am trying to spawn an actor at the right edge of a 780x360 world 20% of the time or so and thought I was using a method that would work, but nothing spawns no matter how long i run the scenario. I am using a custom method written in my world code and called after the prepare method in the World act method. What am I doing wrong? This is my World code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(780, 360, 1, false); 

        
        prepare();
        spawnPuffer();

    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        // lines for adding the ground as the world scrolls
        Actor sand = new Sand();
        addObject(sand, 0, getHeight()-sand.getImage().getHeight()/2);
        addObject(new Sand(), sand.getImage().getWidth(), sand.getY()); 

        

        // code for adding big coral
        BigCoral bigcoral = new BigCoral();
        addObject(bigcoral,141,267);
        bigcoral.setLocation(141,291);
        bigcoral.setLocation(128,303);
        BigCoral bigcoral2 = new BigCoral();
        addObject(bigcoral2,692,262);
        bigcoral2.setLocation(710,304);
        BigCoral bigcoral3 = new BigCoral();
        addObject(bigcoral3,430,261);
        bigcoral3.setLocation(414,275);
        bigcoral2.setLocation(710,309);
        bigcoral.setLocation(101,294);

        // code for adding little coral
        LittleCoral littlecoral = new LittleCoral();
        addObject(littlecoral,589,308);
        LittleCoral littlecoral2 = new LittleCoral();
        addObject(littlecoral2,327,314);
        LittleCoral littlecoral3 = new LittleCoral();
        addObject(littlecoral3,197,314);
        LittleCoral littlecoral4 = new LittleCoral();
        addObject(littlecoral4,37,312);
        LittleCoral littlecoral5 = new LittleCoral();
        addObject(littlecoral5,517,311);
        LittleCoral littlecoral6 = new LittleCoral();
        addObject(littlecoral6,779,324);

        Goldfish goldfish = new Goldfish();
        addObject(goldfish,72,153);
    }

    /**
     * Method to spawn Puffer at right edge at a random  Y coordinate 50% of the time
     */

    public void spawnPuffer()

    {
        if(Greenfoot.getRandomNumber(100) <20)
        { 
           
            Puffer puffer = new Puffer();
            addObject(new Puffer(),765,(Greenfoot.getRandomNumber(360)));
            
            

        } 

    }

}
danpost danpost

2016/11/2

#
Line 23, which calls the randomly spawning method, is in the constructor of the world. It is executed one time, when the world is created. So, you may have one spawn one out of five resets of the project. If you want the method to executed continuously during the running of the project, it needs to be called from an 'act' method (the World class has one, similar to that of the Actor class; it is just not included in the class skeleton).
JWK3986 JWK3986

2016/11/2

#
Ok, I see where I went wrong. I cut Line 23, created an act method in my World class and added the spawnPuffer method to it, and adjusted the If statement condition in order to slow down the spawning rate since at 20% it seemed ridiculously fast. I also commented out line 83 and it works as intended still, that line may have been redundant. Thanks for the help, once again lol.
You need to login to post a reply.