Skip to content

Commit 9527174

Browse files
author
Mike Barkmin
committed
Simplify the usage of sprites by remove the usage of super
1 parent f73ffb9 commit 9527174

File tree

14 files changed

+122
-47
lines changed

14 files changed

+122
-47
lines changed

README.md

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ initialize the ScratchStage. You only need to place this statement
2323
```java
2424
import eu.barkmin.processing.scratch.*;
2525

26+
ScratchStage stage;
27+
2628
void setup() {
2729
ScratchStage.init(this);
30+
stage = ScratchStage.getInstance();
2831
}
2932

3033
void draw() {
@@ -64,8 +67,16 @@ To add a new backdrop call `stage.addBackdrop("newBackdrop",
6467

6568
| Scratch | Processing |
6669
| :-: | :-: |
67-
| ![stage when key pressed](web/assets/sprite_when_keypressed.png) | Overwrite `stage.keyEvent(KeyEvent e)`. The method will be called everytime a new KeyEvent is fired. For example when pressing or releasing a key. See [KeyEvent](https://processing.github.io/processing-javadocs/core/processing/event/KeyEvent.html) for more Information. |
68-
| ![stage when move moved](web/assets/sprite_when_mouse_moved.png) | Overwrite `stage.mouseEvent(MouseEvent e)`. The method will be called everytime a new MouseEvent is fired. For example when pressing, releasing or moving the mouse. See [MouseEvent](https://processing.github.io/processing-javadocs/core/processing/event/MouseEvent.html) for more Information. |
70+
| ![stage when key pressed](web/assets/sprite_when_keypressed.png) | Overwrite `stage.whenKeyPressed(int keycode)`. This method is called everytime a key is pressed. See [http://keycode.info](http://keycode.info) for keycode information |
71+
| ![stage when move moved](web/assets/sprite_when_mouse_moved.png) | Overwrite `stage.whenMouseMoved(float x, float y)`. This method is called everytime the mouse is moved. |
72+
73+
74+
### Other methods
75+
76+
| Processing | Description |
77+
| :-: | :-: |
78+
| `stage.addSprite(sprite)` | Adds a sprite to stage |
79+
| `stage.removeSprite(sprite)` | Removes a sprite from the stage |
6980

7081
#### Sound
7182

@@ -95,33 +106,34 @@ Source Code: https://github.com/mikebarkmin/processing-library-scratch/blob/mast
95106
In Scratch sprites are the main actors. Every sprite has a custom set of
96107
costumes and sounds, which could be dynamically changed. Most of the
97108
functionality which sprites in Scratch have were tried to reimplement in
98-
processing.
109+
processing. When added to the stage, the run method of a sprite will be called continuously.
99110

100111
#### Creation
101112

102113
```java
103114
import eu.barkmin.processing.scratch.*;
104115

116+
ScratchStage stage;
105117
CatSprite myCat;
106118

107119
void setup() {
108120
size(800, 600);
109121
ScratchStage.init(this);
122+
stage = ScratchStage.getInstance();
110123
myCat = new CatSprite();
124+
stage.addSprite(myCat);
111125
}
112126

113127
void draw() {
114-
myCat.draw();
115128
}
116129

117130
// Define a class Cat
118131
class CatSprite extends ScratchSprite {
119132
CatSprite() {
120-
super("cat", "sprites/cat.png");
133+
this.addCostume("cat", "sprites/cat.png");
121134
this.setOnEdgeBounce(true);
122135
}
123-
void draw() {
124-
super.draw();
136+
void run() {
125137
this.move(2);
126138
}
127139
}
@@ -186,6 +198,41 @@ To add a new costume call `sprite.addCostume("newCostume",
186198
| ![sprite when key pressed](web/assets/sprite_when_keypressed.png) | Overwrite `sprite.keyEvent(KeyEvent e)`. The method will be called everytime a new KeyEvent is fired. For example when pressing or releasing a key. See [KeyEvent](https://processing.github.io/processing-javadocs/core/processing/event/KeyEvent.html) for more Information. |
187199
| ![sprite when move moved](web/assets/sprite_when_mouse_moved.png) | Overwrite `sprite.mouseEvent(MouseEvent e)`. The method will be called everytime a new MouseEvent is fired. For example when pressing, releasing or moving the mouse. See [MouseEvent](https://processing.github.io/processing-javadocs/core/processing/event/MouseEvent.html) for more Information. |
188200

201+
### Other methods
202+
203+
| Processing | Description |
204+
| :-: | :-: |
205+
| `sprite.run()` | Will be called continuously, when the sprite is added to the stage. |
206+
| `sprite.draw()` | Overwrite this methode to gain more controll over the sprite and draw it without added it to the stage. |
207+
208+
209+
The following code will show the same result. The normal sprite is handled by the ScratchStage, the custom sprite is handled by us.
210+
```
211+
Custom custom;
212+
213+
void setup () {
214+
ScratchStage.init(this);
215+
ScratchStage.getInstance().addSprite(new Normal());
216+
}
217+
218+
void draw() {
219+
custom.draw();
220+
}
221+
222+
class Normal extends ScratchSprite {
223+
run() {
224+
this.move(10);
225+
}
226+
}
227+
228+
class Custom extends ScratchSprite {
229+
draw() {
230+
super.draw();
231+
this.move(10);
232+
}
233+
}
234+
```
235+
189236
#### Sound
190237

191238
When you work with sound you need to install

examples/Cat/Cat.pde

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import eu.barkmin.processing.scratch.*;
22

3+
ScratchStage stage;
34
CatSprite myCat;
45

56
void setup() {
67
size(800, 600);
78
ScratchStage.init(this);
9+
stage = ScratchStage.getInstance();
810
myCat = new CatSprite();
11+
stage.addSprite(myCat);
912
}
1013

11-
void draw() {
12-
myCat.draw();
13-
}
14+
void draw() {}
1415

1516
// Define a class Cat
1617
class CatSprite extends ScratchSprite {
1718
CatSprite() {
18-
super("cat", "sprites/cat.png");
19+
this.addCostume("cat", "sprites/cat.png");
1920
this.setOnEdgeBounce(true);
2021
}
21-
void draw() {
22-
super.draw();
22+
void run() {
2323
this.move(2);
2424
}
2525
}

examples/Clock/Clock.pde

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ void setup() {
88
ScratchStage.init(this);
99
stage = ScratchStage.getInstance();
1010
clock = new ClockSprite();
11+
stage.addSprite(clock);
1112
}
1213

1314
void draw() {
14-
//stage.eraseAll();
15-
clock.draw();
1615
}

examples/Clock/ClockSprite.pde

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ class ClockSprite extends ScratchSprite {
55
HourHandSprite hour;
66

77
ClockSprite() {
8-
super("clock", "sprites/clock.png");
8+
this.addCostume("clock", "sprites/clock.png");
99
second = new SecondHandSprite();
1010
minute = new MinuteHandSprite();
1111
hour = new HourHandSprite();
1212
}
1313

14-
void draw() {
14+
void run() {
1515
second.draw();
1616
minute.draw();
1717
hour.draw();
18-
super.draw();
1918
}
2019
}

examples/Clock/HourHandSprite.pde

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class HourHandSprite extends ScratchSprite {
22
HourHandSprite() {
3-
super("hand", "sprites/hour.png");
3+
this.addCostume("hand", "sprites/hour.png");
44
}
55

6-
void draw() {
7-
super.draw();
6+
void run() {
87
int hour = this.getCurrentHour();
98
this.setRotation(hour / 12.0 * 360);
109
}

examples/Clock/MinuteHandSprite.pde

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class MinuteHandSprite extends ScratchSprite {
22
MinuteHandSprite() {
3-
super("hand", "sprites/minute.png");
3+
this.addCostume("hand", "sprites/minute.png");
44
}
55

6-
void draw() {
7-
super.draw();
6+
void run() {
87
int minute = this.getCurrentMinute();
98
this.setRotation(minute / 60.0 * 360);
109
}

examples/Clock/SecondHandSprite.pde

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class SecondHandSprite extends ScratchSprite {
22

33
SecondHandSprite() {
4-
super("hand", "sprites/second.png");
4+
this.addCostume("hand", "sprites/second.png");
55
}
66

7-
void draw() {
8-
super.draw();
7+
void run() {
98
int second = this.getCurrentSecond();
109
if (isKeyPressed(32)) {
1110
int millisecond = this.getCurrentMillisecond();

examples/RainbowVine/RainbowVine.pde

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,24 @@ void setup() {
1111
stage.setColor(0, 0, 0);
1212

1313
vine = new VineSprite();
14+
stage.addSprite(vine);
1415
}
1516

16-
void draw() {
17-
vine.draw();
18-
}
17+
void draw() {}
1918

2019
class VineSprite extends ScratchSprite {
2120

2221
ArrayList<LeafSprite> leafs = new ArrayList();
2322

2423
VineSprite() {
25-
super("vine", "sprites/vine.png");
24+
this.addCostume("vine", "sprites/vine.png");
2625
this.getPen().down();
2726
this.getPen().setSize(3);
2827
this.getPen().setColor(120);
2928
this.hide();
3029
}
3130

32-
void draw() {
33-
super.draw();
31+
void run() {
3432
this.setPosition(ScratchStage.parent.mouseX, ScratchStage.parent.mouseY);
3533
this.turnRight(5);
3634

examples/RandomDot/RandomDot.pde

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import eu.barkmin.processing.scratch.*;
22

3+
ScratchStage stage;
34
RandomDotSprite dot;
45

56
void setup() {
67
size(800, 600);
78
ScratchStage.init(this);
9+
stage = ScratchStage.getInstance();
810
dot = new RandomDotSprite();
9-
11+
stage.addSprite(dot);
1012
}
1113

12-
void draw() {
13-
dot.draw();
14-
}
14+
void draw() {}
1515

1616
class RandomDotSprite extends ScratchSprite {
17-
void draw() {
18-
super.draw();
17+
void run() {
1918
if(this.getTimer().everyMillis(100)) {
2019
this.getPen().down();
2120
this.getPen().setSize(10);

examples/Robot/Robot.pde

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import eu.barkmin.processing.scratch.*;
22

3+
ScratchStage stage;
34
RobotSprite robot;
45

56
void setup() {
67
size(800, 600);
78
ScratchStage.init(this);
89
robot = new RobotSprite();
10+
stage = ScratchStage.getInstance();
11+
stage.addSprite(robot);
912
}
1013

11-
void draw() {
12-
robot.draw();
13-
}
14+
void draw() {}

0 commit comments

Comments
 (0)