How do I Play a Sound Only Once?

Get help using Construct 2

Post » Thu Mar 10, 2016 5:56 pm

Hi guys,

I have a plane that needs to play a sound after it has travelled 320 px. I have done this in two ways and both times it doesn't give the required result and I don't know what I'm doing wrong:

Image

When I do A it plays the sound but it seems to play continuously, not looping, but multiple instances of it playing, I guess as the plane progresses every pixel the sound plays. Logical but not what was wanted.

When I do B the sound doesn't play, period. Whereas with code A it sounds like a Russian army choir trying to shout over each other, using code B it's the sound of silence.

So, what am I doing wrong?
Last edited by BigFrank on Thu Mar 10, 2016 10:59 pm, edited 1 time in total.

"To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment."

Ralph Waldo Emerson
B
10
S
2
G
1
Posts: 56
Reputation: 1,300

Post » Thu Mar 10, 2016 6:28 pm

For A: add Trigger Once.

B probably never occurs i.e. a moving object is probably never going to exactly equal a value due to the nature of computers and numbers.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
136
S
31
G
86
Posts: 5,486
Reputation: 59,758

Post » Thu Mar 10, 2016 11:04 pm

Many thanks for your reply.

I forgot to mention that the plane spawns randomly so at any one time there could be two planes on the layout, and another one is definitely on its way. So, I did what you said in the two ways shown below:

Image

In A it plays correctly, but just for the first plane. The following planes don't play the sound.

In B it plays the sound for the first plane only but continually, as in A in my first post.

Getting there, but still doing it wrong.

Edit: I also tried this, but that also didn't do the trick.

Image

It was the same result as A in this post:

"To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment."

Ralph Waldo Emerson
B
10
S
2
G
1
Posts: 56
Reputation: 1,300

Post » Fri Mar 11, 2016 1:52 am

For the first, also try adding a "For Each" loop.
Zelda Gamekit (my project)



Example capxs:

Zelda-Style Inventory

Zelda Health

3-saveslot system

[url=https://www.dropbox.com/s/d4396pwak0oh7q6/ZeldaDialogueSystem.capx?dl=0]Zelda-like Dialogue[
B
51
S
24
G
30
Posts: 534
Reputation: 19,620

Post » Fri Mar 11, 2016 9:12 am

Thank you for the reply. I did what you said. I read the For Each tutorial and then added a "For each plane" condition to my event. But the result wasn't what I wanted. As in A above it plays correctly, but only for the first plane. Then as in B above it plays the sound for the following planes, but continually, every tick. "Trigger once" only seems to work for the first plane.

I spent a long time on this now, too long really, and one day I'll get it, but not today. I in fact solved it by looking at it from another angle. I put an invisible sprite, a vertical wavy line, called "playSound" on the layout at about 320 px:
Code: Select all
Plane > On collision with playSound > Play sound
   System > Trigger once

Perfect. Done job. :)

What's more, this actually works better because I have a parallax background three times the width of my layout. So I set my sprite to move with my player, who is always roughly in the centre of the screen (640 px). In this way the sound doesn't just play at a fixed 320 px, it only plays when the Plane is visible at 320px from the left edge. This is exactly what I wanted :)

"To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment."

Ralph Waldo Emerson
B
10
S
2
G
1
Posts: 56
Reputation: 1,300

Post » Fri Mar 11, 2016 12:55 pm

Image
Each instance has a boolean instance variable that helps for the picking.
Once "Played" is set to true, the plane won't be picked again and will only have played the sound once.

Setting the opacity here is to have a visual representation of which "plane" is playing the sound.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
293
S
117
G
96
Posts: 7,310
Reputation: 71,069

Post » Fri Mar 11, 2016 1:42 pm

I can completely follow the logic of that and, to be sure, it's a far more elegant solution than the one I came up with! There's so much I have yet to learn.

Thank you very much for your help :)

"To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment."

Ralph Waldo Emerson
B
10
S
2
G
1
Posts: 56
Reputation: 1,300


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 53 guests