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

2011/6/11

Percents in game

1
2
kiarocks kiarocks

2011/6/11

#
I'm making a game that has a percent chance to change the image of an actor if it is next to a certain actor. How do I do this?
danpost danpost

2011/6/11

#
You could use the 'Greenfoot.getRandomNumber(int)' . If you wanted 20 percent chance, the code would look like this:
1
2
3
4
if (Greenfoot.getRandomNumber(100) <  20)
{
    // change the image here
}
For any other percentage, just change the 20 to the percentage you want.
kiarocks kiarocks

2011/6/11

#
ok thanks!
kiarocks kiarocks

2011/6/11

#
but im having another problem. here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  public boolean infect;
    {
         
    World theWorld = getWorld();
    if (Greenfoot.getRandomNumber(100) <  25
    setImage("ppl2.gif");
        List Uninfect = getObjectsInRange(2, InfectLv1.class);
    if(Uninfect.isEmpty())
          
             return false;
             
         
        else {
            return true;
        }
and it says return outside method. How do i fix this. Im new so Im trying to figure out as much as possible.
davmac davmac

2011/6/11

#
First thing: if you get the indentation right, it makes the code structure much easier to see and understand. Use the "auto-layout" function from the edit menu! Now, the problem with this code is actually the ";" that you have immediately after "infect". You want to declare a method, so the format is: public boolean infect { .... As it is, the compiler sees "public boolean infect;" and thinks you are trying to declare a variable called "infect" rather than a method called "infect". Just remove the ";" !!
kiarocks kiarocks

2011/6/11

#
says";" expected now, though
danpost danpost

2011/6/11

#
Put '()' at the end of your method declaration as such
1
2
3
4
public boolean infect()
{
    // code here
}
Also, your original error message 'return outside method' is because you are missing brackets around the return statement after the 'if' clause. This should be as such:
1
2
3
4
5
6
7
8
if (Uninfect.isEmpty())
{
    return false;
}
else
{
    return true;
}
Now better, and more convenient, would be this:
1
return !Uninfect.isEmpty();
It take the boolean value (Uninfect.isEmpty()), "nots" it (!), and then returns it. I did not try it, but you may have to write it this way:
1
return !(Uninfect.isEmpty());
I am fairly new at java myself, but I am familiar with programming languages (syntax can be a bear).
danpost danpost

2011/6/11

#
Try this -- let me know if it works.
1
return ((getObjectsInRange(2,InfectLv1.class)).size() != 0);
kiarocks kiarocks

2011/6/11

#
still same message as before-where should i put the code?
danpost danpost

2011/6/11

#
I'll need to see your code and know what error message you got. And where did YOU put the code?
kiarocks kiarocks

2011/6/11

#
now it works! Thanks!!
danpost danpost

2011/6/11

#
Glad to hear it! Did you try the one-liner return statement?
kiarocks kiarocks

2011/6/11

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public boolean infect()
    {
 
        World theWorld = getWorld();
 
        if (Greenfoot.getRandomNumber(100) <  100) <set for 100%
            {
 
                setImage("ppl2.gif");
 
            }
        {
            return ((getObjectsInRange(2, InfectLv1.class)).size() != 0);
        }
    }
...or not? compiles without errors, but nothing happens to them.
danpost danpost

2011/6/11

#
remove a single set of brackets around your return statement -- try again. Also, not sure why you have World theWorld = getWorld(); statement in method.
kiarocks kiarocks

2011/6/11

#
got rid of world statement and brackets- no errors but doesnt work still
There are more replies on the next page.
1
2