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

2018/12/26

Game Creation: Directional Idle, Shooting and Jumping.

1
2
3
4
Unlimited Unlimited

2018/12/26

#
Hello everyone, im trying to create a game for class and im having a couple of problems. for example i cannot seem to figure out how to animate directional idle animations and shooting animations. Having a idle animation in one direction was simple enough since i only animated one direction. However i dont know how to tell the programm in what direction i have previously used as in to give my game information what idle or standing shooting animation to use. Next Problem is, if i want to create an improved jump i am facing two problems. First, i do not know how to program a jump in which i can control the high of so to give it that megaman feel. Second, nor do i know if i am able to create such a jump how to tell the game to play a different animation after the apex of the jump and when landing rather then after some unchangable time . The following is the entire code of my Character. It would be really nice if somebody could help me. I know there are probably already threats about these problems but despite my hardest effort i was not able to understand them.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * This class is just an example. You can delete it or change the code.
 * It's not necessary for the scrolling system.
 */
public class Azure extends ScrollingActor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 20;
     
     
     
     
    
     
    private GreenfootImage Stand1 = new GreenfootImage("Stand_01.png");
    private GreenfootImage Stand2 = new GreenfootImage("Stand_02.png");
    private GreenfootImage Stand3 = new GreenfootImage("Stand_03.png");
    private GreenfootImage Stand4 = new GreenfootImage("Stand_04.png");
    private GreenfootImage Stand5 = new GreenfootImage("Stand_05.png");
    private GreenfootImage Stand6 = new GreenfootImage("Stand_06.png");
    private GreenfootImage Stand7 = new GreenfootImage("Stand_07.png");
    private GreenfootImage Stand8 = new GreenfootImage("Stand_08.png");
    private GreenfootImage Stand9 = new GreenfootImage("Stand_09.png");
     
    private GreenfootImage WalkA1 = new GreenfootImage("Walk-A_01.png");
    private GreenfootImage WalkA2 = new GreenfootImage("Walk-A_02.png");
    private GreenfootImage WalkA3 = new GreenfootImage("Walk-A_03.png");
    private GreenfootImage WalkA4 = new GreenfootImage("Walk-A_04.png");
    private GreenfootImage WalkA5 = new GreenfootImage("Walk-A_05.png");
    private GreenfootImage WalkA6 = new GreenfootImage("Walk-A_06.png");
    private GreenfootImage WalkA7 = new GreenfootImage("Walk-A_07.png");
    private GreenfootImage WalkA8 = new GreenfootImage("Walk-A_08.png");
    private GreenfootImage WalkA9 = new GreenfootImage("Walk-A_09.png");
    private GreenfootImage WalkA10 = new GreenfootImage("Walk-A_10.png");
    private GreenfootImage WalkA11 = new GreenfootImage("Walk-A_11.png");
     
    private int frame = 1;
    private int animationCounter = 0;
    
     
    
     
 
    /**
     * Here you can tell your actor what he has to do.
     */
    public void act() {
         
         
         
         
        ceiling();
         
         
         
        animationCounter ++;
         
        if (Greenfoot.isKeyDown("space")) {
            setLocation(getX(), getY() - 20);
             
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 10);
             
        }
         
         
         
        if (Greenfoot.isKeyDown("left") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() - 15, getY());
        }
        if (Greenfoot.isKeyDown("right") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() + 15, getY());
        }
        move();
        canMoveRight();
        canMoveLeft();
        checkFall();
         
         
    }
     
     
    public void move()
    {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("right") && canMoveRight()) x+=14;
         
         
        if(Greenfoot.isKeyDown("left") && canMoveLeft()) x-=14;
         
         
         
         
             
         
         
        if(Greenfoot.isKeyDown("right") && canMoveRight()){
            if(animationCounter % 2 == 0)
                animateWalkA();       
            }
            else
            {
                 if(animationCounter % 6 == 0)
                 animateStand();
            }
        setLocation(x,y);
    }
     
     
     
    
     
 
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
         
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
     
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int) (spriteHeight/2+30);
         
        Actor ground = getOneObjectAtOffset(0, lookForGround, Highway.class);
         
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
         
    }
     
    public boolean ceiling()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight/-2-30);
         
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Highway.class);
         
        if(ceiling != null)
        {
            vSpeed =1;
            hitHead(ceiling);
            return true;
        }
        else
        {
             
            return false;
        }
         
    }
     
     
    public void hitHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
         
        setLocation(getX(), newY);
         
         
    }
     
     
    
     
    public boolean canMoveLeft()
    {
       boolean canMoveLeft = true;
        
       int imageWidth = getImage().getWidth();
       int imageHeight = getImage().getHeight();
       if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Border.class) !=null ||
       getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2, Border.class) !=null)
            canMoveLeft = false;
        
       return canMoveLeft;
         
    }
     
    public boolean canMoveRight()
    {
        boolean canMoveRight = true;
        
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Border.class) !=null ||
        getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2, Border.class) !=null)
            canMoveRight = false;
        
        return canMoveRight;
    }
     
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
         
        setLocation(getX(), newY);
        jumping = false;
         
    }
     
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
             
        }
        else
        {
            fall();
        }
 
    }
     
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
         
         
        
    }
     
     
     
     
     
     
    public void animateStand()
    {
        if(frame == 1)
        {
            setImage(Stand1);
             
        }
        else if(frame == 2)
        {
            setImage(Stand2);
             
        }
        else if(frame == 3)
        {
            setImage(Stand3);
             
        }
        else if(frame == 4)
        {
            setImage(Stand4);
        }
        else if(frame == 5)
        {
            setImage(Stand5);
             
        }
        else if(frame == 6)
        {
            setImage(Stand6);
             
        }
        else if(frame == 7)
        {
            setImage(Stand7);
             
        }
        else if(frame == 8)
        {
            setImage(Stand8);
             
        }
        else if(frame == 9)
        {
            setImage(Stand9);
            frame = 1;
            return;
       }
       frame ++;
    }
     
    public void animateWalkA()
    {
        if(frame == 1)
        {
            setImage(WalkA1);
             
        }
        else if(frame == 2)
        {
            setImage(WalkA2);
             
        }
        else if(frame == 3)
        {
            setImage(WalkA3);
             
        }
        else if(frame == 4)
        {
            setImage(WalkA4);
        }
        else if(frame == 5)
        {
            setImage(WalkA5);
             
        }
        else if(frame == 6)
        {
            setImage(WalkA6);
             
        }
        else if(frame == 7)
        {
            setImage(WalkA7);
             
        }
         else if(frame == 8)
        {
            setImage(WalkA8);
             
        }
         else if(frame == 9)
        {
            setImage(WalkA9);
             
        }
         else if(frame == 10)
        {
            setImage(WalkA10);
             
        }
        else if(frame == 11)
        {
            setImage(WalkA11);
            frame = 3;
            return;
       }
       frame ++;
    }
     
     
}
danpost danpost

