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

2015/8/10

Need Help With The Following

Nyaru Nyaru

2015/8/10

#
I'm trying to place fly's in the worlds using a buffer amount based on the fly s image width. This is what I have so far:
import greenfoot.*;

public class BugWorld extends World
{
    //Fields for values that can be used anywhere in the class. Feel free to play with these!
    private final int NUMBER_OF_Flies = 5;
    private final int NUMBER_OF_SPIDERS = Greenfoot.getRandomNumber(50);
    private static final int WORLD_WIDTH = 600;
    private final static int WORLD_HEIGHT = 400;
   
    private GreenfootImage spider;
   
    /**
     * Constructor for objects of class BugWorld.
     */
    public BugWorld()
    {
        super(WORLD_WIDTH, WORLD_HEIGHT, 1);
        
        //Instantiate the spider GreenfootImage
        GreenfootImage drawedImage = new GreenfootImage("spider.png");
                
        drawSpidersOnBackground();
        PopulateWorldWithFlies();
        
    }

    private void drawSpidersOnBackground()
    {
        int buffer = spider.getWidth();
        int x,y;
        
        int count = 0;
        while(count < NUMBER_OF_SPIDERS)
        {
            x= Greenfoot.getRandomNumber(getWidth()-(2*buffer)) + buffer;
            y = Greenfoot.getRandomNumber(getHeight()-(2*buffer)) + buffer;
            drawSpiderAt(x,y);
            count++;
        }
    }
    
    private void PopulateWorldWithFlies()
    {
        int count = 0;
        while(count < NUMBER_OF_Flies)
        {
            placeFly();
            count++;
        }
    }
    
    private void drawSpiderAt(int x, int y)
    {
        //Turn the spider image 90 degrees clockwise from its current orientation
        spider.rotate(90);
        
        //Make the spider image a random transparency value between 20 and 200 
        spider.setTransparency(Greenfoot.getRandomNumber(181)+20);
        
        getBackground().drawImage(spider, x, y);
    }
    
  
    public void placeFly()
    {
        //Create variables that will only be available in this method
        int x ,y; 
        int buffer;
        int turnAmount = Greenfoot.getRandomNumber(360) - 180; //range: -180 to 179
        
        //Create a Fly actor to place in the world
        Fly fly = new Fly();
        
        
       buffer = Fly.getImage().getWidth();
        //Set the x and y coordinates for placing the fly in a random location in the world
        x = Greenfoot.getRandomNumber(getWidth()-(2*buffer)) + buffer;
        y = Greenfoot.getRandomNumber(getHeight()-(2*buffer)) + buffer;
        
        //Add the fly to the world
        addObject(fly,x,y);
        
        //Turn the fly by the random amount calculated earlier
        fly.turn(turnAmount);
    }
}
danpost danpost

2015/8/10

#
You know -- I see all sorts of things being done to 'spider'. Yet, nowhere do I see anything that starts with
spider = ???
Francis1004 Francis1004

2015/8/10

#
dan? will you help me out?
danpost danpost

2015/8/10

#
danpost wrote...
You know -- I see all sorts of things being done to 'spider'. Yet, nowhere do I see anything that starts with
spider = ???
Maybe line 21 should be that line.
Nyaru Nyaru

2015/8/13

#
Ah ok made that change but I keep getting the error at line 30: non-static image method getImage() cannot be referenced from a static content
Super_Hippo Super_Hippo

2015/8/13

#
Are you sure that it is at line 30? I guess that line 76 would cause that error.
buffer = Fly.getImage().getWidth();
Change 'Fly' to 'fly'.
Nyaru Nyaru

2015/8/13

#
It worked! I hate spelling errors thanks for the help!
You need to login to post a reply.