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

2014/9/15

Link between actors

1
2
VideoGame VideoGame

2014/9/15

#
Hi, I have a question about how to link an actor to get the characteristics of an another actor ( like the boolean, or something..). For example: I want to remove an object after touching an another object. If we touch this object, activate come true. How can I use this true in the other object to remove ?
danpost danpost

2014/9/15

#
I think you are adding an extra step to the process that is not need. That is, why set a boolean in the object to remove to true and then have that object remove itself when it finds the boolean is true? Instead of setting a boolean in the object, just remove the object from the world straight away from the class you were setting the boolean from. If you show the code you were using, it might help to show what I mean (the code where you detect the touching).
VideoGame VideoGame

2014/9/16

#
I tried this
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Switch here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Switche extends Platform
{
    private GreenfootImage switche = new GreenfootImage("Switch2.gif");
    public static SpecialDoor specialdoor;
    /**
     * Act - do whatever the Switch wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {               
        Actor player = getOneIntersectingObject (Player.class);
        if (player != null)
        {
            setImage(switche);
            getWorld().removeObject(specialdoor);
        }  
    
}
But it doesn't work ... And another thing, that was an example. I always want to link two actors for boolean because "if the player touch an object, boolean touch = true so traps don't work" etc. etc.
Super_Hippo Super_Hippo

2014/9/16

#
Since 'specialdoor' is never set to anything, it is null. If there is only one object of class 'Specialdoor' which you want to remove, you can use:
1
getWorld().removeObject( getWorld().getObjects(Specialdoor.class).get(0) );
VideoGame VideoGame

2014/9/16

#
Super Hippo: problem with your code with 'get(0)': "Some message have been simplified" it says and I don't understand your code
danpost danpost

2014/9/16

#
It looks like your switch is a one time deal -- that is, once it is flipped, it cannot be un-flipped. If that is the case, then the first statement in your act method should be something like this:
1
if (getImage() == switche) return;
This basically says not to execute the code below it if this Switche object has already detected the player. Now, the other thing that the method does, besides changing its image, is removing a 'specialdoor' object. This can be used within the class code for the trap to determine whether it is working or not:
1
if (Switche.specialdoor.getWorld() == null)...else...
This says do one thing if the switch was not flipped, otherwise do something else. Of course, you do not necessarily need an 'else' part and the '==' could be changed to '!=' if you only do something if the specialdoor is still in the world.
danpost danpost

2014/9/16

#
Will you ever have more than one Switche object in the world at a time? Do you set 'Switche.specialdoor' to a SpecialDoor object in your world class?
VideoGame VideoGame

2014/9/16

#
Normally, there will be one Switch to remove one SpecialDoor in each level. But I don't understand your explanation. The change of the image after being touched by the player works perfectly BUT this is the part to remove the SpecialDoor which doesn't work. There is no trap in that case. Just the player, the switche and the specialdoor
danpost danpost

2014/9/16

#
danpost wrote...
Will you ever have more than one Switche object in the world at a time? Do you set 'Switche.specialdoor' to a SpecialDoor object in your world class?
I need information.
VideoGame VideoGame

2014/9/16

#
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
 
/**
 * Write a description of class Background here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level extends World
{
   public static int level;
   private GameOver gameover = new GameOver();
   public static Player p;
   private GreenfootSound music1 = new GreenfootSound("music1.mp3");
   private GreenfootSound end = new GreenfootSound("end.mp3");
   public static String[][] map =
   {
        {"                              ",
         "                              ",
         "                              ",
         "               i              ",
         "                              ",
         "                              ",
         "                              ",
         "                              ",
         "                              ",
         "                              ",
         "  p             y            d",
         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
         "                              ",
         "                              ",
         "                              ",
         "                              ",
         "                              ",
         "                              ",
         "                              ",
         "                              ",},
        {"sssssssssssssssssssssssssssssm"
         "                             q"
         "                             m"
         "                            ym"
         "         k    k   k       qmmm"
         "   k   mmmmmmmmmmmmmm      l m"
         "   mm                  mm  l m"
         "    l    aaaaaaaaa      l  l m"
         "n   l     l             l  l d"
         "    l     l k           l  l q"
         "    l     l mmm   k     mm l m"
         "    w   k l       nn    m  l m"
         "       mmm              q  l m"
         "                        m  l m"
         "              mm     mmmm  l m"
         "           k            maaaam"
         "         mmm                 q"
         "       mmmmmmm               m"
         "p    mmmmmmmmmmm        k    m"
         "mmmmmmmmmmmmmmmmmmmaaaammmaaam"},
        {"                             m",
         "      k      k               q",
         "p  mmmmmmmmmmmmm             m",
         "mmmssssssssss                m",
         "                             m",
         "               k  o     qaaaam",
         "         k  mmmmmm           m",
         "     mmmmm           k       q",
         "o                   mmm      m",
         "l            k      mbm      m",
         "l          mmmm     mlm      m",
         "l          q         l    aaam",
         "l    mmm   sssssssss l       q",
         "l m                  l       m",
         "l m                  l       q",
         "l m                  l  aaaaam",
         "l m     ww           l       m",
         "l mo        ww    www        m",
         "  ym                   m     d",
         "mmmmaaaaaaaaaaaaaaaaaaamammmmm"},
        {"    m                        m",
         "   ym                   n  m m",
         "  mmmmmmmm      n    n     m m",
         "      l  q   n             m m",
         "m     l  m        n       bm m",
         "mmmmmml  ml    n          mm m",
         "      l   lm               m m",
         "     k    lm    w    w     m m",
         "  mmmmmmmmlmaaaaaaaaaaaaaaam m",
         "n        mlmmmmmmmmmmmmmmmmm m",
         "    k    mlmmmmmmmmmmmmmmmmm m",
         "  mmmmmm mlm lllllllllllllll m",
         "  ml    nmlm                 m",
         "o  l     mlm    k   qo   k   m",
         "m mlmmmm mlm  mmmmmmmmmmmmmmmm",
         "mfm o     lo                 m",
         "mmm o     lo                 m",
         "   nm   mmmmmmmmmmmmmmmmmmmm m",
         "p   m       k        k    bm m",
         "mmm mmmmmmmmmmmmmmmmmmmmmmmmdm"},
        {"d  lme e e e                 q",
         "mmmlm                       pm",
         "mmmlm                     mmmm",
         "ssslm        x m x m x m  me m",
         "   lm                     m  m",
         "l  lm  mmmmmmaaaaaaaaaaaaam  m",
         "lmmmm                 l       ",
         "lmmmmmmm    k    k    l       ",
         "lmmmmeemmmmmmmmmmmm   l   k   ",
         "  emm            lmmmmlmmmmmmm",
         "    my           l   ql      l",
         "    mmmmmmmmmmmmml   ml    k l",
         "mx  v            l mmmmmmmmmml",
         "    v            l           l",
         "  xmmmmmmmmmmm   l           l",
         "              m  l           l",
         "xm               l           l",
         "               mmxmmxxmxmxxmxm",
         "   mmmmmmmmmmmmm              ",
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
        {"   m e              m        m",
         "d  m       k       mm       pm",
         "m  m    mmmmmmm   mm     mmmmm",
         "   m   m    me   mm   mmm     ",
         "   m  m    me   mm   ml       ",
         "   m m    me   mm   m lmmm mmm",
         "   mm    me   mm   m  l m    m",
         " n m    me   mm   m   l m    m",
         "   m   me   mm   m    l m    m",
         "   m  m    mm   m   mmmmmmmmmm",
         "   m m    mm   m              ",
         " n m m lmmm   m               ",
         "   m m lmm   m       aaaaaaaaa",
         "   m m lmm  m    mmmmmm       ",
         "   m m lmm     m m    mmm     ",
         " n m m lmm       m      mmm   ",
         "   m mmmmmmmmmmmmm  mm    mm  ",
         "   m m                mm      ",
         "n  m my                mm     ",
         "   m mmaaaaaaaaaaaaaaaammmmmmm",},
        {"o        ee              e    ",
         "e                            p",
         "  mmmmmmm  mmmmm  mmmmmmm  mmm",
         "  m  m       mmm   m    m  m  ",
         "  mm m       m          m  m  ",
         "     m   m   m  mmm  mmmm mmm ",
         "mm  mmlmmmmmmm    m  m    m   ",
         "mm  m lm  m  mm  mmmmmm   m   ",
         "mm  m lm  m  em  m m m    mm  ",
         "m     l   m   m  m   mmm  m   ",
         "m     m  mmm  m mm     m  m  m",
         "    m            m m  mm      ",
         "    maaaaaaaaaaaaaam   m      ",
         "  mmmmmmmmmmmmmmmmmmmmmmmmmmmm",
         "                              ",
         "      k       k        k      ",
         "mmmmmmmmmmmmmmmmmmmmmmmmmmmm  ",
         "                             q",
         "     k    k     k     k       ",
         "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",},
        {"m         m e             m  d",
         "m         m  mmmmmm       m  m",
         "m  mmmm   mm     lmmmmmmm m  m",
         "   meem  km  mmmmlm       mm m",
         "y  m  mm mmmmm  mlm mmmmmmm  m",
         "mm m            mlm         mm",
         " l m    m        lm          m",
         " lmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
         " l e e e e e e e e e e e e l m",
         " l                         l m",
         "aaaaaaaaaaaaaaaaaaaaaaaaaaml m",
         "mmmmmmmmmmmmmmmmmmmmmmmmmmml m",
         "ml l      l        l       l m",
         "ml l      l        l       l m",
         "ml l  qo  l   qo   l  qo   l m",
         "mlmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
         "ml                            ",
         "ml m   x   m   x   m   m   m  ",
         "ml                           p",
         "mmaaaaaaaaaaaaaaaaaaaaaaaaaamm",},
        {"  l m e   l mee l  l  l      d",
         "p l m     l m   l  l  l      m",
         "mmlmm   m l m  ml ml ml m  m m",
         "  l mm  m l m  ml ml ml m  m m",
         "  l m   m l m  ml ml ml m  m m",
         "  l m  mm l mmmml mmmml mmmm m",
         "  l m   m l m   l     l      m",
         "  l mm  m l m   l  k  l  k   m",
         "  l m   m l m  mmmmmmmmmmmmmmm",
         "  l m  mm l m                e",
         "  l m   m l mm    k     k     ",
         "  l mm  m l mmmmmmmmmmmmmmmm  ",
         "  l m   m l m                 ",
         "  l m  mm l m     k     k    m",
         "  l m   m l m  mmmmmmmmmmmmmmm",
         "  l mm  m l m                 ",
         "  l m   m l mm    k     k     ",
         "  l m  mm l mmmmmmmmmmmmmmmm  ",
         "                             m",
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaam",},
        {"    m       m  m  l           ",
         "p   m       m  m  l           ",
         "vm  m  m m  m  mmvlmmvm m  m  ",
         "    vm   m  m     l  vm m  m  ",
         "    m    m  mm    l mmm m  m  ",
         "  mmmmm  m     m  m  vm m  m  ",
         "      m vm     m  m  vm m  m  ",
         "mmm   m  m mmmmmmmmmmmmamaam  ",
         "  m   m     m                 ",
         "  m mmmm k  mmm        k   k  ",
         "  m   mmmml m   m  mmmmmmmmmm ",
         "m        ml m  mmmmm          ",
         "    k    mlvm                 ",
         "mmmmmmm  mllmmm  mm  m   xmm  ",
         "         mllmmmaammaammaa     ",
         "      k  mmlm               mm",
         "mmm mmmmvm lm               mm",
         "           lm  mm mm mm mm mmm",
         "       k   mm                 ",
         "mmmammmmmmmmmaaaaaaaaaaaaaaaad",}};
   
   public void populateWorld()
   {
        Player p = new Player();
        Door door = new Door();
        SpecialDoor specialdoor = new SpecialDoor();
        Mountain m = new Mountain();
        Mountain1 n = new Mountain1();
        Mountain2 w = new Mountain2();
        Pike1 a = new Pike1();
        Pike2 s = new Pike2();
        Ladder l = new Ladder();
        Knight k = new Knight();
        for( int i = 0; i < map[level].length; i++) { // la longeur du tabmap
            for( int j = 0; j < map[level][i].length(); j++) { // la longueur de la String map[i]
                Actor actor = null; // crée un actor null si w,p,m = on ajoute l'actor
                if(map[level][i].charAt(j) == 'p') {
                    actor = new Player();
                    this.p = (Player)actor;
                }
                else if(map[level][i].charAt(j) == 'm') {
                    actor = new Mountain();
                }
                else if(map[level][i].charAt(j) == 'n') {
                    actor = new Mountain1();
                }
                else if(map[level][i].charAt(j) == 'w') {
                    actor = new Mountain2();
                }
                else if(map[level][i].charAt(j) == 'd') {
                    actor = door;
                }
                else if(map[level][i].charAt(j) == 'a') {
                    actor = new Pike1();
                }
                else if(map[level][i].charAt(j) == 's') {
                    actor = new Pike2();
                }
                else if(map[level][i].charAt(j) == 'e') {
                    actor = new Pike3();
                }
                else if(map[level][i].charAt(j) == 'k') {
                    actor = new Knight();
                }
                else if(map[level][i].charAt(j) == 'l') {
                    actor = new Ladder();
                }
                else if(map[level][i].charAt(j) == 'x') {
                    actor = new Falling();
                }
                else if(map[level][i].charAt(j) == 'o') {
                    actor = new ArrowthrowD();
                }
                else if(map[level][i].charAt(j) == 'q') {
                    actor = new ArrowthrowG();
                }
                else if(map[level][i].charAt(j) == 'y') {
                    actor = door.getKey();
                }
                else if(map[level][i].charAt(j) == 'i') {
                    actor = new Menu();
                }
                else if(map[level][i].charAt(j) == 'v') {
                    actor = new Invisible();
                }
                else if(map[level][i].charAt(j) == 'b') {
                    actor = specialdoor;
                }
                else if(map[level][i].charAt(j) == 'v') {
                    actor = new SpecialDoor();
                }
                if( actor != null) {
                    addObject( actor, 16+j*32, 16+i*32);  // inversion de i et de j
                }
            }  
        }
   }
    
   public Level()
   {
        super(32*map[level][0].length(), 32*map[level].length, 1);
        // 30x20 charactères
        // 960/30= 32 pixels et 640/20 = 32 pixels
        removeObjects(getObjects(null));
        populateWorld();            
   }
}
Here is the subclass of world
Super_Hippo Super_Hippo

2014/9/16

#
VideoGame wrote...
Super Hippo: problem with your code with 'get(0)': "Some message have been simplified" it says and I don't understand your code
Maybe it was because I didn't make the 'D' in SpecialDoor uppercase. Never saw this message before though.
1
2
3
4
5
// remove this line:
// public static SpecialDoor specialdoor;
 
getWorld().removeObject( getWorld().getObjects(SpecialDoor.class).get(0) ); 
//instead of: getWorld().removeObject(specialdoor); 
'getWorld().getObjects(SpecialDoor.class)' creates a list of all objects of class 'SpecialDoor'. 'get(0)' gets the first element (and the only element in this case) from the list. 'getWorld().removeObject(...)' removes this object.
VideoGame VideoGame

2014/9/16

#
I understand now what you want to do but it's not the 'D' the problem...
danpost danpost

2014/9/16

#
(1) I do not see where a Switche object is being added into the world; (2) I do not see where you set 'Switche.specialdoor' to the SpecialDoor object; (3) You are using 'v' for more than one type object; and the second one of those is your SpecialDoor object. This means that no SpecialDoor objects will be added into your world through the 'populateWorld' method.
Super_Hippo Super_Hippo

2014/9/16

#
Hm, if I google "Some message have been simplified" (with the ""), the only thing which pops up is this thread. Sorry, I never saw this error message, so I don't know what's wrong :(
VideoGame VideoGame

2014/9/16

#
danpost: 1. switche added. 2. How do I set it ? 3. It's Ok.
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
for( int i = 0; i < map[level].length; i++) { // la longeur du tabmap
    for( int j = 0; j < map[level][i].length(); j++) { // la longueur de la String map[i]
        Actor actor = null; // crée un actor null si w,p,m = on ajoute l'actor
        if(map[level][i].charAt(j) == 'p') {
            actor = new Player();
            this.p = (Player)actor;
        }
        else if(map[level][i].charAt(j) == 'm') {
            actor = new Mountain();
        }
        else if(map[level][i].charAt(j) == 'n') {
            actor = new Mountain1();
        }
        else if(map[level][i].charAt(j) == 'w') {
            actor = new Mountain2();
        }
        else if(map[level][i].charAt(j) == 'd') {
            actor = door;
        }
        else if(map[level][i].charAt(j) == 'a') {
            actor = new Pike1();
        }
        else if(map[level][i].charAt(j) == 's') {
            actor = new Pike2();
        }
        else if(map[level][i].charAt(j) == 'e') {
            actor = new Pike3();
        }
        else if(map[level][i].charAt(j) == 'k') {
            actor = new Knight();
        }
        else if(map[level][i].charAt(j) == 'l') {
            actor = new Ladder();
        }
        else if(map[level][i].charAt(j) == 'x') {
            actor = new Falling();
        }
        else if(map[level][i].charAt(j) == 'o') {
            actor = new ArrowthrowD();
        }
        else if(map[level][i].charAt(j) == 'q') {
            actor = new ArrowthrowG();
        }
        else if(map[level][i].charAt(j) == 'y') {
            actor = door.getKey();
        }
        else if(map[level][i].charAt(j) == 'i') {
            actor = new Menu();
        }
        else if(map[level][i].charAt(j) == 'u') {
            actor = new Invisible();
        }
        else if(map[level][i].charAt(j) == 'v') {
            actor = new SpecialDoor();
        }
        else if(map[level][i].charAt(j) == 'z') {
            actor = new Switche();
        }
        if( actor != null) {
            addObject( actor, 16+j*32, 16+i*32);  // inversion de i et de j
        }
    }  
}
Super Hippo, no problem, thanks for your help
There are more replies on the next page.
1
2