2018/12/26

#
To change jump height, just add or subtract a small value (maybe up to 10) when applying jumpstrength to vSpeed. Maybe adding a parameter to the jump method would help, to pass the jump strength to it:
1
2
3
4
5
6
private void jump(int strength)
{
    vSpeed = -strength; // strength should be set to vSpeed, not just subtracted from its value
    jumping = true;
    fall();
}
Then a normal jump would be coded:
1
jump(jumpStrength);
and a small jump might be:
1
jump(jumpStrength-10);
For better control on your animations, you might consider placing each animation image set in a separate array. Then you can add a field to indicate which array of images is currently in use. You could also add a field to indicate which animation set is "pending" to be used next.
Unlimited Unlimited

2018/12/26

#
Uh yes that entire last part, could you explain that a little more as in what exactly would i have to write? I am a Novice at this so im sorry for the troubles. I am yet not able to make up my own code, i can only look up whats been done before.
danpost danpost

2018/12/26

#
Unlimited wrote...
Uh yes that entire last part, could you explain that a little more as in what exactly would i have to write?
By placing the images of your animation sets into arrays:
1
2
3
4
5
6
7
8
private static GreenfootImage[] standAnim = new GreenfootImage[9];
private static GreenfootImage[] rightAnim; = new GreenfootImage[11];
 
static
{
    for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
    for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
}
they will be organized in groups. The 'static' modifier makes the members (fields declared in lines 1 and 2) class members, as opposed to instance members, allowing the arrays to be set up at compile time instead of during run time. This is allowed, as all instances of the class can use the same image sets. Next, you can add an instance field to indicate which image set is currently being used by an actor of the class:
1
private GreenfootImage[] animSet = standAnim;
Initially setting it to the standing image set makes sense as no key states are evaluated until the act method is executed on the actor. Finally, running the current animation needs to be done with a little care as the animation sets are not all of equal length. Also, your frame and animationCounter fields can be combined into one counter (animCount). However, because they do not all have equal frame rates, you will need another field for its value and it must be adjusted any time you change the current animation set:
1
2
3
4
5
6
7
8
9
10
11
12
// with the following fields
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)
 
