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

2015/2/11

planet get

gr8n8 gr8n8

2015/2/11

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

/**
 * Write a description of class Planet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Planet extends Actor
{

    public int getValue() 
    {
        int x = Greenfoot.getRandomNumber(100);

        return x;
    }

    public void getPlanet()
    {
        int a = getValue();

        if (a>0 && a<30)
        {
            setImage("button-green.png");
        }
        if (a>30 && a<50)
        {
            setImage("button-red.png");
        }
        if (a>50 && a<70)
        {
            setImage("button-blue.png");
        }

    }

    public void act()
    {
        this.getPlanet();
    }
}
I am trying to set a random number and then based off of that number change the number of the planet, previous problems include: instead of staying at one image it would flash through the options I gave it, instead of staying as one... pls help
gr8n8 gr8n8

2015/2/11

#
*change the image for the planet
Super_Hippo Super_Hippo

2015/2/11

#
It's changing all the time because you call the method from the act method. If you call it from the constructor instead, the image is set once. So remove the act method and add this:
public Planet()
{
    getPlanet();
}
Btw, if the random number is 30, 50, 70 or higher, no image is set.
danpost danpost

2015/2/11

#
The 'act' method is called repeatedly while the project is running. If you do not want the 'getPlanet' method to be executed repeatedly, do not call it unconditionally from the 'act' method. What mechanism or criteria are you, or do you, want to use to have the planet change its image?
gr8n8 gr8n8

2015/2/11

#
thank you, I love you
You need to login to post a reply.