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

2012/6/4

setRotation() not working

guvnor guvnor

2012/6/4

#
Hey all, this code snippet is from my World sub-class, and for some reason when I call the addSpider() method in my Spider class they are always turned east. I want the default rotation to be random.
    public void addSpider()
    {
        Spider spider = new Spider();
        
        if(Greenfoot.getRandomNumber(spawn) < 1)
        {
            addObject(new Spider(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            spider.setRotation(Greenfoot.getRandomNumber(360));
        }
    }
danpost danpost

2012/6/4

#
You did it again! You are creating TWO instances of Spider class objects. You adding the second one to the world, and rotating the first one. You need to add and turn the same one.
public void addSpider()
{
    if (Greenfoot.getRandomNumber(spawn) < 1)
    {
        Spider spider = new Spider();
        addObject(spider, Greenfoot.getRandomNumber(getWidth()), GreenfootgetRandomNumber(getHeight()));
        spider.setRotation(Greenfoot.getRandomNumber(360));
    }
}
You need to login to post a reply.