// this method might help
private void setAnim(GreenfootImage[] anim, int frameRate)
{
    animSet = anim;
    animCount = -1; // prepares to set first image when incremented
    animDelay = frameRate;
    animate(); // calls a method to set a frame of this newly set animation
}
The animate method can be written as follows:
1
2
3
4
5
private void animate()
{
    animCount = (animCount+1)%(animSet.length*animDelay);
    if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay];
}
That is it. You can dispense with your animateStand and animateWalkA codes.
danpost danpost

2018/12/26

#
Sidenote: I was never much a fan of "if (onGround()) { ... } else fall();" code. This is akin to saying gravity does not exist unless you are up in the air. I prefer:
1
2
3
4
5
6
7
8
9
10
11
12
boolean onGround = false; // assume in air
vSpeed += acceleration; // apply gravity
setLocation(getX(), getY()+vSpeed); // fall
Actor actor = getOneIntersectingObject(Actor.class);
if (actor != null)
{ // colliding with another actor
    int vDir = (int)Math.signum(vSpeed); // vertical direction of movement
    vSpeed = 0; // kill vertical speed
    if (vDir > 0) onGround = true; // landing and not bopping head
    setLocation(getX(), actor.getY()-vDir*(actor.getImage().getHeight()+getImage().getHeight())/2); // assigning stop location
}
if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -20; // initiate a jump
This does pretty much everything for vertical movement (initiating a jump, falling and colliding with other actors).
Unlimited Unlimited

2018/12/26

#
I really appreciate the effort but i have to say this is all a little complicated for me. All that code, i dont understand from where its coming. When i copy it i also get errors so i dont know what to do. I tried using a Counter for a work around as following.
1
2
3
4
5
6
7
8
9
public void Counter()
    {
         
        if(Greenfoot.isKeyDown("right"))
        Counter = 0;
         
        if(Greenfoot.isKeyDown("left"))
        Counter = 1;
    }
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
public void animationCheckRight()
    {
         
         
        if(Greenfoot.isKeyDown("right") && canMoveRight() )
        {
            if(animationCounter % 4 == 0){
                animateWalkA();
                 
            }
            }
            else
            {
                if(animationCounter % 6 == 0 && Counter==0){
                 animateStand();
                  
                }
            }
         
         
    }
     
    public void animationCheckLeft()
    {
         
         
         
        if(Greenfoot.isKeyDown("left") && canMoveRight() )
        {
            if(animationCounter % 4 == 0){
                animateWalkLeftA();
                 
            }
        }
    }
And this seems to work i think, as in when i press left he does the proper animation. However now i have the problem after a couple of seconds if i stop running there is like a 50% chance that the animation breaks. This happens regartless of where i have the animation. It doesnt matter if i have it in move, act or in its own thing. Do you maybe a have a idea on why that happens?
Unlimited Unlimited

2018/12/26

#
I really have a hard time understanding what youre writing, is there maybe an easier way for the programm to not keep on breaking the animation or like telling it to restart?
danpost danpost

2018/12/27

#
Unlimited wrote...
I really appreciate the effort but i have to say this is all a little complicated for me. All that code, i dont understand from where its coming.
You can ask about any specific part of it for clarification (one part at a time).
When i copy it i also get errors so i dont know what to do. I tried using a Counter for a work around as following. << Codes Omitted >> And this seems to work i think, as in when i press left he does the proper animation. However now i have the problem after a couple of seconds if i stop running there is like a 50% chance that the animation breaks. This happens regartless of where i have the animation. It doesnt matter if i have it in move, act or in its own thing. Do you maybe a have a idea on why that happens?
You should not need a counter. Please show the current class codes (in its entirety). Also, if you are getting errors on it, copy/paste them here so we can better understand what might be going on.
Unlimited Unlimited

2018/12/27

