@@ -2,16 +2,7 @@ enum CharacterState {
22 IDLE , RUN , WALK
33}
44
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 {
156
167 CharacterState state;
178
@@ -27,26 +18,9 @@ class Character extends ScratchSprite {
2718 this . addSound(" bump" , " assets/bump.wav" );
2819 this . addSound(" run" , " assets/run.wav" );
2920
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);
5024
5125 this . setOnEdgeBounce(true );
5226 this . setSize(30 );
@@ -56,9 +30,8 @@ class Character extends ScratchSprite {
5630 this . setRotation(random (0 , 360 ));
5731 }
5832
59- void draw () {
33+ void run () {
6034 this . setTint(this . tintColor);
61- super . draw();
6235
6336 if (isTouchingMousePointer()) {
6437 state = CharacterState . WALK ;
@@ -69,31 +42,25 @@ class Character extends ScratchSprite {
6942 if (isTouchingEdge() && ! hasTouchedEdge) {
7043 this . playSound(" bump" );
7144 hasTouchedEdge = true ;
72- } else if (! isTouchingEdge() && hasTouchedEdge) {
45+ } else if (! isTouchingEdge() && hasTouchedEdge) {
7346 hasTouchedEdge = false ;
74- }
47+ }
7548
7649 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 ;
9764 }
9865 }
9966}
0 commit comments