Sprites and costumes
A sprite is a figure on the stage. In Scratch every sprite has its own scripts. In Java every kind of sprite gets its own class.
A sprite class
This class describes a bunny. The constructor runs once, when a bunny is made.
run() runs again and again, about 60 times a second.
new Bunny() makes a bunny, and this.add(...) puts it on the stage. Two
new give two bunnies. Each one has its own position.
Try it: add a third bunny at x: 0, y: 100.
Positions
The middle of the stage is x: 0, y: 0. A stage of 480 × 360 goes from x: -240 on the left to 240 on the right, and from y: -180 at the bottom to 180 at the top. As in Scratch, y grows upwards.
| Scratch | Java |
|---|---|
| go to x: (0) y: (0) | setPosition(x, y) |
| change x by (10) | changeX(10) |
| set y to (0) | setY(0) |
| (x position) | getX() |
Costumes
A sprite can have many costumes. Give each one a name, then switch by name:
Click the stage, then hold the space bar.
-
addCostume("alienGreen_stand")uses a built-in picture and its name. -
addCostume("stand", "alienGreen_stand")gives the costume a name of your own. nextCostume()switches to the next one, as in Scratch.
Your own pictures
Give a file path instead of a built-in name. Anything with a file ending, like
.png, is a file:
this.addCostume("hero", "assets/hero.png");
In Studio, put the file into the project's assets folder. Draw your own
costumes facing right: a sprite with direction 90 looks right, as in
Scratch, so move takes it the way it looks.
Free pictures: Kenney, OpenGameArt. Free tools to draw them: Aseprite for pixel art, Inkscape and GIMP.
Animations
An animation is a list of costumes that change by themselves. Built-in
pictures that belong together are numbered, like alienGreen_walk1 and
alienGreen_walk2. %d stands for the number:
-
addAnimation("walk", "alienGreen_walk%d", 2)addsalienGreen_walk1andalienGreen_walk2. setAnimationInterval(0.15)shows each picture for 0.15 seconds.playAnimation("walk")inrun()keeps the animation going.
Size and direction
| Scratch | Java |
|---|---|
| set size to (50) % | setSize(50) |
| point in direction (90) | setDirection(90) |
| turn cw (15) degrees | turnRight(15) |
| point towards [mouse-pointer v] | pointTowardsMousePointer() |
| set rotation style [left-right v] | setRotationStyle(RotationStyle.LEFT_RIGHT) |
Move the mouse over the stage:
RotationStyle.LEFT_RIGHT only flips the sprite left or right, so a walking
figure never stands on its head. RotationStyle.DONT never turns it.
In front and behind
Sprites added later are drawn in front. To change that:
goToFrontLayer()andgoToBackLayer(), as in Scratch,goLayersForwards(1)andgoLayersBackwards(1).
A stage also has four big layers: Layer.BACKGROUND, Layer.MAIN (where
sprites are), Layer.FOREGROUND and Layer.UI. setLayer(Layer.UI) keeps a
sprite above everything, for a score or a heart display.
In a game seen from above, a sprite further down should stand in front of one
further up. Call this.sortByY() once in the stage's constructor, and the stage
sorts its sprites by their y position in every frame.
Hiding
hide() and show() work as in Scratch. A hidden sprite still runs its
run(), but it does not touch anything.
To take a sprite off the stage for good, use remove().
Many sprites of the same kind
You do not need clones for that. Make objects with new in a loop:
for (int i = 0; i < 5; i++) {
Coin coin = new Coin();
coin.setPosition(-200 + i * 100, 0);
this.add(coin);
}
Clones work too, see Clones.
Finding sprites on the stage
The stage knows every sprite on it. Ask it with a class:
| Method | Gives you |
|---|---|
find(Fish.class) |
a list of all fish, to go through with a for loop |
count(Fish.class) |
how many fish there are |
getAll() |
a list of all sprites |
remove(Fish.class) |
removes all fish |
removeAll() |
removes everything from the stage |
A sprite asks its stage the same way: this.getStage().count(Fish.class).
Sprites that know each other
Often one sprite needs another one: a bee chases the bunny, a carrot tells the stage that it was eaten. There are three ways.
Pass it in the constructor. The bee gets the bunny when it is made and keeps it in a field.
Ask the stage. getStage() gives you the stage the sprite is on. To call a
method of your own stage class, tell Java which class it is:
(Garden) this.getStage(). This is called a cast.
Send a message. That is best when many sprites should react and the sender does not care who.
The first two in one program. Move the bunny with the mouse:
getStage() is null in the constructor. A sprite is on no stage until
add puts it there. Use getStage() in run() and in the when… methods.
Next
- All built-in pictures, click one to copy its line
- Sprite in the reference, every method with an example
- Touching: when sprites meet