Switch?

Hi kirupians!

Long time since i don´t ask questions here, most of my problems are solved with the fabulous search feature ;), but this time even the search didn´t helped me :frowning: that´s why i´m posing this question to you flash wiz on the prowl :stuck_out_tongue:

I want to have a .SWF that loads other .SWFs with a set of conditions and variables to control the load order. like:

**loader.swf ** (blank, with only scripts to control the load order) will first load the a.swf;
when the a.swf ends it tells the **loader.swf ** to play the b.swf;
and when b.swf the ends it tells the **loader.swf ** to play the c.swf;
and when c.swf the ends it tells the **loader.swf ** to play the d.swf;
and when d.swf the ends it tells the **loader.swf ** to play the a.swf again;
and…

I tought that i could use the switch action to do that for me, but i don´t know how.
Any one out there who knows how to do that, with switch or whatever ??

[SIZE=1](sen are you there?)[/SIZE]

Switch is just a cleaner way of doing multiple if-then statements. Whatever you can do with switch can be done with if-then.

For your project, you can simply have the loader track the loading progress of each of the other SWF’s. So as soon as loader sees that a is loaded, it lods b, and so on. I don’t see how a switch statement would help with this…or maybe I’m interpreting your question wrong. :pirate:

-Al

I wouldnt nec. use a switch. I think Id use an array. Depending on whether or not these are in levels or movieclips, youd populate the array with the locations of the swfs (with all loading issues aside).
ie:
control_array = [_level1, _level2, _level3, etc…];
using movieclips if loaded in movieclips.

There you would use an index variable to determine your position in the array and what swf/level to play and to check for when determininig if it has finished or not. When it has finished, increment the index variable and play the next level accordingly, being sure to cycle back down to 0 once you pass the length of the array.

something along the lines of


control_array = [_level1, _level2, _level3];
index_pos = 0;

control_array[index_pos].play();

this.onEnterFrame = function(){
  if (control_array[index_pos]._currentframe == control_array[index_pos]._totalframes){
    control_array[index_pos].gotoAndStop(1);
    index_pos++;
    index_pos %= control_array.length;
    control_array[index_pos].play()
  }
}

thanks for the reply alethos :slight_smile:

but you missed this:

*Originally posted by Guig0 *
**…Any one out there who knows how to do that, with switch or whatever ??..SIZE] **

[SIZE=1]sen?[/SIZE]

there was a thread earlier today about a switch statement. If you want to know how that functions, I think it will give a little insight

…oops nevermind you already saw that thread haha ok Ill doa little writeup about the switch statement.

btw, for others, the thread is
http://www.kirupaforum.com/showthread.php?s=&threadid=15391

Switch

What it does
The switch action is an if-like action for comparing one expression with other multiple expressions. Switch offers nothing more then that which could be achieved with if clauses, though it does offer a more compact and concise alternative way of coding it. Switch however, only checks for equality, and strict equality ( === ) at that. This means that true is equal to true but true is not equal to 1 (1 is equal to true if == (equality) is used). There are no less thans or greater thans with switch. So how does it work?

Setup
Switch is comprised of a switch block which within contains multiple case blocks. The switch action maintains the original comparison value and each case contains the value to which it is to be compared. Following each case block are the commands which are to be run if the comparison between the switch value and the case value resolves to be true.

switch (expression) {
	case (expression):
		statements...
	case (expression):
		statements...
	...
}

Heres a simple example in context

switch (myVar) {
	case 1:
		trace("myVar equals 1");
		break;
	case 2:
		trace("myVar equals 2");
		break;
	case 3:
		trace("myVar equals 3");
		break;
	default:
		trace("No match was found");
}

This goes through and checks myVar to equal (strictly) each of the values following the case keyword within the switch block. Here we check for 1, 2 and 3. Each case is checked in order from top to bottom until one is found to be equal. When it is, the following code is execueted. If myVar was 1 here, “myVar equals 1” would appear in the output window since the case 1 clause was equal to myVar and the code was run. In terms of an if/else statement, the above would look like:

if (myVar === 1) {
	trace("myVar equals 1");
}else if (myVar === 2){
	trace("myVar equals 2");
}else if (myVar === 3){
	trace("myVar equals 3");
}else{
	trace("No match was found");
}

Default
The defauly case - which doesnt use the case keyword - contains the statements which are to be run if none of the previous case clauses evaluated to be true. If myVar was 4, the default clause would be run since neither of the previous case clauses were found to be equal to myVar. You dont have to use the default case if you dont want to. Its use is optional.

Break
The break statement exits the switch block. This is needed because if a case statement is equal to the switch expression, the statements in that case AND all following case statements are run. Using break will cause only that case statement to run and then skip all the rest since at that point you wont have to worry about the following cases. You may, however, not want to use break in some cases. For instance, you can check myVar to be either one value OR another value and have certain code run based on that. For example:

switch (myVar) {
	case 1:
	case 2:
		trace("myVar equals 1 or 2");
		break;
	case 3:
	case 4:
		trace("myVar equals 3 or 4");
		break;
	default:
		trace("No match was found");
}

Since there is no break after case 1: or case 3: if myVar is either of those values, it will run through the case statments associated with that case block (none) and continue to the next case block and run those statements (trace) until reaching the break which exits the switch block completely. As an if/else this would be:

if (myVar === 1 || myVar === 2) {
	trace("myVar equals 1 or 2");
}else if (myVar === 3 || myVar === 4){
	trace("myVar equals 3 or 4");
}else{
	trace("No match was found");
}

