Getting a sound clip to play once even if the event is triggered multiple times?

I have two issues: how do I get sound clip to play only once, even if the cursor is still triggering it and how do you get a frame to reset clip locations on load.

Building on previous advice, I was able to flesh out a game scenario where the selected sprite character is able to move around the stage via keyboard arrows. It can advance to the next level via “hitTest” and likewise gather a fail state from hitting the enemy dog clip.

I wanted to make it so there was fun interaction for gathering (touching) the dirt-cups on the way to the exit. I tried to add a sound clip of crunching . You can see what I mean by moving the butterfly to the right side dirt cup.

Likewise, if you fail with any bug, your position is retained on reload to the starting frame (unless you refresh your browser.)

1 Like

Here’s a file containing a possible solution for each issue:
My Project11-12-2020_2-19-05PM.wick (8.1 KB)

First, the clip reset issue - you can set the position you’d like a clip to start at by establishing its x and y values in its default script. In the above example, the blue circle clip has this code in its default script:

this.x=170;
this.y=200;

This way, whenever the frame is loaded, this code is executed, and so the blue circle will always reset to this position. On a side note, you can copy-paste the Origin X and Origin Y values of your clip from the inspector into the code:
img29

Now for the hitTest issue - what I personally do is set a boolean to the clip in its default script. In the example file, the green circle has “this.hit=false;” in its default script. Then in the update script, the hitTest code also checks if the boolean is false, then sets it to true after the other code is executed, like this:

if (this.hitTest(player)&&this.hit===false) {
//do stuff here
this.hit=true;
}

This way, after the hitTest has fired once, the code before this.hit=true will fire only once. You can see this in the example project - the blue circle can hit the green circle, and when it does, the green circle shrinks to half its original size, and a sound plays. If it wasn’t for the boolean, the code would continuously fire and the green circle’s size would change multiple times (and the sound would play multiple times as well). But with the boolean, the code only runs once.

1 Like

Incredibly helpful, as always!

1 Like