Ambinaut

Code sample

This script belongs to the SoundPlayer's "cue" frame. It spreads out the loading of sound clips, allowing play to begin almost immediately.
// initialise the space loop counter
spaceLoop = 32;

// cue a new track
target = random(4);
trace("");
trace("you chose track " + target);

// if the target track hasn't been loaded yet, we load it now.
// while it loads, we play any track that was loaded earlier (random choice).
// if no tracks have been loaded yet, we pause briefly then cue again.
if (target > 0 && !isAvailable(target))
{
  // the target track hasn't been loaded yet
  trace("track " + target + " isn't ready");
  originalTarget = target;

  // pick an alternative (already loaded) track...
  if (availableTracks.length > 0)
  {
    trace("but i can offer you:");
    for (var i = 0; i < availableTracks.length; i++)
    {
      trace("track " + availableTracks[i]);
    }

    var index = random(availableTracks.length);
    target = availableTracks[index];
    trace("you chose " + target);
  }

  // ...and quietly preload the original target
  if (originalTarget > 0)
  {
    if (!slot[originalTarget-1].isLoading())
    {
      slot[originalTarget-1].loadMovie(clip[originalTarget-1]);
      trace("i'll preload track " + originalTarget);
    }
  }

  // note: once loaded, the original target will be 
  // appended to the availableTracks array 
  // (see slot clip event handlers)
}

// play the target track
if (availableTracks.length > 0)
{
  // we have one or more preloaded tracks, so play
  trace("now i'll play track " + target);
  gotoAndPlay("track" + target);

  // debug output
  if (target == 0)
    trace("< spacing out >");
  else
  if (target == 1)
    trace("track " + target + " (waves2)");
  else
  if (target == 2)
    trace("track " + target + " (waves3)");
  else
  if (target == 3)
    trace("track " + target + " (waves4)");
}
else
{
  // we have no preloaded tracks, so cue again
  spaceLoop = 1;
  trace("nothing to play yet");
  trace("so i'll try again...");
  trace("< wee pause >");
}

trace("");