How do I Play a Sound Only Once?

0 favourites
  • 7 posts
From the Asset Store
Change the size and position of everything without calculating anything!
  • 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:

    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?

  • 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.

  • 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:

    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.

    It was the same result as A in this post:

  • For the first, also try adding a "For Each" loop.

  • 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:

    Plane > On collision with playSound > Play sound
       System > Trigger once [/code:2ggq5lo9]
    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
  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)