@@ -2,16 +2,7 @@ enum CharacterState {
2
2
IDLE , RUN , WALK
3
3
}
4
4
5
- class Character extends ScratchSprite {
6
-
7
- int runAnimationFrame = 0 ;
8
- String [] runAnimation;
9
-
10
- int idleAnimationFrame = 0 ;
11
- String [] idleAnimation;
12
-
13
- int walkAnimationFrame = 0 ;
14
- String [] walkAnimation;
5
+ class Character extends ScratchAnimatedSprite {
15
6
16
7
CharacterState state;
17
8
@@ -27,26 +18,9 @@ class Character extends ScratchSprite {
27
18
this . addSound(" bump" , " assets/bump.wav" );
28
19
this . addSound(" run" , " assets/run.wav" );
29
20
30
- this . addTimer(" idle" );
31
- idleAnimation = new String [idleAnimations];
32
- for (int i = 1 ; i <= idleAnimations; i++ ) {
33
- this . addCostume(" idle_" + i, pathBase + " Idle (" + i + " ).png" );
34
- idleAnimation[i- 1 ] = " idle_" + i;
35
- }
36
-
37
- this . addTimer(" run" );
38
- runAnimation = new String [runAnimations];
39
- for (int i = 1 ; i <= runAnimations; i++ ) {
40
- this . addCostume(" run_" + i, pathBase + " Run (" + i + " ).png" );
41
- runAnimation[i- 1 ] = " run_" + i;
42
- }
43
-
44
- this . addTimer(" walk" );
45
- walkAnimation = new String [walkAnimations];
46
- for (int i = 1 ; i <= walkAnimations; i++ ) {
47
- this . addCostume(" walk_" + i, pathBase + " Walk (" + i + " ).png" );
48
- walkAnimation[i- 1 ] = " walk_" + i;
49
- }
21
+ this . addAnimation(" idle" , pathBase + " Idle (%d).png" , idleAnimations);
22
+ this . addAnimation(" run" , pathBase + " Run (%d).png" , runAnimations);
23
+ this . addAnimation(" walk" , pathBase + " Walk (%d).png" , walkAnimations);
50
24
51
25
this . setOnEdgeBounce(true );
52
26
this . setSize(30 );
@@ -56,9 +30,8 @@ class Character extends ScratchSprite {
56
30
this . setRotation(random (0 , 360 ));
57
31
}
58
32
59
- void draw () {
33
+ void run () {
60
34
this . setTint(this . tintColor);
61
- super . draw();
62
35
63
36
if (isTouchingMousePointer()) {
64
37
state = CharacterState . WALK ;
@@ -69,31 +42,25 @@ class Character extends ScratchSprite {
69
42
if (isTouchingEdge() && ! hasTouchedEdge) {
70
43
this . playSound(" bump" );
71
44
hasTouchedEdge = true ;
72
- } else if (! isTouchingEdge() && hasTouchedEdge) {
45
+ } else if (! isTouchingEdge() && hasTouchedEdge) {
73
46
hasTouchedEdge = false ;
74
- }
47
+ }
75
48
76
49
switch (state) {
77
- case IDLE :
78
- if (this . getTimer(" idle" ). everyMillis(100 )) {
79
- idleAnimationFrame = (idleAnimationFrame + 1 ) % idleAnimation. length;
80
- this . switchCostume(idleAnimation[idleAnimationFrame]);
81
- }
82
- break ;
83
- case RUN :
84
- if (this . getTimer(" run" ). everyMillis(50 )) {
85
- runAnimationFrame = (runAnimationFrame + 1 ) % runAnimation. length;
86
- this . switchCostume(runAnimation[runAnimationFrame]);
87
- }
88
- move(4 );
89
- break ;
90
- case WALK :
91
- if (this . getTimer(" walk" ). everyMillis(100 )) {
92
- walkAnimationFrame = (walkAnimationFrame + 1 ) % walkAnimation. length;
93
- this . switchCostume(walkAnimation[walkAnimationFrame]);
94
- }
95
- move(4 );
96
- break ;
50
+ case IDLE :
51
+ this . setAnimationInterval(100 );
52
+ this . playAnimation(" idle" );
53
+ break ;
54
+ case RUN :
55
+ this . setAnimationInterval(50 );
56
+ this . playAnimation(" run" );
57
+ move(4 );
58
+ break ;
59
+ case WALK :
60
+ this . setAnimationInterval(100 );
61
+ this . playAnimation(" walk" );
62
+ move(2 );
63
+ break ;
97
64
}
98
65
}
99
66
}
0 commit comments