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

2015/3/4

How do you add object within the constructor for the object you are adding?

Diljot Diljot

2015/3/4

#
I am in a Grade 12 AP Computer Science course and we are currently recreating the Jeroo environment within Greenfoot. So far I have recreated everything that Jeroo can do within Greenfoot. I just need to know how to add the Jeroo object into the world using the Jeroo constructor. In a separate class I have code that looks like this:
1
Jeroo bob = new Jeroo();
However this will not add the object into the world. Is there anyway I can add the object into the world using the Jeroo's constructor? I've tried many work arounds and so far nothing works. Could anyone please help? Thanks, Diljot.
danpost danpost

2015/3/5

#
The line of code you gave above creates a Jeroo object and assigns that object to the local field 'bob'. It does not add the object into a world. If you are executing that line in a class that extends World, then you can follow that line with:
1
addObject(bob, /** x and y coordinates to place bob */);
Diljot Diljot

2015/3/5

#
That would work, but the class this code is in, is a Tester class. It is separate from everything.
danpost danpost

2015/3/5

#
Diljot wrote...
That would work, but the class this code is in, is a Tester class. It is separate from everything.
Well then. The Jeroo object created would have no idea as to what world object to even place itself into. Does the Tester class hold that information? Maybe you should post the code for the Tester class. It would make it much easier to understand what is going on and possibly what can be done about the issue. Also, the code of the class where the Tester object is created, if applicable.
Diljot Diljot

2015/3/5

#
Here is the Tester class:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class JerooTester
{
    //constants
    private static final String EAST = "EAST";
    private static final String WEST = "WEST";
    private static final String SOUTH = "SOUTH";
    private static final String NORTH = "NORTH";
     
    private static final String RIGHT = "RIGHT";
    private static final String LEFT = "LEFT";
    private static final String AHEAD = "AHEAD";
    private static final String HERE = "HERE";
    public static void main(String args[])
    {       
        Jeroo Player = new Jeroo(1,1);
        Jeroo StartVar = new Jeroo(12,12,1);
        Jeroo EndVar = new Jeroo(24,24,SOUTH);
        while(StartVar.hasFlower())
        {
            if(EndVar.hasFlower())
            {
                StartVar.toss();
            }
            if (Player.isWater(AHEAD))
            {
                Player.turn(RIGHT);
            }
            else if (Player.isFlower(HERE))
            {
                Player.pick();
            }
            else if (Player.isNet(AHEAD))
            {
                if(Player.hasFlower())
                {
                    Player.toss();
                }
                else
                {
                    Player.turn(RIGHT);
                }
            }
            else if (Player.isJeroo (AHEAD))
            {
                if (Player.hasFlower())
                {
                    Player.give(AHEAD);
                }
                else
                {
                    Player.turn(RIGHT);
                }
            }
            else if (!Player.isClear(AHEAD) || Player.isClear(AHEAD))
            {
                Player.move();
            }
            else
            {
                Player.move();
            }
        }
    }
}
Diljot Diljot

2015/3/5

#
That code is copied from a program I made in actual Jeroo. Once I recreated all the Jeroo functionality it was just a matter of making the Tester class work. This code compiles but running it returns the following error: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. Hope that helps.
danpost danpost

2015/3/5

#
Obviously, Jeroo and Greenfoot do vastly different things behind the scenes and code cannot be that easily transferred from one to the other. I am not at all familiar with Jeroo, but, one big difference is that you run your projects in Jeroo from the command line, where in greenfoot, you do not. I will try to get you closer to what is needed in greenfoot, but I cannot guarantee anything. It does appear that the first two int values in the constructor of your Jeroo objects are for where they get placed into the world -- so, I am proceeding along those lines. I suggest changing your JerooTester class to the following:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
public class JerooTester extends World
{
    //constants
    private static final String EAST = "EAST";
    private static final String WEST = "WEST";
    private static final String SOUTH = "SOUTH";
    private static final String NORTH = "NORTH";
      
