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

2012/6/2

Adding actors

1
2
nadouda nadouda

2012/6/3

#
public class Bird extends Actor
{
    private int count = 0;
    public void act() 
    {
        move();  
        checkMouse();  
        if (getWorld() != null) 
            checkOut();  
        if (getWorld() != null && getOneIntersectingObject(Bird.class) != null){
                Actor Bird = getOneIntersectingObject(Bird.class) ;
                getWorld().removeObject(Bird); 
                getWorld().removeObject(this);
                return;
            } 
    }  
        public void move()
    {
        setLocation(getX() + 2, getY() + Greenfoot.getRandomNumber(3) - 1);
        
    }
     public boolean atWorldEdge()
    {
        if(getX() == 0 || getX() == (getWorld().getWidth() - 1) || getY() == 0 || getY() == (getWorld().getHeight() - 1))
            return true;
        else
            return false;
    }
    public void checkMouse(){
          if (Greenfoot.mouseClicked(this)){
            getWorld().removeObject(this);
           
        }
    }
    private void checkOut()
        {
           if(atWorldEdge()){
              getWorld().removeObject(this);
                }
            count++;
            checkCount();
            

        }
    public void checkCount()
        {
            if(count>3)
                Greenfoot.stop();
           
    }
}
Sidstar Sidstar

2012/6/3

#
danpost wrote...
You put the 'if' statement that checks the time in the Space constructor? The world constructor only runs once, when you create the world. The 'if' statement there will never be run 40 seconds after the world was created! Try this:
import greenfoot.*;

public class Space extends World
{
    static final int MAX_BIRDS = 10; // adjust this value
    static final int MIN_BIRDS = 5; // adjust this value
    static final int INTERVAL= 40;
    Long beginTime = System.currentTimeMillis();

    public Space()
    {
        super(600, 400, 1);
        addObject(new Gun(), 300, 200);
        addRandomBirds();
    }
        
    public void act()
    {
        if ((System.currentTimeMillis() - beginTime) / 1000 >= INTERVAL)
        {
            addRandomBirds();
            beginTime = System.currentTimeMillis();
        }
    }
    
    private void addRandomBirds()
    {
        for (int i = 0; i < Greenfoot.getRandomNumber(MAX_BIRDS + 1 - MIN_BIRDS) + MIN_BIRDS; i++)
            addObject(new Bird(), 0, Greenfoot.getRandomNumber(400));
    }
}
Where do you put this code.. Sorry I am just a starter. SOrry
nadouda nadouda

2012/6/3

#
in the world class
Sidstar Sidstar

2012/6/3

#
So is this How to add actors in the middle of your game Can you please make a quick video on how to arrange it. Do you need to code top post and bottom post?
danpost danpost

2012/6/3

#
The bottom post is the code for a world that starts with a gun, and adds a random number of birds to it every 40 seconds. The top post is the code for the Bird class, which moves the birds across the screen toward the right, checking for collisions and (right) edge of world. Also, if four birds make it across the screen, the game ends. I noticed one more problem with the counting of birds making it across the screen. The count variable has to be a 'class' variable, not an 'instance object' variable. As an instance object variable, each bird that makes it across the screen will increment its own counter to 1, and no counter will ever make it to 2; nonetheless, 3 or 4. In your first post above (in the Bird class code), change line 3 (private int count = 0;) to 'static int count = 0;'. This makes it a class variable (the one and only counter, no matter how many birds are created).
You need to login to post a reply.
1
2