Favicon SparkleJ Framework Home GitHub

Scratch Block → Sparkle API Calls Mapping

Warning - this page has a lot of images. Loading it may heavily affect the Internet traffic.

This page shows Java statements/expressions that use Sparkle API and do something similar to what a certain Scratch block would do. This can be useful for translating your Scratch project into Sparkle! This can't be done automatically with a converter, since there are many design differences between Scratch and Sparkle. For example, Sprites in Sparkle do not execute or even hold any kind of code, they're just renderable data. And multiple Stage instances can be created, did you know that?

This mapping was created for Sparkle version 0.1-beta. May change in the future!

In the following code snippets, replace sprite with your target Sprite instance, stage with your target Stage instance, otherSprite with a different Sprite instance, and "spriteId" with the target Sprite's ID on the target Stage.

Scratch block Sparkle API equivalent Notes

Motion

Move 10 Steps sprite.moveSteps(10);
Turn Right 15 Degrees sprite.rotateRight(15);
Turn Left 15 Degrees sprite.rotateLeft(15);
Go to Random Position double halfWidth = stage.getWidth() / 2d;
double halfHeight = stage.getHeight() / 2d;
sprite.goTo(Constants.RANDOM.nextDouble(-halfWidth, halfWidth), Constants.RANDOM.nextDouble(-halfHeight, halfHeight));
Go to Other Sprite sprite.copyPos(otherSprite);
Go to Mouse Pointer No direct equivalent; working on that 😅
Go to XY sprite.goTo(48, 36);
Glide 1 secs to Random Position double halfWidth = stage.getWidth() / 2d;
double halfHeight = stage.getHeight() / 2d;
sprite.glide(Constants.RANDOM.nextDouble(-halfWidth, halfWidth), Constants.RANDOM.nextDouble(-halfHeight, halfHeight), 1000);
Glide 1 secs to Other Sprite sprite.glideTo(otherSprite, 1000); Second argument - 1000 - represents the count of milliseconds to glide for. 1000 ms = 1 s
Sparkle (and Java) use milliseconds/nanoseconds instead of seconds since the time amount must be a whole number, so, to allow more precision, the time unit itself should be smaller.
Glide 1 secs to Mouse Pointer No direct equivalent yet
Glide 1 secs to XY sprite.goTo(48, 36, 1000);
Point in Direction 90 sprite.setDirection(90);
Point towards Mouse Pointer No direct equivalent yet
Point towards Other Sprite sprite.pointTowards(otherSprite);
Point towards Random Direction sprite.setDirection(Constants.RANDOM.nextDouble(0, 360)); loading="lazy" although this option can't be selected in vanilla Scratch, it can be set using file editing or Scratch mods like TurboWarp, and is completely functional in vanilla Scratch.
Change X by 10 sprite.changeX(10);
Set X to 48 sprite.setX(48);
Change Y by 10 sprite.changeY(10);
Set Y to 36 sprite.setY(36);
If on Edge, Bounce sprite.bounce(); You're right, Sprite.bounce() does not check if the sprite is on edge. I don't really know how to check that with current API, sorry 😅
Set Rotation Style: Left-Right sprite.setRotationMode(RotationMode.LEFT_RIGHT);
Set Rotation Style: Don't Rotate sprite.setRotationMode(RotationMode.NO_ROTATION);
Set Rotation Style: All Around sprite.setRotationMode(RotationMode.ALL_AROUND);
Set Rotation Style: Up-Down sprite.setRotationMode(RotationMode.UP_DOWN); This rotation mode comes from PenguinMod and is not available in Scratch.
Set Rotation Style: Look At sprite.setRotationMode(RotationMode.LOOK_AT); This rotation mode comes from PenguinMod and is not available in Scratch.
X Position sprite.getX() Note the absence of semicolon - this is not a statement, it's an expression!
Y Position sprite.getY()
Direction sprite.getDirection()

Looks

