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

Report as inappropriate.

programmer
programmer presents ...

2008/12/24

animation tutorial results

No description.

5495 views / 2 in the last 7 days

Tags: None

open in greenfoot
Your browser does not support the canvas tag.
programmerprogrammer

2008/12/24

I am confused about how getRandomNumber works. I thought my code would add 5 objects, but it adds only one. Why is that? Also, it seems like getRandomNumber does not get a new number sometimes, but just uses the same number you got before.
RHE_THSRHE_THS

2008/12/24

first of all, you have to manually add the objects into your world class. We can't interactively add them when you export the scenario to Greenfoot Gallery. Also, getRandomNumber generates a random integer between 0 (inclusive) and your number (exclusive). so if we suppose that we have *random* assigned to the random number, you could make a loop that repeats *random* number of times and places an object into the world each time. If your increment for getRandomNumber is small, it might get the same random number 2 times in a row, making it seem like you are using the same number as before. Making your source code available would be much better so I can see where your code needs editing. Hope this helps!
A new version of this scenario was uploaded on Thu Dec 25 15:35:23 UTC 2008
A new version of this scenario was uploaded on Thu Dec 25 15:41:50 UTC 2008
programmerprogrammer

2008/12/25

hi RHE_THS thanks. sorry. I meant to publish the code. The default is not-published. Now it is published. Here is the code public TestWorld() { // Create a new world with 20x20 cells with a cell size of 10x10 pixels. super(800, 300, 1); Bird bird = new Bird(); addObject(bird, Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(400)); addObject(bird, Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(400)); addObject(bird, Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(400)); addObject(bird, Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(400)); addObject(bird, Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(400));
RHE_THSRHE_THS

2008/12/27

You might want trying just to make a loop out of this so you only have one addObject line. Or, you might consider putting "new Bird()" in place of the "bird" (and therefore removing the line where you declare bird) since bird applies to only 1 of them.
mjrb4mjrb4

2008/12/28

Firstly, the scenario doesn't work at the moment because your Smiley0.png has an upper case S at the start - and it needs to be lower case since it's lower case in the prefixes you've specified! In versions of Greenfoot previous to 1.4.6 the API was case insensitive, so it'd usually work when you were running it on your home PC, but not the gallery (which is case sensitive.) However, the latest squeaky new version of Greenfoot does I believe have case sensitive filenames built in, so if you upgrade you shouldn't fall into that trap anymore - you'll get the errors when you compile on your home PC rather than just getting a dead scenario when you upload to the gallery :) Secondly, you probably want to get rid of or change: // Create a new world with 20x20 cells with a cell size of 10x10 pixels. ...since the following line clearly doesn't do that! Also, is one of those lines meant to read 700 instead of 800? As pointed out earlier, you're not actually adding seperate birds in the scenario with your code above, you're just repositioning an existing one (since you only ever create one bird.) So at the moment, the 4 last lines in that code could be deleted with no noticeable affect. And as for creating a loop, that's always the best way to avoid writing out the same code lots of times (even if it's only one line!) So if you wanted 5 seperate birds, you could do something like the following in place of the existing code: for(int i=0 ; i<5 ; i++) { addObject(new Bird(), Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(400)); } If nothing else, this way makes it easier to avoid mistakes (such as the 700, if that was a mistake!), reduces clutter and generally just improves code quality.
programmerprogrammer

2008/12/29

RHE_THS and mjrb4, Thank you both very much for explaining why the addObject lines were not working and how to fix it. This is a problem I had in several scenarios. I'll change it into a loop. The 700 was trying to debug, to see if it would solve the problem. I'll go and fix the case problem too. Thanks.
davmacdavmac

2009/1/27

Hi programmer, have you had a chance to fix the case problem with the images? I still can't see this scenario...

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.