Is it possible to load sounds from a sound matrix into AS3?

2

I have sounds in the library and I would like to know if it would be possible to name them as 1,2,3 ... and be able to load them as if they were the index of a matrix .

They are many and depending on the events I have to load one or the other. If you could name them with the numbers for export and then at the time of loading them refer to the index [i] I would avoid writing the same code 100 times. Depending on the value of "i" one sound or another would be charged. Is it possible to do this?

Thanks in advance

Greetings

    
asked by Carmen Torralvo 05.04.2016 в 12:03
source

1 answer

1

NOTE: The practical solution is with functions and objects but the technique will be the same or very similar in any of the usual object-oriented languages.

  

If they could be named with the numbers for export

As to rename them, if it is to load them you do not need it, if you have the files in the same directory, you can load all the files dynamically:

File folder = new File("ruta/al/directorio/de/tus/sonidos");
for (File f : folder.listFiles()) {
     // cargar en el array/list de sonidos
}
  

and then when loading them, referring to the index [i] would avoid writing the same code 100 times. Depending on the value of "i" one sound or another would be charged. Is it possible to do this?

Yes, in object-oriented languages it is possible to create an array or a collection of audio / sound type objects compatible with the files you have.

In you can use the class Clip , depending on what you want File will also help you.

Either fine creating an array [] :

Clip[] sonidos = new Clip[numeroDeSonidos];

Or a ArrayList (so you will not need to know the size beforehand as they are dynamic in size):

ArrayList<Clip> sonidos = new ArrayList<>();

In both cases, you can iterate through the Clips you can use a for-each:

for (Clip c : sonidos) {
    c.loop(1); // hacer que suene 1 vez... OJO A QUE SE SOLAPARAN!
}

Or take an element for the position:

sonidos[posicion];      // array
sonidos.get(posicion)¡; // ArrayList
    
answered by 05.04.2016 в 12:10