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

2014/2/3

Spawing

tomw tomw

2014/2/3

#
Hi i was wondering if anyone can help me with this i have messed it up and i have just confused my self so can anyone please tell me what is wrong with this?
    
{    public void spawn()
    { if (Greenfoot.getRandomNumber (600) == 1)
        getWorld().addObject(new robot(), 400, 300);
    }
     
}
jimboweb jimboweb

2014/2/3

#
You don't need the curly brackets at the beginning and end. You should just have this: public void spawn() { if (Greenfoot.getRandomNumber (600) == 1) getWorld().addObject(new robot(), 400, 300); }
danpost danpost

2014/2/3

#
Actually, there is another place you may want to place curly brackets:
public void spawn()
{
    if (Greenfoot.getRandomNumber(600) == 1)
    {
        getWorld().addObject(new robot(), 400, 300);
    }
}
If you are not too experienced in programming, the brackets for the 'if' block help in the event you add code to do more when the robot is added to the world (so the new code does not end up outside the 'if' block). Also, class names (by convention) should begin with an uppercase letter; so, you should use 'new Robot()' and not 'new robot()' (it can cause confusion when others see the code). One more thing: the code should work if placed within an Actor subclass; if in a World subclass, the 'getWorld().' part should be removed (and the World subclass is probably where the random spawning code should go).
tomw tomw

2014/2/5

#
Ok thanks works now :)
You need to login to post a reply.