#
This is the entire code. Im getting errors at: private static GreenfootImage rightAnim; = new GreenfootImage; But that is Probably because there is a ; to many. Then in the same line im getting errors at the . However if i remove the ; after rightAnim im getting errors at all these lines: private void setAnim(GreenfootImage anim, int frameRate) { animSet = anim; animCount = -1; // prepares to set first image when incremented animDelay = frameRate; animate(); // calls a method to set a frame of this newly set animation } // with the following fields private int animCount; // counter for animations private int animDelay = 6; // initially set for standing animation (frame rate field) // this method might help private void setAnim(GreenfootImage anim, int frameRate) { animSet = anim; animCount = -1; // prepares to set first image when incremented animDelay = frameRate; animate(); // calls a method to set a frame of this newly set animation }
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * This class is just an example. You can delete it or change the code.
 * It's not necessary for the scrolling system.
 */
public class Azure extends ScrollingActor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 20;
     
     
     
     
  
     
     
    private GreenfootImage Stand1 = new GreenfootImage("Stand_01.png");
    private GreenfootImage Stand2 = new GreenfootImage("Stand_02.png");
    private GreenfootImage Stand3 = new GreenfootImage("Stand_03.png");
    private GreenfootImage Stand4 = new GreenfootImage("Stand_04.png");
    private GreenfootImage Stand5 = new GreenfootImage("Stand_05.png");
    private GreenfootImage Stand6 = new GreenfootImage("Stand_06.png");
    private GreenfootImage Stand7 = new GreenfootImage("Stand_07.png");
    private GreenfootImage Stand8 = new GreenfootImage("Stand_08.png");
    private GreenfootImage Stand9 = new GreenfootImage("Stand_09.png");
     
    private GreenfootImage StandLeft1 = new GreenfootImage("StandLeft_09.png");
    private GreenfootImage StandLeft2 = new GreenfootImage("StandLeft_08.png");
    private GreenfootImage StandLeft3 = new GreenfootImage("StandLeft_07.png");
    private GreenfootImage StandLeft4 = new GreenfootImage("StandLeft_06.png");
    private GreenfootImage StandLeft5 = new GreenfootImage("StandLeft_05.png");
    private GreenfootImage StandLeft6 = new GreenfootImage("StandLeft_04.png");
    private GreenfootImage StandLeft7 = new GreenfootImage("StandLeft_03.png");
    private GreenfootImage StandLeft8 = new GreenfootImage("StandLeft_02.png");
    private GreenfootImage StandLeft9 = new GreenfootImage("StandLeft_01.png");
     
    private GreenfootImage WalkA1 = new GreenfootImage("Walk-A_01.png");
    private GreenfootImage WalkA2 = new GreenfootImage("Walk-A_02.png");
    private GreenfootImage WalkA3 = new GreenfootImage("Walk-A_03.png");
    private GreenfootImage WalkA4 = new GreenfootImage("Walk-A_04.png");
    private GreenfootImage WalkA5 = new GreenfootImage("Walk-A_05.png");
    private GreenfootImage WalkA6 = new GreenfootImage("Walk-A_06.png");
    private GreenfootImage WalkA7 = new GreenfootImage("Walk-A_07.png");
    private GreenfootImage WalkA8 = new GreenfootImage("Walk-A_08.png");
    private GreenfootImage WalkA9 = new GreenfootImage("Walk-A_09.png");
    private GreenfootImage WalkA10 = new GreenfootImage("Walk-A_10.png");
    private GreenfootImage WalkA11 = new GreenfootImage("Walk-A_11.png");
     
    private GreenfootImage WalkLeftA1 = new GreenfootImage("WalkLeft-A_13.png");
    private GreenfootImage WalkLeftA2 = new GreenfootImage("WalkLeft-A_12.png");
    private GreenfootImage WalkLeftA3 = new GreenfootImage("WalkLeft-A_11.png");
    private GreenfootImage WalkLeftA4 = new GreenfootImage("WalkLeft-A_10.png");
    private GreenfootImage WalkLeftA5 = new GreenfootImage("WalkLeft-A_09.png");
    private GreenfootImage WalkLeftA6 = new GreenfootImage("WalkLeft-A_08.png");
    private GreenfootImage WalkLeftA7 = new GreenfootImage("WalkLeft-A_07.png");
    private GreenfootImage WalkLeftA8 = new GreenfootImage("WalkLeft-A_06.png");
    private GreenfootImage WalkLeftA9 = new GreenfootImage("WalkLeft-A_05.png");
    private GreenfootImage WalkLeftA10 = new GreenfootImage("WalkLeft-A_04.png");
    private GreenfootImage WalkLeftA11 = new GreenfootImage("WalkLeft-A_03.png");
     
    private int frame = 1;
    private int animationCounter = 0;
    private int Counter = 0;
     
     
     
     
    private static GreenfootImage[] standAnim = new GreenfootImage[9];
private static GreenfootImage[] rightAnim; = new GreenfootImage[11];
  
static
{
    for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
    for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
}
 
private GreenfootImage[] animSet = standAnim;
 
    /**
     * Here you can tell your actor what he has to do.
     */
    public void act() {
         
         
         
         
         
         
         
         
        animationCounter ++;
         
        if (Greenfoot.isKeyDown("space")) {
            setLocation(getX(), getY() - 20);
             
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 10);
             
        }
         
         
         
        if (Greenfoot.isKeyDown("left") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() - 15, getY());
        }
        if (Greenfoot.isKeyDown("right") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() + 15, getY());
        }
        
        move();
        Counter();
        canMoveRight();
        canMoveLeft();
        checkFall();
        ceiling();
         
    }
     
     
    // with the following fields
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)
  
