How to play random sound ?

Get help using Construct 2

Post » Mon May 05, 2014 12:09 pm

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 ?
B
12
S
3
Posts: 288
Reputation: 1,694

Post » Mon May 05, 2014 12:41 pm

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...
B
12
S
3
Posts: 288
Reputation: 1,694

Post » Mon May 05, 2014 2:17 pm

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.
What I know would fill a Book , what I don't know would fill a Library
B
37
S
12
G
29
Posts: 331
Reputation: 16,499

Post » Mon May 05, 2014 3:07 pm

Hi friend, try this.
regards
You do not have the required permissions to view the files attached to this post.
B
42
S
11
G
2
Posts: 111
Reputation: 3,823

Post » Tue May 06, 2014 12:07 pm

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

choose("sound1", "sound2", "sound3")
B
60
S
10
G
10
Posts: 1,013
Reputation: 9,155

Post » Tue May 06, 2014 12:08 pm

You don't need Global Variable to play random sounds
B
60
S
10
G
10
Posts: 1,013
Reputation: 9,155


Return to How do I....?

Who is online

Users browsing this forum: David181 and 21 guests