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

2015/4/24

Setting a random background!

TheGoldenBucket TheGoldenBucket

2015/4/24

#
Hey everyone! I needed some help with this code. The return error is "cannot find symbol - method setImage(greenfoot.GreenfootImage) Here is the world's code:
import greenfoot.*;

/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends World
{
    public int img;
    public int age;

    /**
     * Constructor for objects of class GameOver.
     * 
     */
    public GameOver()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        imgset();
    }
    
    public void imgset()
    {
        age++;
        if (age == (1))
        {
            img = (Greenfoot.getRandomNumber(2));
            GreenfootImage backImage = null;
            if (img == (0))
            {
                backImage = new GreenfootImage("gameover1.png");
            }
            if (img == (1))
            {
                backImage = new GreenfootImage("gameover2.png");
            }
            if (img == (2))
            {
                backImage = new GreenfootImage("gameover3.png");
            }
            if (backImage != null) setImage(backImage);
        }
    }
}
davmac davmac

2015/4/24

#
The World class does not have a 'setImage' method. Check the API documentation and I'm sure you'll find the method you're looking for.
raza raza

2015/4/24

#
May be this will help you ...!!!
import greenfoot.*;
 
/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends World
{
    public int img;
    private int age=0;
 
    /**
     * Constructor for objects of class GameOver.
     * 
     */
    GreenfootImage ge1 = new GreenfootImage("images/B1.png");
    GreenfootImage ge2 = new GreenfootImage("images/B2.png");
    GreenfootImage ge3 = new GreenfootImage("images/B3.png");
    public GameOver()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        /*for(int i=0;i<=3;i++)
        {
            imgset();
        }*/
    }
    public void imgset()
    {
        //int n=0;
	age++;
        
        if (age == (n))
        {
            img = (Greenfoot.getRandomNumber(2));
            if (img == (0))
            {
              this.getBackground().drawImage(ge1,0,0);
            }
            if (img == (1))
            {
                this.getBackground().drawImage(ge2,0,0);
            }
            if (img == (2))
            {
                this.getBackground().drawImage(ge1,0,0);
            }
            
            //n++;
        }
           /*if (backImage != null) 
            {
                this.getBackground().drawImage(img,0,0);
            }*/
        }
    }

danpost danpost

2015/4/24

#
I do not understand the usage of the 'age' field in the GameOver world class; nor is there a need to have 'img' as an instance field in the class (its value does not need to be retained from one act cycle to the next). Actually the 'age' field does not really do anything -- the 'imgSet' method is only called once, where the value of the field will become one and the code inside the 'if' block will execute; that would be no different if you removed the 'if' condition from the code in the 'imgSet' method and removed the 'age' field altogether. Another thing is that you have 3 possible images that could be used; yet, you are only getting two possible random numbers. Zero and one are the only possible returned values when using 'Greenfoot.getRandomNumber(2)'.
TheGoldenBucket TheGoldenBucket

2015/4/24

#
Do you think it would be better if I just created an actor to run this code instead, considering that "setImage" can't be used in the world's class?
danpost danpost

2015/4/24

#
TheGoldenBucket wrote...
Do you think it would be better if I just created an actor to run this code instead, considering that "setImage" can't be used in the world's class?
No. I think you should find the appropriate method from the greenfoot.World API doumentation.
TheGoldenBucket TheGoldenBucket

2015/4/25

#
I just changed setImage to setBackground and it works. Thanks! -GoldenBucket PS: My game is nearing "completion"!
You need to login to post a reply.