A little sound makes a game feel alive. In GameSpark you draw sounds in the Sound tab and play them from code with one function: sfx().
1. Make a sound
Open the Sound tab. You are editing sound slot 0. Click along the note grid to sketch a short blip — a rising run of notes makes a cheerful "pickup", a single low note makes a "thud". Press play in the editor to preview it until you like it.
2. Play it from code
sfx(n) plays the sound in slot n (there are 64 slots, numbered 0–63). Play slot 0 the moment a key is pressed. Use keyp, not key, so it fires once per press instead of sixty times a second:
function update() {
if (keyp(' ')) {
sfx(0); // space plays sound 0, once per tap
}
}
function draw() {
cls(1);
print('press space', 36, 60, 7);
}Do not call sfx every frame. Putting sfx(0) in draw() (or behind key(' ')) retriggers it sixty times a second — an ugly buzz. Trigger sounds on events: a key press, a bounce, a pickup.
3. Sounds for game events
The real use is reacting to things that happen. Make a second sound in slot 1, then play it whenever the ball from Motion and bouncing hits a wall:
if (x < r || x > 127 - r) {
dx = -dx;
sfx(1); // "bounce" sound on every wall hit
}Because a bounce is a one-time event (it happens on a single frame), calling sfx(1) right there plays the sound exactly once per bounce — no keyp needed.
Try it: give different events different slots — a soft sfx(0) for pickups, a harsher sfx(2) for damage. Play a random one with sfx(flr(rnd(3))) for variety.
What you learned
- Sounds are drawn in the Sound tab; each lives in a numbered slot (0–63).
sfx(n)plays the sound in slotn.- Trigger sounds on events (a key press, a bounce), never every frame.
- Use
keypfor key-triggered sounds so they fire once per press.