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

2016/12/19

isTouching/intersectingObject/objectAtOffset

Nosson1459 Nosson1459

2016/12/19

#
What is a/the more/most accurate, and shorter way of checking if my actor (Pengu) is colliding with a different one (Box/Block/Cliff), and what should I change in the code below? Right now the Pengu doesn't look like it's always bumping into these things, it can sometimes be inside the image. P. S. danpost, if you don't know of a solution (like last time), then don't "Post a reply" and make people think that the problem/issue is solved, if you do know a solution then don't hesitate to share it with me. Here is my world class 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * This is the whole scene. It creates and contains the objects that are in it.
 */
public class Scene extends World
{
    public int lives=2; // the number of lives, when 0 - game over
    public int score; // the total score of the game
    public int coins=0; // the amount of collected coins
    public int level=1; // the game level
    public boolean move=false; // only side-scroll when true
    public boolean direction=true; // the direction the penguin is facing
    public boolean way=true; // direction for  side-scrolling on a platform
    public boolean shooting=false; // whether penguin has shooting
    public boolean pause=false; // whather game is paused
    public boolean isDoor=false; // when a door is added (for stopping side-scrolling)
    public boolean onCloud=false; // when on a cloud - true
    public boolean isThere=false; // true when the paused actor is there
    private Pengu pengu; // penguin actor
    private Score addScore; // Score actor/object
    /**
     * The Scene constructor
     */
    public Scene()
    
