Browse  Forum  Clubs  Chat  News 

ActionScripters Club -> ActionScripters -> Problems with Arrays of Sounds - Is there anyone able to help me?

ActionScripters Forum: Problems with Arrays of Sounds - Is there anyone able to help me?

NJB: Problems with Arrays of Sounds - Is there anyone able to help me? - Sep 14th 2007, 6:43PM Link | Report
Ok, to give you a short intro, I have a Flash Library with various notes and beats, and I'm trying to make a class that takes a string input and outputs it essentially as music, (think of a Midi/Impulse Tracker like thing for Flash, hopefully cutting down the size on large games due to the music being rendered on-the-fly.

Anyhow, thanks to Florian Segginger the String to Array Code works perfectly, on my part, the Timer works fine too but sound objects fail to work It'll play the first Note of the track, but none of the others.

Any and all help is much appreciated and will be credited.

The Tracker is instantiated with:

var Track1:Tracker = new Tracker();
var Song:String = "2BABCBA";
Track1.PlaySong(Song,true,120);


The class code is:

class Tracker {

function Read(song:String) {
//Coded by Florian Segginger over MSN for NJ Boyd
//define an array with all tracks

//var song:String = TracksIn;
//get the number of tracks (1st character, index is like arrays, starts at 0)
var numTracks:Number = parseInt(song.charAt(0));

var Tracks:Array = new Array(numTracks);
var numNotes:Number = (song.length-1)/numTracks;
//trace(numNotes);

//Create all necessary melodies in the track array
for (var n:Number = 0; n<numTracks; n++) {
Tracks[n] = new Array();

}
//iterate through the string, we start at 1 since 0 is the number of tracks
for (var i:Number = 1; i<=song.length-2; i += numTracks) {
//go through each character in that bit of string
for (var j:Number = 0; j<numTracks; j++) {
//ord gets the ASCII value of the character, we substract 65 because A is ascii value 65, thus you can have notes from 0 to64, that's more than enough
//var Note:Number = ord(song.charAt(i+j));
var Note:Number = song.charCodeAt(i+j)-65;
Tracks[j].push(Note);
}

}
//trace(Tracks.length);
return Tracks;
}

function PlaySong(Song:String, loopoolean, BPM:Number) {

//Coded by NJ Boyd
//Gets Array from String input
var PlayTracks:Array = Read(Song);
//Defines an array of sound objects according to the number of tracks
var SoundTracks:Array = new Array(PlayTracks.length);

for (var st:Number = 0; st<layTracks.length; st++) {

SoundTracks[st] = new Sound();

}
//Adds and plays the sounds by updating the Sound objects with sound IDs from the note array
var p:Number = 0;
var b:Number = 0;
var id = setInterval(function () {
trace(PlayTracks.length);
trace(p);
while (p<layTracks.length) {
SoundTracks[p].attachSound(PlayTracks[p]
.toString());
SoundTracks[p].start();
p++;
trace("Track: "+p);
trace("Note No."+PlayTracks[p].toString());
trace(PlayTracks);
}
b++;
trace("Note: "+b);
if (b>layTracks[0].length) {
if (loop) {
b = 0;
} else {
clearInterval(id);
}
}
}, (60000/BPM));
}
}

NJB - Sep 14th 2007, 6:46PM Link | Report
I suggest using the "Quote" Option to get the code intact, it'll be more reliable than trying to repair the emoticon damage manually.
NJB - Sep 14th 2007, 7:59PM Link | Report
Well, I've sorted out that problem, but now I've found a new one.
For all notes after the 2nd, it just repeats the second note.
Page: