← All templates
Star clicker
BeginnerTemplateWeb & desktopMobile friendly

Star clicker

Game · Casual

Click the star twenty times. Your first scripts: each one is a few lines attached to a single control, with the score shown by a label and a winning button that appears on its own.

by Arcadia Studio
Read the guide ↓

Star clicker

Click the star twenty times to win. The star bounces and pops each time you click it, a counter keeps score, and when you reach twenty a You did it! button appears and takes you to the winning screen.

This is the template where you write your first scripts. Each one is tiny, just a few lines, and belongs to a single control: the star has a script, and so does the Reset button. Everything else is done with actions, like in Menu screens.

The game screen

What you'll learn

  • Keeping the score in a screen variable, and showing it in a label with ${score}.
  • Writing a small script and attaching it to a control's click event.
  • Making a button appear by itself with VisibleIf, once the score is high enough.
  • Making a short animation and starting it from a script.
  • Using a script and actions on the same event.

Glossary

WordWhat it means
Screen variableA named value the screen remembers while it's open, like score. Every screen starts with fresh variables each time it opens.
${score}Written inside a label's text, it's replaced by the value of the variable score, and updates by itself.
ScriptA few lines of JavaScript that run when an event happens.
FunctionA named block of script. An event runs one function, like addPoint.
ctxWhat every function is given. ctx.state reads and writes variables, and ctx.ui changes controls.
VisibleIfA condition on a control, like score >= 20. The control only shows while it's true.
AnimationChanges to a control over time: here the star grows a little and shrinks back.

Rebuild it from scratch

1. Project and screens

  1. File → New project (Ctrl+N), named Star clicker with the Id clicker.
  2. In Project → Project settings…, set Made for to Web & desktop only.
  3. Make three screens, all 320 × 180: title (tick Main screen), game and win. Use **New screen and Screen settings** above the canvas.
  4. Give each screen a dark background Panel that fills it.

If you've done Menu screens, the title and win screens will feel familiar:

TitleWin
TitleWin
  • title has a big label, a hint (Click the star 20 times!) and a Play button. The button's click has two actions: play_sound (a click from the Sound effect maker), then open_ui with the value game.
  • win says You did it!, and has Play again (open_ui game) and Menu (open_ui title). Add a Sound control with a short fanfare from the Music maker, with Autoplay ticked, so it plays when the screen opens.

2. The score variable

  1. Go to the game screen and click Screen settings.
  2. In Variables, type score=0. The game screen now has a variable called score that starts at 0 every time the screen opens.

3. The star and the score label

  1. From the Toolbox, open Shapes and drag the star onto the screen. Call it star, make it 60 × 60, and place it in the middle, near the top (x 130, y 44). Give it a yellow Fill color, a brown Border color with Width 2, and clear its text.
  2. Add a Label called score_label below it. Set its Text to:
   Stars: ${score}

The ${score} part shows the variable's value, so the label counts up by itself.

  1. Add a label at the top that says Click the star 20 times!.

4. Your first script: the star

  1. Select the star and open the Events tab. Pick click.
  2. Under Client, click + Add action, choose play_sound, and pick a pop sound. Make one with Coin in the Sound effect maker.
  3. Now click New Script. A script file and a function are made for you. Put these lines inside the function:
   var score = Number(ctx.state.get('score')) + 1;
   ctx.state.set('score', String(score));
   ctx.ui.animate('pop');
  1. Click Save & Assign.

In the template, the whole script, scripts/client/star.js, looks like this:

// The star's script. Its click event runs addPoint.
function addPoint(ctx) {
  var score = Number(ctx.state.get('score')) + 1;
  ctx.state.set('score', String(score));
  ctx.ui.animate('pop');
}

Line by line:

  • ctx.state.get('score') reads the variable. Variables are stored as text, so Number(…) turns it into a number before adding 1.
  • ctx.state.set('score', …) saves the new value. The label updates straight away.
  • ctx.ui.animate('pop') starts the animation you'll make in step 6.

5. The Reset button

  1. Add a Button called reset in the corner with the text Reset.
  2. On its click event, add a play_sound click. Then use New Script again, with just this one line inside the function:
   ctx.state.set('score', '0');

That's scripts/client/reset_button.js in the template. A second control, with its own little script.

6. The pop animation

  1. Open Advanced → Animations and click + New animation. Call it pop, 160 ms long.
  2. Add four tracks with + Track, all on the star: x, y, width and height.
  3. Give each track three keyframes:
Timexywidthheight
0 ms130446060
70 ms125397070
160 ms130446060

The star grows by 10 and moves up-left by 5 at the same time, so it looks like it grows from its middle.

7. The winning button

  1. Add a Button called you_did_it with the text You did it! →, and make it green.
  2. In Properties, under Behavior, set VisibleIf to:
   score >= 20
  1. On its click, add the actions play_sound (click) and open_ui with the value win.

The button is hidden until the score reaches 20, and then it appears by itself. No script needed.

At 20 stars the button appears

8. Play it

Press Preview (F5), click the star twenty times, and win. Then press Play again: the score is back to 0, because the game screen starts fresh every time it opens.

How it works

  • The score lives on the screen. score is a screen variable. The star's script changes it, the label shows it, and the winning button watches it. None of them needs to know about the others.
  • Scripts and actions on the same event. The star's click plays a sound (an action) and runs addPoint (a script). Actions are the quick way to do simple things. Scripts are for anything that needs a little thinking, like adding 1.
  • Screens start fresh. Leaving a screen and coming back resets its variables. That's why Play again starts from 0 without any reset code.

Make it yours

  • Change the goal: edit the hint label, the VisibleIf (score >= 50) and the win screen's text.
  • Make each click worth more: change + 1 to + 5.
  • Add a second star with its own script that takes a point away.
  • Draw your own star in the Pixel editor and give it to the star shape as its Skin image (Properties → Appearance). It keeps its click event. An Image control has no click, so the shape is what you want here.