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

2015/1/30

Adding an object of itself please help!

h123n h123n

2015/1/30

#
Hi, I've been playing around with randomisation and I have been trying to have a scenario where a grass square appears in a random position and you press space and there is a 1 in 2 chance it will duplicate itself on the screen. I can't figure out how to get it to work. I've looked at the example of the crab and worms but I want to add an object of itself. It is hard to do this because I cannot declare it. Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import greenfoot.*;
 
/**
 * Write a description of class Grass here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Grass extends Actor
{
    int lucky = Greenfoot.getRandomNumber(2);
    /**
     * Act - do whatever the Grass wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            if (lucky == 1)
            {
                if (grass != null)
                {
                    World world;
                    world.getWorld();
                    world.removeObject(grass);
                }
            }
        }
    }   
}
h123n h123n

2015/1/30

#
anyone? ;(
h123n h123n

2015/1/30

#
I solved this myself. I found instead of declaring the class itself, you can just use "this" to replace the grass bit.
Super_Hippo Super_Hippo

2015/1/30

#
Line 11 creates a random number, either 0 or 1. It is one number for each object, not a random number every time you use the variable. Line 18 is checking for the 'right' button, not 'space'. Line 22 is checking if 'grass' isn't null. What is grass supposed to be? Line 25 probably should be 'world=getWorld();' Why do you want to remove this grass. I thought you want to duplicate it?
1
2
3
4
5
6
7
8
9
10
public void act()
{
    if (Greenfoot.isKeyDown("space"))
    {
        if (Greenfoot.getRandomNumber(2)==0)
        {
            getWorld().addObject(new Grass(), getX()+Greenfoot.getRandomNumber(3)-1), getY()+Greenfoot.getRandomNumber(3)-1;
        }
    }
}
Pay attention that this will cause MANY Grasses to appear if you hold down space... EDIT: Was distracted... too late ^^
h123n h123n

2015/1/31

#
I've been playing around with it, I have done what you said and instead of doing it out of 2, I did it out of 100. Also with the remove thing, I wasn't sure what the actuall method was called, I've changed it now to .addObject (Not sure if it even is a method)
danpost danpost

2015/1/31

#
h123n wrote...
I wasn't sure what the actuall method was called, I've changed it now to .addObject (Not sure if it even is a method)
To be sure, check the documentation. For code in an Actor subclass, refer to the Actor class documentation, where you will find the 'getWorld' method that returns a World object; then, check the World class documentation, where you will find the 'addObject' method. You will always be able to follow this pattern for any 'dot' operated statement (each part returns an object reference or a value; and for any object reference, you can refer to the documentation for the possible methods or fields you can access on that referenced object).
You need to login to post a reply.