// this method might help
private void setAnim(GreenfootImage[] anim, int frameRate)
{
    animSet = anim;
    animCount = -1; // prepares to set first image when incremented
    animDelay = frameRate;
    animate(); // calls a method to set a frame of this newly set animation
}
     
 // with the following fields
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)
  
// this method might help
private void setAnim(GreenfootImage[] anim, int frameRate)
{
    animSet = anim;
    animCount = -1; // prepares to set first image when incremented
    animDelay = frameRate;
    animate(); // calls a method to set a frame of this newly set animation
}  
 
 
    public void Counter()
    {
         
        if(Greenfoot.isKeyDown("right"))
        Counter = 0;
         
        if(Greenfoot.isKeyDown("left"))
        Counter = 1;
    }
     
    public void move()
    {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("right") && canMoveRight()) x+=6;
        {
            WalkRight();
        }
         
         
        if(Greenfoot.isKeyDown("left") && canMoveLeft()) x-=6;
        {
            WalkLeft();
        }
         
        setLocation(x,y);
    }
     
    public void WalkRight()
    {
        if(Greenfoot.isKeyDown("right") )
        {
            if(animationCounter % 4 == 0){
                animateWalkA();
                 
            }
            }
            else
            {
                if(animationCounter % 6 == 0 && Counter==0){
                 animateStand();
                  
                }
            }
    }
     
    public void WalkLeft()
    {
        if(Greenfoot.isKeyDown("left") )
        {
            if(animationCounter % 4 == 0){
                animateWalkLeftA();
                 
            }
        }
         
            else
            {
                if(animationCounter % 6 == 0 && Counter==1){
                 animateStandLeft();
                  
                }
            }
    }
     
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
         
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
     
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int) (spriteHeight/2+30);
         
        Actor ground = getOneObjectAtOffset(0, lookForGround, Highway.class);
         
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
         
    }
     
    public boolean ceiling()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight/-2-30);
         
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Highway.class);
         
        if(ceiling != null)
        {
            vSpeed =1;
            hitHead(ceiling);
            return true;
        }
        else
        {
             
            return false;
        }
         
    }
     
     
    public void hitHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
         
        setLocation(getX(), newY);
         
         
    }
     
     
    
     
    public boolean canMoveLeft()
    {
       boolean canMoveLeft = true;
        
       int imageWidth = getImage().getWidth();
       int imageHeight = getImage().getHeight();
       if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Border.class) !=null ||
       getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2, Border.class) !=null)
            canMoveLeft = false;
        
       return canMoveLeft;
         
    }
     
    public boolean canMoveRight()
    {
        boolean canMoveRight = true;
        
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Border.class) !=null ||
        getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2, Border.class) !=null)
            canMoveRight = false;
        
        return canMoveRight;
    }
     
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
         
        setLocation(getX(), newY);
        jumping = false;
         
    }
     
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
             
        }
        else
        {
            fall();
        }
 
    }
     
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
         
         
        
    }
     
     
         
         
     
     
     
     
    public void animateStand()
    {
        if(frame == 1)
        {
            setImage(Stand1);
             
        }
        else if(frame == 2)
        {
            setImage(Stand2);
             
        }
        else if(frame == 3)
        {
            setImage(Stand3);
             
        }
        else if(frame == 4)
        {
            setImage(Stand4);
        }
        else if(frame == 5)
        {
            setImage(Stand5);
             
        }
        else if(frame == 6)
        {
            setImage(Stand6);
             
        }
        else if(frame == 7)
        {
            setImage(Stand7);
             
        }
        else if(frame == 8)
        {
            setImage(Stand8);
             
        }
        else if(frame == 9)
        {
            setImage(Stand9);
            frame = 1;
            return;
       }
       frame ++;
    }
     
    public void animateWalkA()
    {
        if(frame == 1)
        {
            setImage(WalkA1);
             
        }
        else if(frame == 2)
        {
            setImage(WalkA2);
             
        }
        else if(frame == 3)
        {
            setImage(WalkA3);
             
        }
        else if(frame == 4)
        {
            setImage(WalkA4);
        }
        else if(frame == 5)
        {
            setImage(WalkA5);
             
        }
        else if(frame == 6)
        {
            setImage(WalkA6);
             
        }
        else if(frame == 7)
        {
            setImage(WalkA7);
             
        }
         else if(frame == 8)
        {
            setImage(WalkA8);
             
        }
         else if(frame == 9)
        {
            setImage(WalkA9);
             
        }
         else if(frame == 10)
        {
            setImage(WalkA10);
             
        }
        else if(frame == 11)
        {
            setImage(WalkA11);
            frame = 3;
            return;
       }
       frame ++;
    }
     
    public void animateWalkLeftA()
    {
        if(frame == 1)
        {
            setImage(WalkLeftA1);
             
        }
        else if(frame == 2)
        {
            setImage(WalkLeftA2);
             
        }
        else if(frame == 3)
        {
            setImage(WalkLeftA3);
             
        }
        else if(frame == 4)
        {
            setImage(WalkLeftA4);
        }
        else if(frame == 5)
        {
            setImage(WalkLeftA5);
             
        }
        else if(frame == 6)
        {
            setImage(WalkLeftA6);
             
        }
        else if(frame == 7)
        {
            setImage(WalkLeftA7);
             
        }
         else if(frame == 8)
        {
            setImage(WalkLeftA8);
             
        }
         else if(frame == 9)
        {
            setImage(WalkLeftA9);
             
        }
         else if(frame == 10)
        {
            setImage(WalkLeftA10);
             
        }
        else if(frame == 11)
        {
            setImage(WalkLeftA11);
            frame = 3;
            return;
       }
       frame ++;
    }
    public void animateStandLeft()
    {
        if(frame == 1)
        {
            setImage(StandLeft1);
             
        }
        else if(frame == 2)
        {
            setImage(StandLeft2);
             
        }
        else if(frame == 3)
        {
            setImage(StandLeft3);
             
        }
        else if(frame == 4)
        {
            setImage(StandLeft4);
        }
        else if(frame == 5)
        {
            setImage(StandLeft5);
             
        }
        else if(frame == 6)
        {
            setImage(StandLeft6);
             
        }
        else if(frame == 7)
        {
            setImage(StandLeft7);
             
        }
        else if(frame == 8)
        {
            setImage(StandLeft8);
             
        }
        else if(frame == 9)
        {
            setImage(StandLeft9);
            frame = 1;
            return;
       }
       frame ++;
    }
}
Unlimited Unlimited

