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

2018/3/19

Delay the entry of the first balls

R0mane R0mane

2018/3/19

#
Hey it's me again! I have one more problem with my game. So when the player clicks on start, the first two balls don't appear right away, only after ten seconds. Ten seconds later, two more balls appear and so on. My problem is that ten seconds without any ball at the beginning is too much, but we don't want the first balls to appear just when the player has clicked start, but like 3 or 5 seconds later. I have done something that kinda works, but it delays everything from 5 seconds, so that means the timer appears only after this time, the start button disappear also only after. I want the timer to start and the start button to disappear and THEN wait 5 seconds and then the balls appear. Here is my code for Background:
public class Background extends World
{
    Start start = new Start();
    Help help = new Help();
    Title title = new Title();
    Timer timer = new Timer();
    boolean isRunning;

    public void act(){
        if (Greenfoot.mouseClicked(start)){
            removeObject(title);
            removeObject(start);
            removeObject(help);
            startGame(); }
        if (isRunning){
            runBallSpawnTimer(); }
        if(getObjects(Player1.class).isEmpty()){
            endGame(); }
        setActOrder(Ground.class);
    }
    public void startGame(){
        this.addObject(timer, 300, 10);
        isRunning = true; }
    
    boolean empty = true;
    int BallSpawnTimer1;
    int BallSpawnTimer;
    int b = 0;
    int a = 0;
    long moveDelay;
    
    private void runBallSpawnTimer(){
        
        BallSpawnTimer = (BallSpawnTimer + 1)%500;
        
        if (a == 0){
            moveDelay = System.currentTimeMillis();
            a = 1;
        }
        while(empty == true && b == 0){
            if(System.currentTimeMillis() - moveDelay == 5000){
                
                spawnBall();
                b = 1;
            } 
        }
        if (BallSpawnTimer == 0 ) {

            if (!getObjects(Ball.class).isEmpty() /*|| empty == true*/){
                spawnBall();
            }
            empty = false;
        }
    }
}
danpost danpost

2018/3/19

#
Change '==' on line 41 to '>=' and report back any change.
R0mane R0mane

2018/3/20

#
It doesn't change anything
Vercility Vercility

2018/3/20

#
How does that even compile, you declare timer in line 27 but you never initialize a value before using it in Line 34
danpost danpost

2018/3/20

#
Vercility wrote...
How does that even compile, you declare timer in line 27 but you never initialize a value before using it in Line 34
Primitive types are automatically initialized with a default value. All numeric primitives (including char) are initially 0 and booleans are initially false. Actually, object references also have the initial default value of null. What I see with the code given is excessive use of fields and some codes that can be placed more reasonably. Line 19 should be placed in the constructor of the class (public Background()). The isRunning, a and b fields can be removed as just asking if timer is in the world is enough to figure out what to do when. Using system time can be replaced with Greenfoot.delay(600);, eliminating the need for two timer fields and the moveDelay field. With the changes, not including the change in the constructor, you get something like this:
public class Background extends World
{
    Start start = new Start();
    Help help = new Help();
    Title title = new Title();
    Timer timer = new Timer();
    int spawnTimer;
     
    public void act()
    {
        if (timer.getWorld() == null){ // not yet running
            if (Greenfoot.mouseClicked(start)){
                removeObject(title);
                removeObject(start);
                removeObject(help);
                addObject(timer, 300, 10);
                spawnTimer = 600; // for initial delay
            }
        }
        else (!getObjects(Player1.class).isEmpty()){ // running
            if (--spawnTimer == 0)
            {
                spawnBall();
                spawnBall();
                spawnTimer =  600;
            }
        }
        else if (getObjects(Gameover.class).isEmpty()){ // game over
            endGame();
        }
    }
}
Note: even the initial delay can be incorporated as above (without Greenfoot.delay(600);).
R0mane R0mane

2018/3/20

#
Thank you
Vercility Vercility

2018/3/20

#
danpost wrote...
Vercility wrote...
How does that even compile, you declare timer in line 27 but you never initialize a value before using it in Line 34
Primitive types are automatically initialized with a default value. All numeric primitives (including char) are initially 0 and booleans are initially false. Actually, object references also have the initial default value of null.
Is that greenfoot specific? Because Im pretty sure int values are null by default, the compiler will also throw this when trying to use an uninitialized value (A fresh array of ints will also contain null everywhere, doesnt it.)
xyz.java:5: error: variable x might not have been initialized
x = x+1;
    ^
1 error
public class xyz{

public static void main (String[] args) {
int x;
x = x+1;
}
}
danpost danpost

2018/3/20

#
Vercility wrote...
Is that greenfoot specific? Because Im pretty sure int values are null by default, the compiler will also throw this when trying to use an uninitialized value (A fresh array of ints will also contain null everywhere, doesnt it.) << Code blocks omitted >>
The codes you gave are showing local variables being declared. That is not the same as the aforementioned instance field declared at line 27. Also, an array, regardless of the type of elements it will hold, is an object and an array field is an Object reference and therefore is null when declared and not assigned an array instance to reference. When creating a new int array instance (whether to be assigned to the array field or not) all elements will be initially set to 0.
Vercility Vercility

2018/3/20

#
danpost wrote...
Vercility wrote...
Is that greenfoot specific? Because Im pretty sure int values are null by default, the compiler will also throw this when trying to use an uninitialized value (A fresh array of ints will also contain null everywhere, doesnt it.) << Code blocks omitted >>
The codes you gave are showing local variables being declared. That is not the same as the aforementioned instance field declared at line 27. Also, an array, regardless of the type of elements it will hold, is an object and an array field is an Object reference and therefore is null when declared and not assigned an array instance to reference. When creating a new int array instance (whether to be assigned to the array field or not) all elements will be initially set to 0.
Makes sense. Thanks
You need to login to post a reply.