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

2013/2/4

Question

avocasso avocasso

2013/2/4

#
How can I create at least five Orbs in the World. Within the OrbWorld constructor, use a loop to place at least five Orbs in the world whenever you run the program. Use conditions to ensure none of the Orb graphics will be cut off by the edge of the world. Assign each of the Orbs a random velocity.
avocasso avocasso

2013/2/4

#
This is what I tried so far: public class OrbWorld extends World { /** * Constructor for objects of class OrbWorld. * */ public OrbWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); randomOrbs(5); } /** * Add at least 5 more orbs to the world. */ private void randomOrbs(int vel) { while (vel >= 5) { What should I have inside here } } }
teddy teddy

2013/2/4

#
Hi Avocasso, May be you can find some examples in the Wombats scenario, where a few wombats are placed on the canvas when you choose to populate te world. Look at the WonbatWorld
populate
method and/or look at
randomLeaves
to see how you could get a random value for your Orb velocity.
danpost danpost

2013/2/4

#
I was thinking something along these lines: (1) start the loop (iterate 5+ times) -- 5 (set part) plus a random amount more (random part) (2) create a new Orb -- the orb constructor can assign random velocity -- the orb constructor can also assign a random size (3) use size of orb created to determine the limits of location placement in the world -- the size of the world minus the size of the orb is the range (random part) -- the added offset is half the size of the orb (set part) (do the above for both horizontal and vertical dimensions) (4) add the orb at determined coordinates (5) finish loop But this does not satisfy 'Use conditions to ensure none of the Orb graphics will be cut off by the edge of the world.' (the key word being 'conditions'). To use conditions (a) place orb anywhere in the world (b) check closeness to each edge and relocate if too close
You need to login to post a reply.