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

2020/6/14

Trying to spawn actors at different times

1
2
3
4
kirajim kirajim

2020/6/14

#
I am making a flappy bird came and i want my coins to spawn at different time throughout the game i have the code and there is no error but it doesn't seem to be working or doing anything. Can someone explain why it isn't
private int spawnTimer;

private void checkForSpawning() // call from act method
{
    spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
    if (spawnTimer == 0) // at each timer reset
    {
        addObject(new Coin(), 150, Greenfoot.getRandomNumber(1));
    }
}
working
danpost danpost

2020/6/14

#
kirajim wrote...
I am making a flappy bird came and i want my coins to spawn at different time throughout the game i have the code and there is no error but it doesn't seem to be working or doing anything. Can someone explain why it isn't working << Code Omitted >>
As long as you call the method unconditionally from act and the scenario speed is at 50, a coin should spawn every 10 seconds along the top edge a few inches from the left corner.
kirajim kirajim

2020/6/14

#
Ya thats the problem i know that the code is correct but no coin is appearing i waited for a whole minute. The speed is at 50, but i am not sure what you mean by "call the method unconditionally from the act".
danpost danpost

2020/6/14

#
kirajim wrote...
Ya thats the problem i know that the code is correct but no coin is appearing i waited for a whole minute. The speed is at 50, but i am not sure what you mean by "call the method unconditionally from the act".
Show entire class code that above method is in.
kirajim kirajim

2020/6/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Flappy_Bird_Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Flappy_Bird_Background extends World
{
    int pipeCounter = 0;
    int flappyCounter = 0;
    int score = 0;
    int FIRST_PIPE = 240;
    int PIPE_SPACE = 60;
    int Score1 = 0;
    int NextLevel = 0;
    int LevelUp = 10;
    int WhenToLevelUp = 9;
    private int spawnTimer;
    Coin[] coins;
    /**
     * Constructor for objects of class Flappy_Bird_Background.
     * 
     */
    public Flappy_Bird_Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);

        // set paint order
        setPaintOrder(bottom_pipe. class, top_pipe.class);
        
        // Create a Score object
        
        // Add Score to Flappy_Bird_Background
        prepare();

}


    public void act() {
        pipeCounter++;
        if (pipeCounter % 100 == 0) {
            // Create a pipe object
           bottom_pipe bpipe = new bottom_pipe();
            
           top_pipe tpipe = new top_pipe();
            
           GreenfootImage bimage = bpipe.getImage();
           GreenfootImage timage = tpipe.getImage();
           
           if (Score1 == 0) {
               System.out.println("Can you be named the Flappy Bird LEGEND?");
               Score1 = 1;
            }
           if (Score1 == 10) {
               System.out.println("Good Job!");
               Score1 = 0;
            }
           if (Score1 >= WhenToLevelUp) {
               PIPE_SPACE = 70;
           }
           
           if (Score1 == 20) {
               System.out.println("So Far So Good!");
               Score1 = 0;
            }
           if (Score1 >= WhenToLevelUp + 10) {
               PIPE_SPACE = 80;
            }
            
           if (Score1 == 30) {
               System.out.println("Are you the Flappy Bird GOAT?");
               Score1 = 0;
            }
           if (Score1 >= WhenToLevelUp + 20) {
               PIPE_SPACE = 90;
            }
            
           if (Score1 == 40){
               System.out.println("Warning: Difficulty HALL OF FAME!");
               Score1 = 0;
            }
           if (Score1 >= WhenToLevelUp + 30) {
               PIPE_SPACE = 100;
            }
            
           if (Score1 == 50) {
               System.out.println("Flappy Bird LEGEND!");
               Score1 = 0;
            }
           if (Score1 >= WhenToLevelUp + 40) {
               PIPE_SPACE = 110;
            }
            
           if (Score1 == 51) {
               System.out.println("Warning: Level Impossible!");
            }
            
           addObject(bpipe, getWidth(), getHeight()/2 + bimage.getHeight()-PIPE_SPACE);
           addObject(tpipe, getWidth(), getHeight()/2 - timage.getHeight()+PIPE_SPACE);
        }
        
        if (pipeCounter >= FIRST_PIPE) {
            if (flappyCounter % 100 == 0) {
                score++;
                NextLevel++;
                Score1 = score;
            }
            flappyCounter++;
        }
        if (NextLevel == LevelUp) {
            displayLevelCompleted();
            NextLevel = 0;
        }
        
    }
    
    private void displayLevelCompleted() {
        System.out.println("NEXT LEVEL");
    }
    
   
     private void prepare()
    {
        coins = new Coin[12];
        for(int i=0; i<coins.length; i++)
        {
            coins[i]= new Coin();
            int flyX = Greenfoot.getRandomNumber(getWidth());
            int flyY = Greenfoot.getRandomNumber(getHeight());
            addObject(coins[i], flyX, flyY);
    }
    

}

private void checkForSpawning() // call from act method
{
    spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
    if (spawnTimer == 0) // at each timer reset
    {
        addObject(new Coin(), 150, Greenfoot.getRandomNumber(1));
    }
}
}
danpost danpost

2020/6/15

#
I do not see where checkForSpawning is called from.
kirajim kirajim

2020/6/15

#
so would I just write private int checkForSpawning
kirajim kirajim

2020/6/15

#
Ok so I wrote the method and for some reason it still isnt showing and i dont know where the problem is from are you sure the code is write?
danpost danpost

2020/6/15

#
kirajim wrote...
so would I just write private int checkForSpawning
No.
checkForSpawning();
(in act method)
kirajim kirajim

2020/6/15

#
oh ok it finally worked thank you so much!! But do you know how to make the coins appear at different places? and not the same place.
danpost danpost

2020/6/15

#
kirajim wrote...
oh ok it finally worked thank you so much!! But do you know how to make the coins appear at different places? and not the same place.
Increase the "1" on line 144.
kirajim kirajim

2020/6/15

#
oh makes sense. Thank you so much i really appreciate your help
kirajim kirajim

2020/6/15

#
One last question how can the time between the 2 coins decrease like right now its 10 sec but i want new coins to appear every 5 seconds.
kirajim kirajim

2020/6/15

#
And what code should be used if i want the coin to disappear when the bird touches it?
danpost danpost

2020/6/15

#
kirajim wrote...
One last question how can the time between the 2 coins decrease like right now its 10 sec but i want new coins to appear every 5 seconds.
Decrease the "600" in line 141.
kirajim wrote...
And what code should be used if i want the coin to disappear when the bird touches it?
if (isTouching(Coin.class)) removeTouching(Coin.class);
(in bird class)
There are more replies on the next page.
1
2
3
4