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/2

#
how can i add a random number of actors while the game is running?
danpost danpost

2012/6/2

#
You will probably need to use the following:
for (int i = 0; i < Greenfoot.getRandomNumber(MAX_ACTORS + 1 - MIN_ACTORS) + MIN_ACTORS; i++) 
With MAX_ACTORS and MIN_ACTORS being the maximum and minimum numbers of those actors allowed to spawn at one time. With the limited amount of information you supplied, that is about all I can help you with. For more assistance, include the code that would trigger the spawnings and where it is located (at least). Be specific and complete.
nadouda nadouda

2012/6/2

#
I just asked the question in the other discussion thank u for your help
nadouda nadouda

2012/6/2

#
okay i'm still facing the same problem the game is about shooting birds so i want a random number of birds to pass after each interval of time from one side of the world to the other , i have put this code but nothing is happening only the gun is available public Space() { super(600, 400, 1); addObject(new Gun(), 300,200); if ((System.currentTimeMillis() - beginTime) / 1000 >= 40) for (int i = 0; i < Greenfoot.getRandomNumber(MAX_ACTORS + 1 - MIN_ACTORS) + MIN_ACTORS; i++) addObject(new Bird(), 0, Greenfoot.getRandomNumber(400)); }
danpost danpost

2012/6/2

#
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));
    }
}
nadouda nadouda

2012/6/2

#
It worked but can i control the speed of the bird? thank u very much i'm new to this program and i don't know much about it.
danpost danpost

2012/6/2

#
What do you have for the Bird class so far?
nadouda nadouda

2012/6/2

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Birds here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class Bird extends Actor
{
    private int count = 0;
    /**
     * Act - do whatever the Birds wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        move();
        checkCount();
        checkMouse();
        checkOut();
        if(getOneIntersectingObject(Bird.class) != null)
            {
                Actor bird = getOneIntersectingObject(Bird.class) ;
                getWorld().removeObject(bird); 
                getWorld().removeObject(this);
                return;
            }  
            
    }  
        public void move()
    {
        setLocation(600,Greenfoot.getRandomNumber(400));
        
    }
     public boolean atWorldEdge()
    {
        if(getX() == 600 && (getY()==Greenfoot.getRandomNumber(400)))
            return true;
        else
            return false;
    }
    public boolean isMoving(){
        boolean move = true;
        if(move)
            move();
        return move;
    }
    public void checkMouse(){
          if (Greenfoot.mouseClicked(Bird.class)){
            getWorld().removeObject(this);
            Greenfoot.playSound("explosion-02.wav");
        }
    }
    private void checkOut()
        {
           if(atWorldEdge()){
              getWorld().removeObject(this);
                }
            count++;
            Greenfoot.playSound("fail-buzzer-04.wav");

            }
    public void drawString(){
        getImage().setColor(Color.red);
        getImage().drawString("You lost!!!", 300,200); 
    }    
    public void checkCount()
        {
            if(count>3){
                Greenfoot.stop();
                drawString();
                Greenfoot.playSound("fail-trombone-03.wav");
            }
    }
}
I didn't set anything to the direction yet.. this is my class i know it may have so many errors.
danpost danpost

2012/6/2

#
OK, you add the Bird objects on the left side of the window and (let me guess) they fly across to the right side whilst the player tries to shoot them down (by clicking on them). If three make it to the right side, you lose. I do see multiple problems with the code you provided, which should be fixed before moving on. You should always fix the code you have before adding more code (or a tangled web you will weave). We will look at your method (other than the act method) first: = move(): the setLocation statement says to randomly pick a location one pixel off the right side of the screen (Somehow, it posted right then, not intended). Anyway, the 600 is outside the world bounds, and any hard-coded number will have the bird jumping up and down along a line because of the random number choosing of the y-coordinate. What you probably want is something like 'setLocation(getX() + 2, getY() + getRandomNumber(3) - 1);'. This will give the bird a constant speed (for now, we will replace the 2 with a variable that can change later), and give it some random up and down (for now, as this will nearly end up to be a straight line across the screen). Will continue on new post (as it would make it easier for me to see what code you had already).
nadouda nadouda

2012/6/2

#
it's true the bird is goig up and down at the right side.. i changed the code and now when i compile a certain number of birds are available after that 2 of them move little distance
danpost danpost

2012/6/2

#
= atWorldEdge(): this method, as is, will always return 'false' as getX() will never return 600, not to mention, that the random chance of a match in the y-coordinate is 1 in 400 (or one-quarter of a one percent chance). What you probably want here is 'if (getX() == 0 || getX() == getWidth() - 1 || getY() == 0 || getY() == getHeight() - 1)'. = isMoving(): this method is not being used anywhere in this class (although, all your methods appear to be 'public', and this one could be called from somewhere else). I am not sure what this method was intended to do, but the returned boolean will always be 'true'; so calling this method is equivalent to calling 'move()' in this class. = checkMouse(): the method used in the first line of the code in this method (Greenfoot.mouseClicked(Object obj)) uses an object as its parameter, not a class. Here, you want 'if (Greenfoot.mouseClicked(this))'. = checkOut(): first method that looks OK, so far. But, this is also where you want to call the checkCount method. Right after the line 'count++;', add the line 'checkCount();' (and remove the 'checkCount();' line from the act method). The placement of the line is strategic, in that you only check the count when it changes. = drawString(): when you use 'getImage', you are returned the image of the bird that was just removed from the world. One thing, is that the image (I would think) would be fairly small; probably, too small for even the text to show completely on it; and most probably too small for your intent. Even after adjusting the size of the image and the font of the text, you would have to add the actor back into the world, and reset its location (cause right now, the new image would be half cut by the right edge of the world). Really, this is no longer a bird at all. What you should do, is create a new sub-class of actor to hold the image you want to display. You could create the image with a graphics editor or by program code; whatever. Then, instead of calling 'drawString()', you would just use 'getWorld().addObject(new Loser(), 300, 200);'. = checkCount(): I am not sure if it makes any difference, but (at least for looks) the 'Greenfoot.stop()' statement should be the last statement in the 'if' block. (I do think the method completes after that statement is encountered). OK, those are the methods (other than act). My next post will deal with the act method.
danpost danpost

2012/6/2

#
From what you are saying, it almost sounds like your speed slider is a 100. Wherever it is, set it to about 50 for now.
danpost danpost

2012/6/2

#
= act(): you should (at this point) have 3 method calls ('move', 'checkMouse', and 'checkOut') and an 'if' block in this method. I am not sure if this is what you intended, so I am stating what you have as far as the 'if' block. It says: if two birds collide with each other, remove both of them. If this is not what you wanted, post what you did want. Next, the two methods, 'checkMouse' and 'checkOut', both could possibly remove the bird from the world. If the first one does remove the bird, the second one, when calling getWorldEdge, will fail with a run-time error (trying to use the location of an actor while not in the world). The 'if' block also uses the location of the bird in the world (with getOneIntersectingObject) and will fail if either of the previous methods removes the bird. To avoid the errer we have to check and make sure the bird is still in the world. So, the code inside the act method up to the given 'if' statement should look like this:
move();
checkMouse();
if (getWorld() != null) checkOut();
if (getWorld() != null && getOneIntersectingObject(Bird.class) != null)
nadouda nadouda

2012/6/2

#
Everything is okay now but the speed is still fast.. the mouse is slow and the bird is too fast so the user cant follow it
danpost danpost

2012/6/2

#
What does it look like now? (the Bird class)
There are more replies on the next page.
1
2