PDA

View Full Version : Arrays in Reverse


AllenSeattle
02-01-2008, 10:06 PM
Hi I recently found a great tutorial on loading external swf files using an array to load. I realized that there wasn't a heck of a lot of examples online for complex classes, but this is a good site (www.flashandmath.com) !

Anyway...

I began to develop my own move loader, but want to expand the math portion of the button function that allows the user to jump through the array. What I would like to have is another button that will go the opposite direction through the array, thus display previous movies. Each loaded movie is a simple animated clip.

Unfortunately, I am a designer, and have never used flash for heavy coding.
I am aware of how much I am missing, but would like to at least see how it is done so that I can scratch my head some more. If anyone has a way to make this work, it would be much appreciated! Thanks in advance!

Here is my code:
/*
This file comes from www.flashandmath.com
*/

// Array of external clips to use. Variable index refers to next clip to be displayed.
var clips:Array = ["clip0.swf", "clip1.swf", "clip2.swf"];
var index:int = 0;


// Stuff to load swf files
var thisLoader:Loader = new Loader();
thisLoader.contentLoaderInfo.addEventListener(Even t.INIT, doneLoading);

var thisMC:MovieClip = new MovieClip();
stage.addChild(thisMC); // Add empty MC initially so the nextClip function can be generic

// Removes old MC and gets the next one, waiting until when it has initialized beore adding it to the stage
function nextClip():void {
thisLoader.load(new URLRequest(clips[index]));
}


/*I added this function to try to reverse the order of the array being displayed*/

function prevClip():void {
thisLoader.load(new URLRequest(clips[index]));
}

// Tell AS that the loaded file is a movie clip and add it to the stage.
function doneLoading(e:Event):void {
stage.removeChild(thisMC);
thisMC = MovieClip(thisLoader.content);
thisLoader.unload();
stage.addChild(thisMC);
thisMC.play();
}


// "Next button" just calls a function that goes to the next file name (mod the number of files in the list)

btnNext.addEventListener(MouseEvent.CLICK, playNext);

function playNext(e:MouseEvent):void {
nextClip();
index = (index + 1)%(clips.length);
}


/*I added this button event and function to try to reverse the order of the array being displayed*/

btnPrev.addEventListener(MouseEvent.CLICK, playPrev);

function playPrev(e:MouseEvent):void {
prevClip();
if(index > 0){
clips.reverse();
index = (index + 1)%(clips.length)
}

}

jaga
02-02-2008, 02:49 AM
Well.. I don't know the tutorial, but it does things in an... interesting way.

To fix it isn't so hard, though. First, you don't need the function prevClip, since it does the exact same thing as nextClip.

Now in the playNext function, they're increasing the index number AFTER telling it to load the clip. This means it can only go in 1 direction (unless you do some other fancy stuff). Pretty strange. Quick fix though, switch the lines:


function playNext(e:MouseEvent):void {
index = (index + 1)%(clips.length);
nextClip();
}


so now that function is increasing the index by 1, and then telling it to play. Now you need only do the reverse for your playPrev - decrease the index by 1 and then tell it to play. No need to change the whole array around..

so


function playPrev(e:MouseEvent):void {
index--; // this means index = index - 1
if(index < 0) { // if the index is -1, wrap around to the end of the array instead
index = clips.length - 1;
}
nextClip(); // Remember, this just tells it to load the clip at the index
}


And that's it! Hope that works for you, so long as I didnt make any typos