Operation
So what REALLY happens? Heres the deal. switch sets the code to be used in the first expression of the comparison. This doesnt have to be a variable, it can just as well be a function - as long its something that resolves down to a single comparable value. When the switch statement is first encountered, this code is not initially run, but waits the comparison with the first case statement. When the first case is reached, the case expression (which can also be a function) is then thrown in a strict comparison operation, the switch expression being evaluated first, followed by the case and then the returned result (or the comparison) is given. If this is false, the case statements are skipped and the next case is evaluated. If true, all following case evaluations are stripped from the switch block and all the following statements are more or less combined to be a single executed block of code. Heres an example which can show you how this works:

switchVar = 0;
caseVar = 0;

switchCall = function(){
	switchVar++;
	trace("switch, switchVar: " + switchVar + ", caseVar: " + caseVar);
	return switchVar;
}
caseCall = function(){
	caseVar++;
	trace("case, switchVar: " + switchVar + ", caseVar: " + caseVar)
	return caseVar;
}

switch( switchCall() ){
	case caseCall():
		trace("first case");
	case caseCall():
		trace("second case");
	case caseCall():
		trace("third case");
	default:
		trace("default case");	
}

Here you can see the switch call being run first, setting switchVar to 1. This is reflected in the case call which shows the new switchVar and sets its own caseVar. Each are returned and at the first case, the equality is true. Because there are no breaks, all case traces are called, but note the comparisons also stop (no more function calls). Once there is a true comparison, all further comparisons cease. To call the functions for each case, all youd need to do is change the initial set value of switchVar or caseVar to something different than the other. Then you can see each function call as its run for each case (though NOT the default case as it does not compare).

Here you need to be careful though. Be aware that switch (++myVar) is different than siwtch (myVar++) since in the instance of ++myVar, myVar will be incremented prior to the case check, while myVar++ will be incremented after.

[NOTE: The repeating switch condition evaluation is specific to Flash MX only, Flash MX 2004 fixes this and the switch is condition is only checked/set once and compared from there with all case comparisons]

The down and dirty of it all is that switch allows you to easily write out concise if equaity conditions in a simple alternative manner. Needed? No. Helpful? Sometimes. heh - I actually never use it, though, no doubt it can come in handy under the right circustances.

For more information, look up switch for Javascript - it should be the exact same thing and theres no doubt tons more documentation out there for it.
:slight_smile:

sen: i forgot to tell that all movieclips are loade in the same level (1) :frowning: sorry

i think that this code will need to be changed now, but i don´t know exactly how :stuck_out_tongue: can you give me a hand?

in that case, the levels would change into the .swf files ie

control_array = [“a.swf”, “b.swf”, “c.swf”, …];

then its a matter of loadMovieNum with the current array element

0 is better than MénageÀTrois :wink: or would that be MéowÀTrois

[edit] hey, whered that post go?[/edit]

hmmm… let me get this straight:

i´m not totally confident that this will do.
this code will load and check if the actual loaded movie has ended to load the next?

can you whip up an example fla for me?

am i asking too much? if so, plz tell me to bug off :stuck_out_tongue:

and there is a way other than the use of _totalframes to load the next movie. coz you know, i can have a movie with a single frame and a clip inside that plays…
like a variable or sumthing coming from the loaded movie to the main movie… ex: _level0.variable=value; to trigger the next load action?

heres a total frames example.

if not by total frames then youjust need an alternative way to decide when the movie has ended. If you want you can have the swfs shoot out a call to level0 and tell loader swf to load a new file.

wow sen! that´s exactly what i was looking for!

i´ll work on the other way without the totalframes thing, but for now that will do.

Thanks a bunch!!! =)

*Originally posted by senocular *
**0 is better than MénageÀTrois :wink: or would that be MéowÀTrois

[edit] hey, whered that post go?[/edit] **

While I was writing my post, you two had already had a full blown discussion. And you said you’d already posted an explanation of the switch statement…so I deleted. :slight_smile:

-Al

aww, that was a good description too… you should have kept it up and not let your work on it go to waste. :frowning:

its appreciated

Thanks, sen, but I dislike reading through repetitive stuff when I’m trying to learn something, so I don’t put people through it if I can help it. Plus, I’m sure your explanation is spot on. :slight_smile:

I like the MéowATrois thing tho :slight_smile:
-Al

*Originally posted by alethos *
**Plus, I’m sure your explanation is spot on. :slight_smile: **

which means you didnt check it :wink: which means chances are, as a link, other people too will skip it. Which means having an in thread explanation (such as yours) may just have benefited more people!

ok Im starting to feel like Im making it seem like Im trying to make you feel bad about doing that heh but Im not. I just dont want to discourage you from such input in the future if you know what I mean :wink: Im sure you do… Im just rambling. Anywho, Ill throw a more thorough explanation here when I get the chance, the thread title seems fitting enough.

I want to thank you all for showing up and help a friend :slight_smile:

Thanks alethos and sen for the help! i really apreciated that =)

Cheers :bounce:

ok a little switch explanation up on page 1 of this thread (my second post)

*Originally posted by Guig0 *
**Long time since i don´t ask questions here, most of my problems are solved with the fabulous search feature ;), but this time even the search didn´t helped me :frowning: **
Well, look again, there’s even a tutorial about switch :stuck_out_tongue:

*Originally posted by ilyaslamasse *
**Well, look again, there’s even a tutorial about switch :stuck_out_tongue: **

I have seen that ilyas :wink:

But it wasn´t quite what i need it to be, and since i don´t know how to do the proper changes… (-: