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

2018/3/17

Problem with ball spawning

Aqurila Aqurila

2018/3/17

#
Hello, I have a if condition that when the start button is clicked, some things of my code have to start. I also wanted to start the timer which is spawning the balls as soon as I click the start button. But if I put it into the condition, it doesn't work. Here the code:
    public void act(){
        if (Greenfoot.mouseClicked(start)){

            startGame();
            runBallSpawnTimer();

        }
    }

    public void startGame(){
        Timer timer = new Timer();
        this.addObject(timer, 300, 10);

    }
    boolean empty = true;
    int BallSpawnTimer;
    private void runBallSpawnTimer(){
        BallSpawnTimer = (BallSpawnTimer+1)%1500;
        if (BallSpawnTimer == 0) {
            
            if (!getObjects(Ball.class).isEmpty() || empty == true){
                spawnBall();
            }
            empty = false;
        }

    }
Whether I put runBallSpawnTimer() in the if condition or in the startGame() method, none of them make it work. As soon as I put the runBallSpawnTimer() outside of the if like this:
public void act(){
        if (Greenfoot.mouseClicked(start)){

            startGame();
            

        }
        runBallSpawnTimer();
    }
It works but well the code is being executed before I pressed start. Thanks for the help! :)
danpost danpost

2018/3/17

#
What is the 'start' object that is to be clicked?
Game/maniac Game/maniac

2018/3/17

#
The problem is caused because the runBallSpawnTimer method has to be called every frame for it to work, however the Greenfoot.mouseClicked method only returns true each time the target object has been clicked, so the timer will only iterate once per start button click. To fix this, you need to record the current state of the game, so when the game is running the timer will execute every act cycle. You can accomplish this by adding a boolean which records the game state, you could call this 'isRunning'. When the start button is clicked isRunning should be set to true, then you should execute the runBallSpawnTimer method if isRunning is true.
boolean isRunning = true;

public void act()
{
    if (Greenfoot.mouseClicked(start))
    {
        startGame();
    }
    if (isRunning)
    {
        runBallSpawnTimer();
    }
}
Aqurila Aqurila

2018/3/17

#
Start is just an actor which is removing some things when it is clicked.
Aqurila Aqurila

2018/3/17

#
I have tried you code Game/maniac.. For some reasons it is still not working. Here are the changes that I have made:
boolean isRunning; public void act(){ if (Greenfoot.mouseClicked(start)){ startGame(); if (isRunning) { runBallSpawnTimer(); } } if(getObjects(Player1.class).isEmpty()){ endGame(); }
and:
public void startGame(){
        Timer timer = new Timer();
        this.addObject(timer, 300, 10);
        isRunning = true;
    }
Aqurila Aqurila

2018/3/17

#
Aqurila wrote...
I have tried you code Game/maniac.. For some reasons it is still not working. Here are the changes that I have made:
boolean isRunning;
    public void act(){
   
        if (Greenfoot.mouseClicked(start)){

            startGame();
           
            if (isRunning)
            {
                runBallSpawnTimer();
            }
        }

        if(getObjects(Player1.class).isEmpty()){
            endGame();
        }
and:
public void startGame(){
        Timer timer = new Timer();
        this.addObject(timer, 300, 10);
        isRunning = true;
    }
Game/maniac Game/maniac

2018/3/17

#
You have put 'if (isRunning)' inside 'if (Greenfoot.mouseClicked(start))', however it must come after 'if (Greenfoot.mouseClicked(start))'
You need to login to post a reply.