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

2012/3/13

Zombie spawn

armoredarmada armoredarmada

2012/3/13

#
I took a break my my shooter game and Im working on a game where a gun rotates to shoot zombies that proceed to its end. I can get the zombie to move perfectly, but my problem is that I am still new to this and I need to know the correct format for getRandomNumber() with the numbers 1 and 6. and I want the zombie actor to be inserted into the map after the random number thing lands on 3. How do I do that?
kiarocks kiarocks

2012/3/14

#
int rand = Greenfoot.getRandomNumber(6)+1;
if(rand == 3)
{
    //do stuff
}
danpost danpost

2012/3/14

#
If it is a matter of a 1 in 6 chance that a zombie is added to the world, and nothing happens when other numbers are landed on, then the following would suffice:
if (Greenfoot.getRandomNumber(6) + 1 == 3)
{
    // do stuff
}
or the following (which is essentially the same)
if (Greenfoot.getRandomNumber(6) < 1)
{ //...
where you actually see the '1' in '6' chance in the code. If other action occur when other numbers are landed on, it may be better to do the following:
switch (Greenfoot.getRandomNumber(6) + 1)
{
    case 1: // action when '1' is landed on followed by 'break;'
    case 2: // action when '2' is landed on followed by 'break;'
    case 3: // insert zombie into world followed by 'break;'
    // etc.
}
armoredarmada armoredarmada

2012/3/14

#
sorry for the incompetence of my newbie-level design capabilities, but why is there a +1 and what does == mean?
danpost danpost

2012/3/14

#
The getRandomNumber(int) method in the Greenfoot class returns an integer value between zero and one less than the 'int' value in its parameter (so getRandomNumber(6) will return one of { 0, 1, 2, 3, 4, 5 }. You asked for a range of 1 to 6, so adding one more to it gives that range. The double equal sign means 'is comparatively equal to' (used in comparing values, and will return a true/false value), as opposed to the single equals sign which is used to set values, and could be read as 'is set to'. Be cautious when using the double equal sign, as it is usually only reliable on numeric values and checking for a null state. For comparing strings for equality, you would use 'if (string1.equals(string2))'.
danpost danpost

2012/3/14

#
The Java Tutorials contain many trails, and has an abundant supply of information linked to this page. I suggest you start by going to the second sub-title on the left in red --'Trails Covering the Basics', and the second item listed under that is 'Learning the Java Language'. Click on that and you will be on your way.
You need to login to post a reply.