Say Hello! for 2 Seconds sprite.sayFor("Hello!", 2000); The sprite will get a text bubble as a property, however, it will not be rendered, because text rendering is currently unsupported 😅
Also notice that Hello! is in quotation marks - this is how all strings should be enclosed in Java.
Say Hello! sprite.say("Hello!");
Think Hmm... for 2 Seconds sprite.thinkFor("Hmm...", 2000);
Think Hmm... sprite.think("Hmm...");
Switch Costume to costume1 sprite.setCostume("costume1");
Next Costume sprite.incrementCostume();
Switch Backdrop to backdrop1 stage.setBackdrop("backdrop1"); Note that backdrop is a property of a Stage instance, not Sprite
Switch Backdrop to backdrop1 and Wait stage.setBackdrop("backdrop1", true);
Next Backdrop stage.incrementBackdrop();
Change Size by 10 sprite.changeSize(10);
Set Size to 100% sprite.setSize(100);
Set Stretch to XY sprite.setStretchX(100);
sprite.setStretchY(50);
This block comes from Stretch TurboWarp extension and is not available in Scratch.
Change Stretch by XY sprite.changeStretchX(10);
sprite.changeStretchY(5);
This block comes from Stretch TurboWarp extension and is not available in Scratch.
Set Stretch X to 100 sprite.setStretchX(100); This block comes from Stretch TurboWarp extension and is not available in Scratch.
Set Stretch Y to 100 sprite.setStretchY(100); This block comes from Stretch TurboWarp extension and is not available in Scratch.
Change Stretch X by 10 sprite.changeStretchX(10); This block comes from Stretch TurboWarp extension and is not available in Scratch.
Change Stretch Y by 10 sprite.changeStretchY(10); This block comes from Stretch TurboWarp extension and is not available in Scratch.
Change Color Effect by 25 sprite.getGraphicEffects().increment(GraphicEffect.HUE_SHIFT, 45); Unlike Scratch's "color" graphic effect, which is measured in some kinda weird unit (0 - 200), Sparkle's HUE_SHIFT graphic effect is measured in degrees (0 - 360), so we have to convert from Scratch's range to Sparkle's range: 25 * (360 / 200) = 45.
Set Color Effect to 0 sprite.getGraphicEffects().put(GraphicEffect.HUE_SHIFT, 0);
Change Fisheye Effect by 25 sprite.getGraphicEffects().increment(GraphicEffect.BULGE, 25); BULGE effect doesn't work properly yet
Set Fisheye Effect to 0 sprite.getGraphicEffects().put(GraphicEffect.BULGE, 0);
Change Whirl Effect by 25 sprite.getGraphicEffects().increment(GraphicEffect.WHIRL, 25); WHIRL effect doesn't work properly yet
Set Whirl Effect to 0 sprite.getGraphicEffects().put(GraphicEffect.WHIRL, 0);
Change Pixelate Effect by 25 sprite.getGraphicEffects().increment(GraphicEffect.PIXELATION, 25); PIXELATION effect doesn't work properly yet
Set Pixelate Effect to 0 sprite.getGraphicEffects().put(GraphicEffect.PIXELATION, 0);
Change Mosaic Effect by 25 sprite.getGraphicEffects().increment(GraphicEffect.MOSAIC, 25); MOSAIC effect doesn't work properly yet
Set Mosaic Effect to 0 sprite.getGraphicEffects().put(GraphicEffect.MOSAIC, 0);
Change Brightness Effect by 25 sprite.getGraphicEffects().increment(GraphicEffect.BRIGHTNESS, 25);
Set Brightness Effect to 0 sprite.getGraphicEffects().put(GraphicEffect.BRIGHTNESS, 0);
Change Ghost Effect by 25 sprite.getGraphicEffects().increment(GraphicEffect.FADE, 25);
Set Ghost Effect to 0 sprite.getGraphicEffects().put(GraphicEffect.FADE, 0);
Clear Graphic Effects sprite.getGraphicEffects().clear();
Show sprite.setVisible(true);
Hide sprite.setVisible(false);
Go to Front Layer stage.moveToFrontLayer(new SpriteID("spriteId", cloneId)); Sprites do not store their layer; it's a property of Stage.
The clone ID is an int that identifies a sprite's clone. The main sprite's clone ID is always 0.
Go to Back Layer stage.moveToBackLayer(new SpriteID("spriteId", cloneId));
Go Forward 1 Layer stage.moveByLayers(new SpriteID("spriteId", cloneId), 1);
Go Backward 1 Layer stage.moveByLayers(new SpriteID("spriteId", cloneId), -1);
Costume Number sprite.getCostumeIndex()
Costume Name sprite.getCostumeName()
Backdrop Number stage.getBackdropIndex() Once again, note that backdrops belong to a Stage instance.
Backdrop Name stage.getBackdropName()
Size No direct equivalent In Sparkle, sprites' size isn't stored as a single property, size can be separate for X axis (X stretch) and Y axis (Y stretch). Querying size for sprites with different X and Y stretches doesn't really make sense. If you're sure these two properties are always equal, you can query either of them instead.
X Stretch sprite.getStretchX() This block comes from Stretch TurboWarp extension and is not available in Scratch.
Y Stretch sprite.getStretchY() This block comes from Stretch TurboWarp extension and is not available in Scratch.

