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

2019/4/23

Random starting rotation?

n1ght5t41xX n1ght5t41xX

2019/4/23

#
Sup, I am studying for an informatics exam I have coming up and I am coding all of our scenarios again. I stumbled upon this task: "let the starting location be random - this is done by the method setLocation(int); Here is my code, but: If I put it into the method that constructs it, I cant call it, and if I put into the Redcell class into act(); , they spin like maniacs. I just need to set the start rotation once! Pls help :) I know this is probably easy to solve but I am mentally challenged, kappa In Redcell:
private int speed = Greenfoot.getRandomNumber(1)+1;
    /**
     * Act - do whatever the Redcell wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX()-speed, getY());
        //setRotation(Greenfoot.getRandomNumber(360));
        turn(1);
        if (getX() == 0)
        {
            getWorld().removeObject(this);  
        }
    }    
In World:
if (Greenfoot.getRandomNumber(100) < 6)
        {
            
            addObject (new Redcell(), 779, Greenfoot.getRandomNumber(359));
            //setRotation(Greenfoot.getRandomNumber(360));
        }
Super_Hippo Super_Hippo

2019/4/23

#
Either use a constructor which is executed once when the object is created:
public Redcell()
{
    setRotation(Greenfoot.getRandomNumber(360));
}
Or call the method on the object you created:
Actor redcell = new Redcell();
addObject(redcell, 779, Greenfoot.getRandomNumber(359));
redcell.setRotation(Greenfoot.getRandomNumber(360));
n1ght5t41xX n1ght5t41xX

2019/4/23

#
Super_Hippo wrote...
Either use a constructor which is executed once when the object is created:
public Redcell()
{
    setRotation(Greenfoot.getRandomNumber(360));
}
Or call the method on the object you created:
Actor redcell = new Redcell();
addObject(redcell, 779, Greenfoot.getRandomNumber(359));
redcell.setRotation(Greenfoot.getRandomNumber(360));
Thanks a lot!
You need to login to post a reply.