    private static final String RIGHT = "RIGHT";
    private static final String LEFT = "LEFT";
    private static final String AHEAD = "AHEAD";
    private static final String HERE = "HERE";
 
    // instance fields
    Jeroo Player = new Jeroo();
    Jeroo StartVar = new Jeroo(1); // change Jeroo(int, int, int) constructor to Jeroo(int) using last int
    Jeroo EndVar = new Jeroo(SOUTH); // change jeroo(int, int, String) constructor to Jeroo(String)
 
    // world constructor
    public class JerooTester extends World
    {
        super(24, 24, 25);
        addObject(Player, 1, 1);
        addObject(StartVar, 12, 12);
        addobject(EndVar, 24, 24);
    }
 
    // the act method
    public void act()
    {
        if(StartVar.hasFlower())
        {
            if(EndVar.hasFlower())
            {
                StartVar.toss();
            }
            if (Player.isWater(AHEAD))
            {
                Player.turn(RIGHT);
            }
            else if (Player.isFlower(HERE))
            {
                Player.pick();
            }
            else if (Player.isNet(AHEAD))
            {
                if(Player.hasFlower())
                {
                    Player.toss();
                }
                else
                {
                    Player.turn(RIGHT);
                }
            }
            else if (Player.isJeroo (AHEAD))
            {
                if (Player.hasFlower())
                {
                    Player.give(AHEAD);
                }
                else
                {
                    Player.turn(RIGHT);
                }
            }
            else if (!Player.isClear(AHEAD) || Player.isClear(AHEAD))
            {
                Player.move();
            }
            else
            {
                Player.move();
            }
        }
    }
}
danpost danpost

2015/3/5

#
Before you change everything, please show the code for your subclass of World.
Diljot Diljot

2015/3/5

#
Here is my JerooWorld class (subclass of World):
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
32
33
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Creates an emulation of the Jeroo Grid in Greenfoot
 *
 * @author Diljot
 * @version 1.0
 */
public class JerooWorld extends World
{
 
    /**
     * Constructor for objects of class JerooWorld.
     *
     */
    public JerooWorld()
    {   
        // Create a new world with 24x24 cells with a cell size of 20x20 pixels.
        super(26, 26, 20);
        populateWorld();
    }
     
    public void populateWorld()
    {
        for(int i = 1; i<=24; i++)
        {
            for(int j = 1; j<=24; j++)
            {
                addObject(new Grass(), i, j);
            }
        }
    }
}
Thanks.
danpost danpost

2015/3/5

#
Ah, so you are not really re-creating the Jeroo environment with the ability for the user to create their projects. You are just re-creating a specific project that was originally created with Jeroo. Making the change to the class suggested above should put you close to what you want (you would also need to adjust the constructors within the Jeroo class). However, if you really want to be able to have the Jeroo objects added from within the Jeroo constructor, then in your world class above, do the following:
1
2
3
4
// insert at line 11
public static World world;
// insert at line 20
world = this;
and in the Jeroo constructor(s):
1
2
// use the following to add the actor into the world
JerooWorld.world.addObject(this, x, y);
where x and y are the first two int parameters in the Jeroo constructor.
Diljot Diljot

2015/3/5

#
Thank you! That works but the code after the Jeroo's are created is executed but the world does not update to show this. I know the code gets executed because I've strategically placed print statements throughout, so I know the code works. But Greenfoot doesn't seem to update the world to show what is going on unless the code is in the act() method of the Jeroo class. Any way to solve this?
danpost danpost

2015/3/5

#
You could put it in the 'act' method of the JerooWorld class like I did above. If you would rather not, then you can add a 'repaint' and a 'Greenfoot.delay' call at the end, but still inside, of the 'while' loop. You will find, however, that you must provide a way out of the loop (whether the goal of the project was accomplished or not). Like checking for a press/release of the "escape" key, or something. Otherwise, there will be no easy way to stop/restart or reset the project.
Diljot Diljot

2015/3/5

#
Ok I will try that now, thanks for all the help!
Diljot Diljot

2015/3/5

#
IT WORKS!! Yes! Thank you again for all your help!
You need to login to post a reply.