Sound

Play Sound meow Until Done sprite.startSound("meow", true);
Start Sound meow sprite.startSound("meow");
Stop All Sounds No direct equivalent This feature of Scratch isn't very convenient. It doesn't allow to choose which sounds to stop. You must collect IDs of sounds (return values of startSound(...) methods) to stop instead.
Change Pitch Effect by 10 sprite.getSoundEffects().increment(SoundEffect.PITCH, 10);
Change Pan Left/Right Effect by 10 sprite.getSoundEffects().increment(SoundEffect.PAN, 10);
Set Pitch Effect to 100 sprite.getSoundEffects().put(SoundEffect.PITCH, 100);
Set Pan Left/Right Effect to 100 sprite.getSoundEffects().put(SoundEffect.PAN, 100);
Change Volume by -10 sprite.getSoundEffects().decrement(SoundEffect.VOLUME, 10); VOLUME is a sound effect in Sparkle, not a separate property.
Set Volume to 100% sprite.getSoundEffects().put(SoundEffect.VOLUME, 100);
Clear Sound Effects sprite.getSoundEffects().clear(); This does affect volume since it's a sound effect.
Volume sprite.getSoundEffects().getDouble(SoundEffect.VOLUME)

Events

When Green Flag Clicked Global.when(StartEvent.class, e -> {
 // your code...
});
This is not the only way to register event handlers. See Global and EventDispatcher classes.
When Some Key Pressed Global.when(KeyboardEvent.KeyPressedEvent.class, e -> {
 if (e.key() == Key.SOME_KEY) {
  // your code...
 }
});
When This Sprite Clicked Global.when(MouseEvent.MouseClickedEvent.class, e -> {
 if (GraphicsUtils.getTargetObject(e) instanceof SpriteID(String thatId, int _) && "spriteId".equals(thatId)) {
  // your code...
 }
});
When Stage Clicked Global.when(MouseEvent.MouseClickedEvent.class, e -> {
 if (GraphicsUtils.getTargetObject(e) == null) {
  // your code...
 }
});
When Backdrop Switches to backdrop1 Global.when(BackdropChangeEvent.After.class, e -> {
 if (e.newBackdropId() == stage.getAssets().texturesIndexesByName().getInt("backdrop1")) {
  // your code...
 }
});
When Loudness > 10 No direct equivalent This functionality is planned for an extension module that allows microphone, camera, etc. access. It will not be available in the core module.
When Timer > 10 No direct equivalent Sparkle does not start a timer automatically, unlike Scratch. You may do it with Stopwatch classes.
When I receive message1 Global.when(MessageEvent.class, e -> {
 if (e.messageId().equals("message1")) {
  // your code...
 }
});
Broadcast message1 Global.fire(new MessageEvent("message1"));
Broadcast message1 and Wait Global.fireAndWait(new MessageEvent("message1"));

Control

