PDA

View Full Version : switch ???



Guig0
February 17th, 2003, 02:39 PM
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 :( that´s why i´m posing this question to you flash wiz on the prowl :P


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 ??

(sen are you there?)

alethos
February 17th, 2003, 02:52 PM
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

senocular
February 17th, 2003, 02:54 PM
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()
}
}

Guig0
February 17th, 2003, 02:55 PM
thanks for the reply alethos :)

but you missed this:

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







sen?

senocular
February 17th, 2003, 02:58 PM
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.
:)

Guig0
February 17th, 2003, 03:00 PM
sen: i forgot to tell that all movieclips are loade in the same level (1) :( sorry

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

senocular
February 17th, 2003, 03:05 PM
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

senocular
February 17th, 2003, 03:07 PM
0 is better than MénageÀTrois ;) or would that be MéowÀTrois

hey, whered that post go?

Guig0
February 17th, 2003, 03:11 PM
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 :P

Guig0
February 17th, 2003, 03:20 PM
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?

senocular
February 17th, 2003, 03:31 PM
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.

Guig0
February 17th, 2003, 03:39 PM
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!!! =)

alethos
February 17th, 2003, 10:28 PM
Originally posted by senocular
0 is better than MénageÀTrois ;) or would that be MéowÀTrois

hey, whered that post go?

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. :)

-Al

senocular
February 17th, 2003, 10:31 PM
aww, that was a good description too... you should have kept it up and not let your work on it go to waste. :(

its appreciated

alethos
February 17th, 2003, 10:38 PM
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. :)


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

senocular
February 17th, 2003, 10:47 PM
Originally posted by alethos
Plus, I'm sure your explanation is spot on. :)

which means you didnt check it ;) 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 ;) 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.

Guig0
February 18th, 2003, 07:21 AM
I want to thank you all for showing up and help a friend :)


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



Cheers :bounce:

senocular
February 18th, 2003, 08:34 AM
ok a little switch explanation up on page 1 of this thread (my second post)

pom
February 18th, 2003, 09:26 AM
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 :( Well, look again, there's even a tutorial about switch :P

Guig0
February 18th, 2003, 09:32 AM
Originally posted by ilyaslamasse
Well, look again, there's even a tutorial about switch :P
I have seen that ilyas ;)

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

senocular
February 18th, 2003, 09:33 AM
Originally posted by ilyaslamasse
Well, look again, there's even a tutorial about switch :P

bust

Guig0
February 18th, 2003, 09:35 AM
not! :P

senocular
February 18th, 2003, 09:36 AM
I didnt know that was there either ;) I didnt bother to search - I just figured your search was right :P

s'alright, I covered some stuff that wasnt there :)

Guig0
February 18th, 2003, 09:38 AM
what are you talking about?? my search was right!! :bad:

i found switch, but not the solution i wanted... dammit!


:P

senocular
February 18th, 2003, 09:42 AM
thats true, it was the loading thing... heh :!:

rkalexander
May 5th, 2007, 05:04 PM
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.

Where is the example?

senocular
May 6th, 2007, 11:38 AM
sometime between now and the 4 years ago since this was posted, the forum lost all attachments. That example was a casualty of war :(

rkalexander
May 6th, 2007, 09:03 PM
wow i didn't notice how old that post was.