2018/12/27

#
Also ive added the walk left and stand left animations.
danpost danpost

2018/12/27

#
Unlimited wrote...
Im getting errors at: private static GreenfootImage rightAnim; = new GreenfootImage; But that is Probably because there is a ; to many.
Correct. The first one on that line should be removed.
However if i remove the ; after rightAnim im getting errors at all these lines: << Code Omitted >>
Not sure as to why that would be. Although, you are missing the animate method I provided which is called from the setAnim method.
danpost danpost

2018/12/27

#
Unlimited wrote...
Also ive added the walk left and stand left animations.
I see. However, you have not removed any of the lines that was to be replaced with the codes I provided, which was pretty much ALL your previously given animation codes. Your added animations should be done similar to what I provided, as well.
Unlimited Unlimited

2018/12/27

#
Im sorry i dont think i understand where to put all of this and what do with the errors. Im currently trying to add the gravity part you added and the animations but i dont know what parts need to be removed. I know this is asking for a lot but could you maybe give me the whole code of how you want it to be. As in to show me what needs to be where and what needs to be removed so i can use that as reference ?
danpost danpost

2018/12/27

#
Unlimited wrote...
Im sorry i dont think i understand where to put all of this and what do with the errors. Im currently trying to add the gravity part you added and the animations but i dont know what parts need to be removed.
Using the code that you originally gave (in your first posting), the parts being replaced (and that would need to be removed) are as follows: * lines 19 through 39 (replaced by static content) * line 42 (replaced by animCount) * lines 104 through 111 (to be replaced later) * lines 251 through 359 (replaced with animate method I provided, which you as yet have not added) The animate method will also need to be called from somewhere in the act method.
Unlimited Unlimited

