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

2020/5/12

The "if" statement is not working this instance

Top_Gerald Top_Gerald

2020/5/12

#
I am making a game where i will have to spawn (food)sources periodically and to perform this i have used if statements. I have used if statements throughout my game but for this one instance it fails to work. The solution is likely simple but I am failing to solve it. Below, i have included code that is most relevant.
    private static final int maxSources = 12;
    private static final int sourceSpawnTimeDelay = 3000;
    int sourceSpawnTime;
public Field()
    {    
        // Create a new world with 750x750 cells with a cell size of 1x1 pixels.
        super(750, 750, 1); 
        Greenfoot.setSpeed(50);

        ....

        sourceSpawnTime++;
        spawnSource();
    }
/**
     * Spawns sources while the game is running
     */
    public void spawnSource()
    {
        // if(getObjects(Source.class).size() <= maxSources)
        // {
            if(sourceSpawnTime >= sourceSpawnTimeDelay)
            {
                sourceSpawnTime = 0;
                int x = Greenfoot.getRandomNumber(getWidth());
                int y = Greenfoot.getRandomNumber(getHeight());
                addObject(new Source(), x, y);
            }
        // }
    }
If someone can help me i will be most thankful!
danpost danpost

2020/5/12

#
Top_Gerald wrote...
I am making a game where i will have to spawn (food)sources periodically and to perform this i have used if statements. I have used if statements throughout my game but for this one instance it fails to work. The solution is likely simple but I am failing to solve it. Below, i have included code that is most relevant. << Code Omitted >> If someone can help me i will be most thankful!
If you pause the scenario, right-click on the world background and select Inspect, you will see that the value of sourceSpawnTime is 1. This would tell you that the spawnSource method was called exactly one time. Indeed, that would be correct, because any instance of Field would be created exactly one time. It makes total sense when you realize you are calling the method from the constructor. The method must be invoked via an act method to be repeatedly called during program execution in greenfoot.
Jarvissss Jarvissss

2020/5/12

#
Hi , I think you have not intialise the sourceSpawnTime at the point of decleartion intialise it with 0 at the starting where you have first declared it. Regards, Aditya Lakhera
danpost danpost

2020/5/12

#
Jarvissss wrote...
I think you have not intialise the sourceSpawnTime at the point of decleartion intialise it with 0 at the starting where you have first declared it.
Sorry. All fields are automatically initialized (at least, within greenfoot). Please note that I did not say variables, but fields:
public class ClassExample
{
    /** primitive types (fields) */
    int a; // initialized to 0
    float b; // initialized to 0f
    // byte, long, char, short are all initialized to zero
    boolean c; // initialized to false
    
    /** non-primitive types (fields) */
    Object d; // initialized to null
    String e; // initialized to null
    Integer a2; // initialized to null
    Boolean c2; // initialized to null
    int[] f; // initialized to null

    public ClassExample()
    {
        /** all types (non-field variables) */
        int g; // not initialized
        String h; // not initialized
        // no non-field variables are automatically initialized
    }
}
Top_Gerald Top_Gerald

2020/5/12

#
Thank you danpost for the comprehensive reply! Also, thanks Jarvissss for the attempt. I managed to get the code working by putting a "public void act()" into my world class and putting the "sourceSpawn" in there.
You need to login to post a reply.