How to play random sound ?

0 favourites
  • 6 posts
From the Asset Store
In this template the music plays without looping in your game
  • If I have say three Sounds that I have imported.

    I want one of three sounds to play when I press a Button, how do I do that ?

    ============================================================

    In Flash I can do this:

    var RandomNumber_Generated:int;

    RandomNumber_Generated = Math.floor(Math.random()*3); //Returns 0 to 2

    trace (RandomNumber_Generated);

    ============================================================

    How do I do this in this program ?

  • Ok, I add a Global Variable [Bad programming Practice for a simple function], and then do a system set value

    floor(random(8))

    So....that global variable now holds a values....how do I get it to play a sound based on that value...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Local variables

    Local variables are variables placed nested under other events, or inside a group. They also show with a different icon to global variables.

    A local variable.

    The main difference between global and local variables is local variables can only be accessed in their scope. A local variable's scope is its level of sub-events. All other events at the same level of indentation, or lower levels, can access the local variable. Events above it (less indented) cannot access the local variable.

    For example, if an event variable is in a group of events, it becomes a local variable. Then, it will only appear as an option for a variable in events inside that group. In other groups or in other event sheets it does not appear at all and cannot be accessed. This makes the variable local to the scope in which it is placed.

    Local variables convenient for temporarily holding variables over a short range of events, such as to calculate an average value (where a temporary 'sum' variable may be necessary). It also helps keep the project simple, since it prevents the need to create more global variables, which appear everywhere in the project even if they are not needed everywhere.

    The scope of local variables is designed to mimic how the scope of variables works in real programming languages.

  • Hi friend, try this.

    regards

  • Use the 'Play (by name)' action in the Audio object and use an expression like this:

    choose("sound1", "sound2", "sound3")

  • You don't need Global Variable to play random sounds

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