Ten useful formulas

Copy these into your own games, one number box at a time.

Before you start

Each formula below goes in a number box (or a text box) in a settings dialog. Object names must match yours exactly, in double quotes. The ƒx helper (Advanced and Expert) can insert them for you. If the box turns red, read the sentence under it.

1. A score that shows on screen

"Score: " + Str(Score) in Set the text, in an Always rule. The Score object does this by itself, but this way you choose the words.

WhenAlways
DoSet text of Message to "Score: " + Str(Score)

2. A dice

Random(6) + 1 gives 1 to 6. Random(6) alone gives 0 to 5.

3. A coin anywhere across the level

Random(FrameWidth) as X (across) in Create an object, with 0 as Y (down). Works whatever the level’s width is.

WhenEvery 1500 ms
DoCreate Coin at (Random(FrameWidth), 0)

4. Follow the mouse

Set position with X (across) MouseX and Y (down) MouseY, in an Always rule. The Follows the mouse movement does the same without a rule.

5. Faster every time

Speed("Ball") + 5 in Set speed. Put it on the paddle collision in Chocobreak and the ball speeds up with every hit. Cap it with Min(Speed("Ball") + 5, 80).

WhenBall collides with Paddle
DoBounce BallSet speed of Ball to Min(Speed("Ball") + 5, 80)

6. Points that depend on the clock

Max(100 - Timer / 1000, 10) in Add to score. Timer is in thousandths of a second, so this is 100 minus the seconds so far, never less than 10. Finish fast, score more.

7. Line up with another object

X("Hero") as X (across) in Set position keeps a hat, a shadow or a health bar above the hero. Use Y("Hero") - 40 for the down value.

WhenAlways
DoSet position of Hat to (X("Hero"), Y("Hero") - 40)

8. A countdown in words

"Time: " + Str(Floor(60 - Timer / 1000)) counts down from 60 in whole seconds. The Timer display object does this without a formula, with Time runs out when it hits zero.

9. Keys collected across the game

"Keys: " + Str(Global("A")) shows global value A, which keeps its number from level to level. Add 1 to it when a key is picked up. See Global values A to Z.

10. How far the ghost is

Distance(X("Hero"), Y("Hero"), X("Ghost"), Y("Ghost")) < 100 in Compare two values: put the Distance part in First value, choose < less than, and 100 in Second value. The ghost wakes up when you get close.

WhenDistance(X("Hero"), Y("Hero"), X("Ghost"), Y("Ghost")) is < 100
DoStart Ghost movingPlay sound Spooky tune

Two more for Expert mode

Value("Slime", "A")The picked slime’s own value A, for example its hit points. "HP: " + Str(Value("Hero", "A")) shows the hero’s.
Flag("Door", 0)1 if the door’s switch 0 is on, otherwise 0. Flag("Door", 0) * 10 is 10 points for an open door.

Still stuck?

Try the Chocobreak tutorial, open a sample game and read its rules, or look at the questions people ask.