Wait 1 Second WaitUtils.waitFor(1000); Remember - specify time in milliseconds!
Repeat 10 times for (int i = 0; i < 10; i++) {
 // your code...
}
This is a standard Java feature 😄
Forever while (true) {
 // your code...
}
Actual representation depends on the context. E.g. for starting game loops, TickEvent handler must be used instead.
If Then if (something) {
 // your code...
}
Replace something with your desired boolean expression.
If Then Else if (something) {
 // code if true...
} else {
 // code if false...
}
Wait Until WaitUtils.waitUntil(() -> something); The () -> segment is to create a lambda. Basically, this is required to re-evaluate the specified expression constantly. If you omit that segment, the expression will be evaluated just once, and your program will freeze on this statement if the condition was false at that moment.
Wait 1 Second or Until WaitUtils.waitForOrUntil(() -> something, 1000); This block comes from PenguinMod and is not available in Scratch.
Repeat Until while (!something) {
 // your code...
}
The exclamation mark before the boolean expression negates it (true becomes false and vice versa).
While while (something) {
 // your code...
}
loading="lazy" although this block can't be found in vanilla Scratch, it can be added using file editing or Scratch mods like TurboWarp, and is completely functional in vanilla Scratch.
Stop All Context-dependent; usually:
stage.stop();
return;
To stop the entire program (closes all windows!): System.exit(0);
To stop a Stage instance: stage.stop();
Stop This Script Context-dependent; usually return;
Stop Other Scripts in Sprite No direct equivalent Sprites in Sparkle do not store any code, and code suspension must be managed by the application, which means this block just doesn't make any sense in Sparkle.
When I Start as a Clone No direct equivalent Sprites in Sparkle do not store any code, and no event is fired upon clone creation. Instead, retrieve the clone Sprite instance upon creation to customize it.
Create Clone int cloneId = stage.createClone("spriteId");
Sprite cloneSprite = stage.getSprite("spriteId", cloneId);
The clone sprite's initial layer will be the front one, unlike in Scratch, where it gets the back one.
Delete Clones stage.deleteAllClones("spriteId"); This block comes from PenguinMod and is not available in Scratch.
Delete This Clone stage.deleteClone("spriteId", cloneId);
Is Clone? cloneId != 0 This block comes from PenguinMod and is not available in Scratch.

Sensing

Touching Mouse Pointer? No direct equivalent yet
Touching Edge? Too complex This is possible to query in Sparkle, but it's too much boilerplate for this list. Working on that!
Touching Other Sprite? Too complex
Touching Color? Not possible yet Can't read color data from surface 😭
Color Is Touching Color? Not possible yet
Distance to Mouse Pointer No direct equivalent yet
Distance to Other Sprite Math.hypot(sprite.getX() - otherSprite.getX(), sprite.getY() - otherSprite.getY())
Ask and Wait No direct equivalent This functionality is planned for an extension module that adds UI elements. It will not be available in the core module.
Answer No direct equivalent
Is Key Pressed? No direct equivalent yet
Mouse Down? No direct equivalent yet
Mouse X No direct equivalent yet
Mouse Y No direct equivalent yet
Set Drag Mode: Draggable No direct equivalent Scratch's dragging doesn't allow customization of how the sprite must move towards the cursor, and most applications don't need dragging anyway. This feature must be implemented by the application if needed.
Set Drag Mode: Not Draggable
Loudness No direct equivalent This functionality is planned for an extension module that allows microphone, camera, etc. access. It will not be available in the core module.
Timer No direct equivalent Sparkle does not start a timer automatically, unlike Scratch. You may do it with Stopwatch classes.
Reset Timer
Backdrop Number of Stage stage.getBackdropIndex()
Backdrop Name of Stage stage.getBackdropName()
Volume of Stage 100 Stage doesn't have sound effects (at least for now)
Variable of Stage stage.getVariable("my variable").getValue() You might want to use a method other than getValue() to get the variable's value, if it's a primitive variable
X Position of Sprite sprite.getX()
Y Position of Sprite sprite.getY()
Direction of Sprite sprite.getDirection()
Costume Number of Sprite sprite.getCostumeIndex()
Costume Name of Sprite sprite.getCostumeName()
Size of Sprite No direct equivalent
Volume of Sprite sprite.getSoundEffects().getDouble(SoundEffect.VOLUME)
Variable of Sprite sprite.getVariable("private variable").getValue() You might want to use a method other than getValue() to get the variable's value, if it's a primitive variable
Current Year LocalDate.now().getYear() The provided examples of querying time might not be the best options in your case. See the java.time package.
Current Month LocalDate.now().getMonthValue()
Current Date LocalDate.now().getDayOfMonth()
Current Day of Week Math.floorMod(LocalDate.now().toEpochDay() + 5, 7)
Current Hour LocalTime.now().getHour()
Current Minute LocalTime.now().getMinute()
Current Second LocalTime.now().getSecond()
Days Since 2000 Duration.between(LocalDateTime.of(2000, 1, 1, 0, 0), LocalDateTime.now()).toNanos() / 86400000000000d This block is often used to calculate a certain duration in Scratch. In Java, however, it's recommended to use the Duration class to measure durations without other (unnecessary) calculations. You can still use this code if you want a numeric value from the start, though.
Username No direct equivalent Sparkle is not an account system in any way, it's just a framework for graphical apps. This wouldn't make sense in the core. Applications must implement this functionlity themselves if needed.