        super(750,650,1,false);
        setBackground("clouds.jpg");
        setPaintOrder(HelpBox.class,Help.class,Coins.class,Level.class,Life.class,Score.class,Pause.class,Bar.class,Door.class,
            Pengu.class,Bullet.class,Cliff.class,DoorPost.class,Paused.class,Box.class,Shooting.class,Snake.class,SnakeD.class,Platform.class);
        addObject(new Bar(),375,18);
        addObject(new Coins("Coins: "),645,18);
        addObject(new Life("Lives: "),85,18);
        addScore=new Score("Score: ");
        addObject(addScore,470,18);
        addObject(new Level("Level: "),270,18);
        addObject(new HelpBox(),730,18);
        addObject(new Pause(),18,18);
        levelUp();
    }
 
    /**
     * Act - do whatever the Scene wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(!pause)
        {
            score=addScore.value;
            if(lives<0)
            {
                Greenfoot.setWorld(new End(false,score));
            }
            if(coins==100)
            {
                score(1000);
                lives++;
                coins=0;
            }
            sideScrolling();
        }
    }
 
    /**
     * Adds the total score to the EndScore class
     */
    public void score(int score)
    {
        addScore.add(score);
    }
 
    /**
     * Resets the level for restarting or for placing the next level
     */
    public void reset(boolean shoot)
    {
        removeObjects(getObjects(SideScrollers1.class));
        removeObjects(getObjects(Pengu.class));
        isDoor=false;
        move=false;
        onCloud=false;
        if(shoot)shooting=false;
    }
 
    /**
     * Calls each level method when the previous one is passed
     */
    public void levelUp()
    {
        if(level==1)
            level1();
        if(level==2)
            level2();
        if(level==3)
            level3();
        if(level==4)
        {
            Greenfoot.setWorld(new End(true,score)); //for the GameOver/Win screen, and score
        }
    }
 
    /**
     * The level 1 constructor
     */
    public void level1()
    {
        pengu=new Pengu();
        addObject(pengu,192,470);
        for(int j=20;j<481;j+=40)
        {
            for(int i=510;i<631;i+=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        addObject(new Creature(),440,390);
        for(int i=240;i<401;i+=40)
        {
            addObject(new Box(1),i,390);
        }
        addObject ( new Cliff(3), 658, 555);
        addObject(new SnakeD(),658,450);
        addObject(new Box(6),658,350);
        addObject(new Cliff(2),908,525);
        addObject(new Coin(),908,310);
        addObject(new Coin(),873,310);
        addObject(new Coin(),943,310);
        addObject(new Cliff(2),1243,525);
        addObject(new Platform(4),1076,450);
        for(int j=1362;j<1483;j+=40)
        {
            for(int i=510;i<631;i+=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        for(int i=1362;i<1443;i+=40)
        {
            addObject(new Coin(),i,390);
        }
        addObject(new Box(2),1482,390);
        addObject(new Block(1),1679,439);
        addObject(new Cliff(3),1788,555);
        addObject(new Cliff(2),1942,525);
        addObject(new Snake(),1942,361);
        for (int j=2105; j<2346; j+=40)
        {
            for (int i=486; i<647; i+=40)
            {
                addObject(new Block(3),j,i);
            }
        }
        addObject(new Door(),2292,433);
        addObject(new DoorPost(),2292,433);
    }
 
    /**
     * The level 2 constructor
     */
    private void level2()
    {
        reset(false);
        pengu=new Pengu();
        addObject(pengu,60,470);
        int a=509;
        for(int j=-20;j<661;j+=40)
        {
            for(int i=630;i>a;i-=40)
            {
                if(j<301||j>381)
                {
                    addObject(new Block(2),j,i);
                }
            }
            if(j>381&&j<541)
            {
                a+=40;
            }
            else if(j>141&&j<261)
            {
                a-=40;
            }
        }
        for (int i=60;i<141;i+=40)
        {
            addObject(new Box(1),i,382);
        }
        for (int i=60;i<141;i+=40)
        {
            addObject(new Coin(),i,272);
        }
        addObject(new SnakeD(),660,520);
        addObject(new Platform(2),800,540);
        addObject(new Snake(),300,359);
        for(int j=1062;j<1183;j+=40)
        {
            for(int i=630;i>470;i-=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        addObject(new SnakeD(),1142,479);
        for(int i=1062;i<1183;i+=40)
        {
            addObject(new Coin(),i,340);
        }
        addObject(new Block(1),1294,461);
        addObject(new Block(1),1387,412);
        addObject(new Block(1),1487,501);
        addObject(new Block(1),1599,575);
        addObject(new Block(1),1681,440);
        addObject(new Cliff(4),1961,555);
        for(int i=2034;i>1988;i-=45)
        {
            addObject(new Snake(-2),i,447);
        }
        for(int i=1807;i<2116;i+=154)
        {
            addObject(new Box(4),i,354);
        }
        for(int i=1807;i<1913;i+=35)
        {
            addObject(new Coin(),i,239);
        }
        addObject(new Box(2),1961,239);
        for(int i=2010;i<2116;i+=35)
        {
            addObject(new Coin(),i,239);
        }
        int b=470;
        for(int j=2245;j<2726;j+=40)
        {
            for(int i=630;i>b;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j==2365)
                b-=120;
            else if(j==2405)
                b+=120;
            else if(j==2725)
                b-=120;
        }
        addObject(new Creature(),2400,440);
        int c=509;
        for(int j=2805;j<2966;j+=40)
        {
            for(int i=510;i>c;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>2805)
                c=389;
        }
        int g=590;
        int h=509;
        for(int j=2765;j<3206;j+=40)
        {
            for(int i=g;i>h;i-=40)
            {
                addObject(new Block(4),j,i);
            }
            if(j==2765)
                h+=40;
            if(j>2925
                h-=40;
            if(j>2965)
                g-=40;
        }
        int d=629;
        for(int j=2765;j<3206;j+=40)
        {
            for(int i=630;i>d;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>2965)
                d-=40;
        }
        int e=470;
        int f=389;
        for(int j=3005;j<3206;j+=40)
        {
            for(int i=e;i>f;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            e-=40;
            if(j>3005)
                f-=40;
        }
        addObject(new SnakeD(),3100,360);
        for(int i=2765;i<2966;i+=40)
        {
            addObject(new Coin(),i,595);
        }
        for(int i=2885;i<3086;i+=40)
        {
            addObject(new Coin(),i,275);
        }
        for (int j=3305; j<3546; j+=40)
        {
            for (int i=486; i<647; i+=40)
            {
                addObject(new Block(3),j,i);
            }
        }
        addObject(new Door(),3492,433);
        addObject(new DoorPost(),3492,433);
    }
 
    /**
     * The level 3 constructor
     */
    private void level3()
    {
        reset(false);
        pengu=new Pengu();
        addObject(pengu,60,470);
        for(int j=-20;j<181;j+=40)
        {
            for(int i=630;i>509;i-=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        for(int i=260;i<500;i+=40)
        {
            addObject(new Coin(),i,390);
        }
        addObject(new Platform(2),300,520);
        int a=489;
        for(int j=500;j<781;j+=40)
        {
            for(int i=630;i>a;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            a-=40;
        }
        for(int i=860;i<1021;i+=40)
        {
            addObject(new Coin(),i,390);
        }
        addObject(new Snake(),780,197);
        addObject(new Platform(2),910,520);
        addObject(new Cliff(1),1140,555);
        addObject(new SnakeD(),1140,447);
        int b=449;
        for(int j=1345;j<1826;j+=40)
        {
            for(int i=630;i>b;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>1344&&j<1425)
                b-=40;
            if(j>1424&&j<1505)
                b+=40;
            if(j>1504&&j<1585)
                b-=40;
            if(j>1584&&j<1665)
                b+=40;
            if(j>1664&&j<1745)
                b-=40;
            if(j>1744)
                b+=40;
        }
        for (int i=1425;i<1784;i+=160)
        {
            addObject(new Box(1),i,250);
        }
        addObject(new Creature(),1585,370);
        for(int j=1965;j<2286;j+=40)
        {
            for(int i=630;i>549;i-=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        addObject(new Cliff(4),2126,435);
        addObject(new SnakeD(),1966,327);
        addObject(new SnakeD(),2286,327);
        for(int i=2102;i<2223;i+=40)
        {
            addObject(new Box(1),i,220);
        }
        int c=2660;
        addObject(new Cliff(4),c+185,589);
        addObject(new Cliff(2),c,525);
        addObject(new Cliff(2),c+316,525);
        addObject(new Box(4),c+75,384);
        addObject(new Box(2),c+115,384);
        addObject(new Box(3),c+155,384);
        addObject(new Snake(),c+90,523);
        addObject(new Snake(),c+150,523);
        addObject(new Platform(4),2560,400);
        int d=589;
        for(int j=3105;j<3346;j+=40)
        {
            for(int i=630;i>d;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>3145&&j<3225)d=349;
            if(j>3264)d=469;
        }
        addObject(new Box(4),3145,450);
        addObject(new Coin(),3225,315);
        addObject(new Coin(),3265,315);
        addObject(new Block(1),3885,495);
        for (int j=4385; j<4626; j+=40)
        {
            for (int i=486; i<647; i+=40)
            {
                addObject(new Block(3),j,i);
            }
        }
        addObject(new Door(),4572,433);
        addObject(new DoorPost(),4572,433);
        addObject(new Platform(4),3635,495);
        addObject(new Platform(4),4135,495);
    }
 
    /**
     * This method is the method that moves all the side scrolling objects
     */
    private void sideScrolling()
    {
        move = pengu.getX() >= 342 && ! onCloud && ! pengu.touchingRightSide();
        if(isDoor==true||pengu.getX()<342)
        {
            move=false;
        }
        if(Greenfoot.isKeyDown("right")&&move==true)
        {
            for (SideScrollers1 obj: getObjects(SideScrollers1.class))
            {
                Actor actor=(Actor) obj;
                actor.setLocation(actor.getX()-7, actor.getY());
            }
        }
    }
 
    /**
     * This method moves all side scrolling objects back and forth when on a platform (onCloud)
     */
    public void platform()
    {
        for (SideScrollers obj: getObjects(SideScrollers.class))
        {
            Actor actor=(Actor) obj;
            if(way==true)
            {
                actor.setLocation(actor.getX()-4,actor.getY());
            }
            if(way==false)
            {
                actor.setLocation(actor.getX()+4,actor.getY());
            }
        }
    }
 
    /**
     * this method is called when the help button is pressed (and the Help isn't there)
     */
    public void help()
    {
        addObject(new Help(),375,325);
    }
 
    /**
     * This method is called when the help button is pressed (and the Help is there)
     */
    public void help2()
    {
        removeObjects(getObjects(Help.class));
    }
}
Here is my Mover class code, which Pengu extends from and has all the collision checking methods:
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * The class Mover provides some basic movement methods. Use this as a superclass
 * for other actors that should be able to move left and right, jump up and fall
 * down.
 */
public abstract class Mover extends Actor
{
    private static final int acceleration = 2;      // down (gravity)
    private static final int speed = 7;             // running speed (sideways)
    public int vSpeed = 0;                          // current vertical speed
    private boolean hasHit=false;                   // boolean for hitting something onTop
    /**
     * Called in Pengu when right arrow key is pressed
     */
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
 
    /**
     * Called in Pengu when left arrow key is pressed
     */
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
 
    /**
     * The next 8 methods are for checking when the Penguin hits something with his head
     *
     * The first 2 are for to make the ifs that onTop are used in smaller
     * The next 3 are for chicking if the penguin hit a (mystery) box the vSpeed<0 are to make sure you hit it on the way up (for invisible box)
     * The last 2 are for checking if the penguin hits a block, the isThere is to make sure it's not [invisible]/[just backgroung color]
     *
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's head can just go through
     */
    public boolean onTop()
    {
        return onTop1B()||onTop2B()||onTop3B();
    }
 
    public boolean onTop2()
    {
        return onTop1Bc()||onTop2Bc()||onTop3Bc();
    }
 
    private boolean onTop1B()
    {
        Box box = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }
 
    private boolean onTop2B()
    {
        Box box = (Box)getOneObjectAtOffset(0, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }
 
    private boolean onTop3B()
    {
        Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }
 
    private boolean onTop1Bc()
    {
        Block block=(Block) getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean onTop2Bc()
    {
        Block block=(Block)  getOneObjectAtOffset(0, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean onTop3Bc()
    {
        Block block=(Block) getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }
 
    /**
     * The next 12 methods are for checking if the Pengu is standing on solid ground
     *
     * The first 4 are for checking if the Pengu is on solid (isThere==true) Box, with the last for shorter ifs
     * The next 4 are for checking if the Pengu is on solid (isthere==true) Block, with the last for shorter ifs
     * the Box and Block can't be together even though both extend Boxes because of isThere
     * The next/last 4 are for checking if the Pengu is on a Cliff, with the last for shorter ifs
     *
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean onGround1B()
    {
        Box box = (Box)getOneObjectAtOffset(0, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }
 
    private boolean onGround2B()
    {
        Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }
 
    private boolean onGround3B()
    {
        Box box  = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }
 
    private boolean onGroundB()
    {
        return onGround1B()||onGround2B()||onGround3B();
    }
 
    private boolean onGround1Bc()
    {
        Block block =(Block) getOneObjectAtOffset(0, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean onGround2Bc()
    {
        Block block =(Block) getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean onGround3Bc()
    {
        Block block =(Block) getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean onGroundBc()
    {
        return onGround1Bc()||onGround2Bc()||onGround3Bc();
    }
 
    private boolean onGround1CA()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }
 
    private boolean onGround2CA()
    {
        Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }
 
    private boolean onGround3CA()
    {
        Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }
 
    private boolean onGroundCA()
    {
        return onGround1CA()||onGround2CA()||onGround3CA();
    }
 
    /**
     * The next 4 methods are for checking if the Pengu is standing on a Platform with the last for shorter ifs
     *
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean onGround1P()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }
 
    private boolean onGround2P()
    {
        Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }
 
    private boolean onGround3P()
    {
        Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }
 
    private boolean onGroundP()
    {
        return onGround1P()||onGround2P()||onGround3P();
    }
 
    public boolean onGround()
    {
        return onGroundBc()||onGroundB()||onGroundCA()||onGroundP();
    }
 
    /**
     * The next 13 methods are for checking if the Pengu hits something on his left side
     *
     * The first 4 are for checking if the Pengu hits solid (isThere==true) Box, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits solid (isThere==true) Block, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits a Cliff, with the last for shorter ifs
     * The last of the 13 methods are to make the ifs even shorter
     *
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean touchingLeftSide1B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
        return box!=null&&box.isThere==true;
    }
 
    private boolean touchingLeftSide2B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
        return box!=null&&box.isThere==true;
    }
 
    private boolean touchingLeftSide3B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,0,Box.class);
        return box!=null&&box.isThere==true;
    }
 
    private boolean touchingLeftSideB()
    {
        return touchingLeftSide1B()||touchingLeftSide2B()||touchingLeftSide3B();
    }
 
    private boolean touchingLeftSide1Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean touchingLeftSide2Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean touchingLeftSide3Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,0,Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean touchingLeftSideBc()
    {
        return touchingLeftSide1Bc()||touchingLeftSide2Bc()||touchingLeftSide3Bc();
    }
 
    private boolean touchingLeftSide1CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Cliff.class);
        return side!=null;
    }
 
    private boolean touchingLeftSide2CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
        return side!=null;
    }
 
    private boolean touchingLeftSide3CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,0,Cliff.class);
        return side!=null;
    }
 
    private boolean touchingLeftSideCA()
    {
        return touchingLeftSide1CA()||touchingLeftSide2CA()||touchingLeftSide3CA();
    }
 
    public boolean touchingLeftSide()
    {
        return touchingLeftSideB()||touchingLeftSideCA()||touchingLeftSideBc();
    }
 
    /**
     * The next 13 methods are for checking if the Pengu hits something on his right side
     *
     * The first 4 are for checking if the Pengu hits solid (isThere==true) Box, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits solid (isThere==true) Block, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits a Cliff, with the last for shorter ifs
     * The last of the 13 methods are to make the ifs even shorter
     *
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean touchingRightSide1B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
        return box!=null&&box.isThere==true;
    }
 
    private boolean touchingRightSide2B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
        return box!=null&&box.isThere==true;
    }
 
    private boolean touchingRightSide3B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,0,Box.class);
        return box!=null&&box.isThere==true;
    }
 
    public boolean touchingRightSideB()
    {
        return touchingRightSide1B()||touchingRightSide2B()||touchingRightSide3B();
    }
 
    private boolean touchingRightSide1Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean touchingRightSide2Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
        return block != null&&block.isThere==true;
    }
 
    private boolean touchingRightSide3Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,0,Block.class);
        return block != null&&block.isThere==true;
    }
 
    public boolean touchingRightSideBc()
    {
        return touchingRightSide1Bc()||touchingRightSide2Bc()||touchingRightSide3Bc();
    }
 
    private boolean touchingRightSide1CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
        return side!=null;
    }
 
    private boolean touchingRightSide2CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-20,Cliff.class);
        return side!=null;
    }
 
    private boolean touchingRightSide3CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,0,Cliff.class);
        return side!=null;
    }
 
    public boolean touchingRightSideCA()
    {
        return touchingRightSide1CA()||touchingRightSide2CA()||touchingRightSide3CA();
    }
 
    public boolean touchingRightSide()
    {
        return touchingRightSideB()||touchingRightSideCA()||touchingRightSideBc();
    }
 
    /**
     * This method sets the vertical speed
     */
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
 
    /**
     * This method is used for jumping and to make the Pengu land onGround if not onGround or if it hit something with his head
     */
    public void fall()
    {
        bump();
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom() )
            gameEnd();
    }
 
    /**
     * This is a boolean if the penguin is at the bottom of the screen (for removing it)
     */
    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }
 
    /**
     * This method is called when the penguin is atBottom it subtracts a life and resets the level
     */
    public void gameEnd()
    {
        Scene scene=(Scene)getWorld();
        scene.lives--;
        scene.reset(true);
        scene.levelUp();
    }
 
    /**
     * This method is for checking if the penguin hit his head on something
     */
    public void bump()
    {
        if (onTop()||onTop2())
        {
            if(!hasHit)
            {
                vSpeed=-vSpeed;
            }
            hasHit=true;
        }
        if(onGround())
        {
            hasHit=false;
        }
    }
}
Here is the Pengu class 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
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * A little penguin that wants to get to the other side.
 */
public class Pengu extends Mover
{
    private static final int jumpStrength = 23; // this is for the height the Pengu jumps
    private int delay=0; // this is so that the bullets don't continuously come out, rather there is a space in between
    private GreenfootImage right=new GreenfootImage("Pengu/pengu-right2.png"); //image of Pengu facing right
    private GreenfootImage left=new GreenfootImage("Pengu/pengu-left2.png"); // image of Pengu facing left
    private GreenfootImage rightShooting=new GreenfootImage("Pengu/pengu-right2Shooting.png"); // image of pengu facing right holding fireballs
    private GreenfootImage leftShooting=new GreenfootImage("Pengu/pengu-left2Shooting.png"); // image of Pengu facing left holding fireballs
    /**
     * Pengu class constructor
     */
    public Pengu()
    {
        setImage(right);
    }
 
    /**
     * Act - do whatever the Pengu wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause) //makes sure the game is not paused
        {
            if(scene.shooting==true)delay++;
            shoot();
            shooting();
            coins();
            checkKeys();      
            checkFall();
        }
    }
 
    /**
     * The movement and images for the Pengu
     */
    private void checkKeys()
    {
        Scene scene=(Scene)getWorld();
        if (Greenfoot.isKeyDown("left")&&getX()>=20 )
        {
            scene.direction=false;
            if(!touchingLeftSide())
                moveLeft();
        }
        if (Greenfoot.isKeyDown("left"))
        {
            if(!scene.shooting)
            {
                setImage(left);
            }
            else if(scene.shooting=true)
            {
                setImage(leftShooting);
            }
        }
        if (Greenfoot.isKeyDown("right")&&scene.move==false)
        {
            scene.direction=true;
            if(!touchingRightSide())
                moveRight();
        }
        if (Greenfoot.isKeyDown("right"))
        {
            if(!scene.shooting)
            {
                setImage(right);
            }
            else if(scene.shooting=true)
            {
                setImage(rightShooting);
            }
        }
        if (Greenfoot.isKeyDown("up") )
        {
            if (onGround())
                jump();
        }
    }   
 
    /**
     * the jumping for the Pengu
     */
    private void jump()
    {
        Scene scene=(Scene)getWorld();
        setVSpeed(-jumpStrength);
        fall();
    }
 
    /**
     * This method checks for retrieving coins
     */
    private void coins()
    {
        Scene scene=(Scene)getWorld();
        Coin coin=(Coin)getOneIntersectingObject(Coin.class);
        if(coin!=null)
        {
            scene.removeObject(coin);
            scene.score(100);
            scene.coins=scene.coins+1;
        }
    }
 
    /**
     * This method is the method that shoots the fireballs
     */
    private void shoot()
    {
        Scene scene=(Scene)getWorld();
        if(Greenfoot.isKeyDown("space")&&delay>=5&&scene.shooting==true)
        {
            if(scene.direction==false)
            {
                scene.addObject(new Bullet(-8),getX()-26,getY());
            }
            if(scene.direction==true)
            {
                scene.addObject(new Bullet(8),getX()+26,getY());
            }
            delay=0;
        }
    }
 
    /**
     * This method gets the shooting power-up and sets the image for the Pengu
     */
    private void shooting()
    {
        Scene scene=(Scene)getWorld();
        if(isTouching(Shooting.class))
        {
            scene.shooting=true;
            scene.score(250);
            removeTouching(Shooting.class);
        }
        if(scene.shooting)
        {
            if(scene.direction==false)
            {
                setImage(leftShooting);
            }
            if(scene.direction==true)
            {
                setImage(rightShooting);
            }
        }
        else if(!scene.shooting)
        {
            if(scene.direction==false)
            {
                setImage(left);
            }
            if(scene.direction==true)
            {
                setImage(right);
            }
        }
    }
 
    /**
     * This method makes sure the Pengu is standing on top of the object that it's touching (but not the door or door post)
     */
    public void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
            Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null);
            if(!checkDoor())
            {
                if(under!=null)
                {
                    int h=under.getImage().getHeight()/2+getImage().getHeight()/2-5;
                    int y=under.getY();
                    setLocation(getX(),y-h);
                }
            }
        }
        else {
            fall();
        }
    }
 
    /**
     * This method is used to see if the Pengu is touching the door or door post
     */
    private boolean checkDoor()
    {
        return isTouching(Door.class)||isTouching(DoorPost.class);
    }
}
Here is the Box class code (collision object, it has variables that need to be checked for/if collision):
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * A mystery box with good things inside for the Pengu
 *
 * @author Nosson1459
 * @version (a version number or a date)
 */
public class Box extends Boxes
{
    private GreenfootImage image1=new GreenfootImage("mystery.png"); // image of box with '?'
    private GreenfootImage image2=new GreenfootImage("MysteryG.png"); // image of gold box with '?'
    private GreenfootImage image3=new GreenfootImage("Block.png"); // image of plain block
    private GreenfootImage image4=new GreenfootImage("transparentBox.png"); //same size as boxes/block but transparent
    private int timer=0; // timer so that there is a space between the changing (flash) image from regular to gold box
    private boolean hit=false; // boolean whether this box has been hit or not
    private int counter=0; // counts the image changes so the mystery box isn't flashing the rest of the level
    private boolean empty=false; // boolean whether this box is empty or not
    private boolean coin=false; // boolean for box containing coin
    private boolean shooting=false; // boolean for box containing shooting power-up
    private boolean invisible=false; // boolean whether box is invivible
    private boolean extraLife=false; // boolean for box containing an extra life
    public boolean isThere=true; // boolean whether the box is solid (yet)
    /**
     * Box class constructor
     * For setting the image and invisibility
     */
    public Box(int type)
    {
        if(type==1)
        {
            invisible=false;
            coin=true;
            shooting=false;
            extraLife=false;
        }
        if(type==2)
        {
            invisible=false;
            shooting=true;
            coin=false;
            extraLife=false;
        }
        if(type==3)
        {
            invisible=false;
            shooting=false;
            coin=false;
            extraLife=true;
        }
        if(type==4)
        {
            invisible=true;
            coin=true;
            shooting=false;
            extraLife=false;
            isThere=false;
        }
        if(type==5)
        {
            invisible=true;
            coin=false;
            shooting=true;
            extraLife=false;
            isThere=false;
        }
        if(type==6)
        {
            invisible=true;
            shooting=false;
            coin=false;
            extraLife=true;
            isThere=false;
        }
        if(invisible==false)
            setImage(image1);
        if(invisible==true)
            setImage(image4);
    }
 
    /**
     * Act - do whatever the Box wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause)
        {
            if(!empty)
            {
                checkPengu();
                hit();
            }
            remove(); // removes the box when it goes off the left side of the screen
        }
    
 
    /**
     * The following 4 methods are to see if the box has been hit by the Pengu
     * the last one makes hit be true
     */
    private boolean checkPengu1()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(0,getImage().getHeight()/2+1,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }
 
    private boolean checkPengu2()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(getImage().getWidth()/2-1,getImage().getHeight()/2,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }
 
    private boolean checkPengu3()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(-getImage().getWidth()/2+1,getImage().getHeight()/2,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }
 
    private void checkPengu()
    {
        if(checkPengu1()||checkPengu2()||checkPengu3())
        {
            hit=true;
        }
    }
 
    /**
     * When hit==true then the Box flashes, the object from inside the box pops up and the points get added to the score
     */
    private void hit()
    {
        Scene scene=(Scene)getWorld();
        if(hit==true)
        {
            timer++;
            if(counter<6)
            {
                if(timer==2&&(getImage()==image1||getImage()==image4))
                {
                    setImage(image2);
                    timer=0;
                    counter++;
                }
                if(timer==2&&getImage()==image2)
                {
                    setImage(image1);
                    timer=0;
                    counter++;
                }
            }
 
        }
        if(counter==5)
        {
            empty=true;
            if(shooting==true)
            {
                scene.addObject(new Shooting(),getX(),getY());
            }
            else if(coin==true)
            {
                scene.coins++;
            }
            else if(extraLife==true)
            {
                scene.addObject(new ExtraLife(),getX(),getY());
            }
            scene.score(100);
            counter=0;
            isThere=true;
            hit=false;
            setImage(image3);
        }
    }
}
Here is the Block class code (collision object, it has variables that need to be checked for/if collision):
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Block here.
 *
 * @author Nosson1459
 * @version (a version number or a date)
 */
public class Block extends Boxes
{
    private GreenfootImage image1=new GreenfootImage("Block.png"); // image of plain block
    private GreenfootImage image2=new GreenfootImage("Bricks2.png"); // image of bricks
    private GreenfootImage image3=new GreenfootImage("EndBricks.png"); // image of different color bricks
    private GreenfootImage image4=new GreenfootImage("Tunnel.png"); // plain green box (whenever used - isThere=false)
    public boolean isThere=true;
    private boolean addObjects=false;
    /**
     * Block class constructor
     * For setting the image and invisibility
     */
    public Block(int type)
    {
        if(type==1)
        {
            setImage(image1);
        }
        if(type==2)
        {
            setImage(image2);
        }
        if(type==3)
        {
            setImage(image3);
        }
        if(type==4)
        {
            setImage(image4);
            isThere=false;
        }
        if(type==5)
        {
            setImage(image1);
            addObjects=true;
        }
    }
 
    /**
     * Act - do whatever the Block wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause)
        {
            remove(); // removes the block when it goes off the left side of the screen
        }
    }
}
I didn't post the cliff class code, because the only thing that it has to do with collision is the picture, no coding.
Nosson1459 Nosson1459

2016/12/20

#
"with no replies"
You need to login to post a reply.