2018/12/27

#
Ok thanks for that input, this is what i have now.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * This class is just an example. You can delete it or change the code.
 * It's not necessary for the scrolling system.
 */
public class Azure extends ScrollingActor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 20;
     
     
     
     
  
     
     
    private static GreenfootImage[] standAnim = new GreenfootImage[9];
    private static GreenfootImage[] rightAnim = new GreenfootImage[11];
  
    static
    {
        for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
        for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
        for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("WalkLeft-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
    }
     
     
    private int frame = 1;
    // with the following fields
    private int animCount; // counter for animations
    private int animDelay = 6; // initially set for standing animation (frame rate field)
  
    // this method might help
    private void setAnim(GreenfootImage[] anim, int frameRate)
    {
        animSet = anim;
        animCount = -1; // prepares to set first image when incremented
        animDelay = frameRate;
        animate(); // calls a method to set a frame of this newly set animation
    }
    private int Counter = 0;
     
     
    private GreenfootImage[] animSet = standAnim;
     
     
     
     
    /**
     * Here you can tell your actor what he has to do.
     */
    public void act() {
         
         
         
         
         
         
         
         
         
         
        if (Greenfoot.isKeyDown("space")) {
            setLocation(getX(), getY() - 20);
             
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 10);
             
        }
         
         
         
        if (Greenfoot.isKeyDown("left") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() - 15, getY());
        }
        if (Greenfoot.isKeyDown("right") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() + 15, getY());
        }
        animate();
        move();
        Counter();
        canMoveRight();
        canMoveLeft();
         
        ceiling();
         
    }
     
     
    
 
    public void Counter()
    {
         
        if(Greenfoot.isKeyDown("right"))
        Counter = 0;
         
        if(Greenfoot.isKeyDown("left"))
        Counter = 1;
    }
     
    public void move()
    {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("right") && canMoveRight()) x+=6;
        
         
         
        if(Greenfoot.isKeyDown("left") && canMoveLeft()) x-=6;
        
         
        setLocation(x,y);
    }
     
     
     
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
         
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
     
    public void onGround()
    {
         boolean onGround = false;
         // assume in air
       vSpeed += acceleration; // apply gravity
       setLocation(getX(), getY()+vSpeed); // fall
       Actor actor = getOneIntersectingObject(Actor.class);
       if (actor != null)
       { // colliding with another actor
           int vDir = (int)Math.signum(vSpeed); // vertical direction of movement
           vSpeed = 0; // kill vertical speed
           if (vDir > 0) onGround = true; // landing and not bopping head
           setLocation(getX(), actor.getY()-vDir*(actor.getImage().getHeight()+getImage().getHeight())/2+30); // assigning stop location
        }
        if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -20; // initiate a jump
    }
         
         
         
         
         
     
     
     
     
    public boolean ceiling()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight/-2-30);
         
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Highway.class);
         
        if(ceiling != null)
        {
            vSpeed =1;
            hitHead(ceiling);
            return true;
        }
        else
        {
             
            return false;
        }
         
    }
     
     
    public void hitHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
         
        setLocation(getX(), newY);
         
         
    }
     
     
    
     
    public boolean canMoveLeft()
    {
       boolean canMoveLeft = true;
        
       int imageWidth = getImage().getWidth();
       int imageHeight = getImage().getHeight();
       if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Border.class) !=null ||
       getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2, Border.class) !=null)
            canMoveLeft = false;
        
       return canMoveLeft;
         
    }
     
    public boolean canMoveRight()
    {
        boolean canMoveRight = true;
        
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Border.class) !=null ||
        getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2, Border.class) !=null)
            canMoveRight = false;
        
        return canMoveRight;
    }
     
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
         
        setLocation(getX(), newY);
        jumping = false;
         
    }
     
     
     
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
         
         
        
    }
     
     
         
         
     
     
     
     
    private void animate()
    {
        animCount = (animCount+1)%(animSet.length*animDelay);
        if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay]);
    }
}
There are more replies on the next page.
1
2
3
4