Operators

Plus expr1 + expr2 expr1 and expr2 are numeric expressions
Minus expr1 - expr2
Times expr1 * expr2
Over (division) (double) expr1 / expr2 Casting to double ensures mathematical division if both expressions are integer. Remove the cast if you want integer division
Pick Random Constants.RANDOM.nextDouble(expr1, expr2)
More Than expr1 > expr2
Less Than expr1 < expr2
Equals expr1 == expr2 Here, expr1 and expr2 don't have to be numeric, they just must be of compatible types. Use equals() for object expressions if you want to compare their values.
And expr1 && expr2 expr1 and expr2 must be boolean expressions
Or expr1 || expr2
Not !expr expr must be a boolean expression
Join String.valueOf(expr1) + String.valueOf(expr2) Here, expr1 and expr2 can be expressions of any type (except void ofc)
Letter of String.valueOf(expr2).charAt(expr1) expr1 must be an integer expression, expr2 can be an expression of any type
Length of String.valueOf(expr).length() expr can be an expression of any type
Contains? String.valueOf(expr1).contains(String.valueOf(expr2)) expr1 and expr2 can be expressions of any type
Mod expr1 % expr2 expr1 and expr2 must be numerical expressions
Round Math.round(expr) expr must be a numerical expression
Abs Of Math.abs(expr) expr must be a numerical expression
Floor Of Math.floor(expr)
Ceiling Of Math.ceil(expr)
Sqrt Of Math.sqrt(expr)
Sin Of Math.sin(expr)
Cos Of Math.cos(expr)
Tan Of Math.tan(expr)
Asin Of Math.asin(expr)
Acos Of Math.acos(expr)
Atan Of Math.atan(expr)
Natural Logarithm Of Math.log(expr)
Logarithm Of Math.log10(expr)
e to the Power Of Math.exp(expr)
10 to the Power Of Math.pow(10, expr)

Variables

Call registerVariable("variableId", variable) on a Sprite/Stage instance to make a variable/list. In some cases, you'll want to use Java's variables instead (e.g. temporary variables, or properties that do not belong to a Sprite/Stage instance).

See classes in net.fokinatorr.sparkle.variable package for more info.

Get Variable's Value target.getVariable("my variable").getValue() You might want to use a method other than getValue() to get the variable's value, if it's a primitive variable
Set Variable to target.getVariable("my variable").setValue(expr); expr must be the same type as the variable
You might want to use a method other than setValue() to set the variable's value, if it's a primitive variable
Change Variable by Variable<Type> v = target.getVariable("my variable");
v.setDoubleValue(v.getValueAsDouble() + 1);
Only works for numeric variables. Replace Type with the variable's actual type.
Show Variable No direct equivalent This functionality is planned for an extension module that adds UI elements. It will not be available in the core module.
Hide Variable No direct equivalent

Lists

Common first statement: List<Type> list = target.<List<Type>>getVariable("my list").getValue();, where Type is the type of elements in the list
Add to list.add(expr); expr must be of type of elements in the list
Delete 1 of list.remove(0); Unlike in Scratch, indexes start from 0, not 1.
Delete All of list.clear();
Insert at 1 of list.add(0, expr);
Replace item 1 of List with list.set(0, expr);
Item 1 of list.get(0)
Item Number of Expression in list.indexOf(expr)
Length of list.size()
List Contains? list.contains(expr)
Show List No direct equivalent This functionality is planned for an extension module that adds UI elements. It will not be available in the core module.
Hide List No direct equivalent

My Blocks

Java has a similar functionality - methods.

Run without screen refresh GraphicsUtils.runWithoutStageRedraw(stage, () ->
 // your code...
});
This is very useful in certain cases in Scratch, and it's a bit weird that it's only available in custom blocks. That's why it can be done anywhere in Sparkle.