PDA

View Full Version : can default from switch/case be used in another switch/case?



lunatic
December 10th, 2003, 06:01 PM
I am doodling at work :jail: and wondering if this is possible. Unfortunately I don't have flash in front of me so I can't test it but since my AS is so horrible it probably wouldn't help anyway!

I want this on an MC:

i = 0;
switch(thing){
case 0:
some action; break;
case 1:
some other action; break;
default:
i++;
}

if (i < 7) {
switch (i) {
case 1:
something else; break;
case 2:
something something else; break;
}}


etc.

Basically what I'm wondering is, will I lose the value of i in the first switch once I'm out of the switch? Can the second switch read the 1st switch?

norie
December 10th, 2003, 08:32 PM
I know you can use 'break' and 'continue' in other statements, but as for default--i think it's only used to define the default action in a switch statment.

here's what MM says:

default
Availability
Flash Player 6.

Usage
default: statements

Parameters
statements Any statements.

Returns
Nothing.

Description
Statement; defines the default case for a switch action. The statements execute if the expression parameter of the switch action doesn't equal (using strict equality) any of the expression parameters that follow the case keywords for a given switch action.

A switch is not required to have a default case. A default case does not have to be last in the list. Using a default action outside a switch action is an error and the script doesn't compile.

Example
In the following example, the expression A does not equal the expressions B or D so the statement following the default keyword is run and the trace() action is sent to the Output panel.

switch ( A ) {
case B:
C;
break;
case D:
E;
break;
default:
trace ("no specific case was encountered");
}

lunatic
December 11th, 2003, 11:24 AM
So would it be better to use a really long if/else statement if I wanted to use the value of i later in the script?

That doesn't seem very efficient?:te:

What about a global variable? I'm a little fuzzy on how they work but would that solve the problem?

norie
December 11th, 2003, 02:30 PM
_global variables, are variables which can be retrieved/set across any frame of any movie clip, of any scene, which makes _global variables very powerful, but they can get real messy if you over use them. global variables are very common in movies where there are mulitple scenes, b/c when you change scenes variables are usually reset to undefined--that is, except globals. You can also use them if you are easily confused about paths. nomally i reference variables from the root (ie _roo.var1). this makes the path Very easy.(plus i hate using scences, i'd rather use multple swfs) but i also use a combination of local variable inside movieclips. the value of i is always excessible, but only the last value of i. if you were to run your code in the _root. , or reference i as _root.i , it's alwasy retrievable.


if (i < 7) {
switch (i) {
case 1:
something else; break;
case 2:
something something else; break;
}
}
in this code you don't even need the if statement b/c the switch case statement won't have any result unless you define a default action. however the if statement would be relevant if there was an default action defined, and you didn't want the default to run unless i was less than 7. Then again you could always add this:

case (i <7):
break;

well, i think, but don't quote me on that.

lunatic
December 11th, 2003, 02:59 PM
Okay so let me play out a real world example.

Let's say I'm getting multiple choice type answers from an input box. I have a switch/case statement for letters A-F. If the user doesn't input one of those letters then I have a default that says i++ so I can keep track of how many times they guess wrong (i.e. they guess something h or higher).

BUT I want to limit how many times they can guess wrong (as long as they guess right they can keep guessing). So they get 6 chances to guess wrong. But every time they guess wrong I want to indicate how many more chances they get. Hence the 2nd switch/case for i. If i = 1 output "you have 5 more chances". If i = 2 output "you have 4 more chances" etc. plus do some other actions besides just output.

These two switch/cases are happenning on a movie clip that is on the main timeline. That way I can later exchange it for another MC (e.g. another "quiz") that maybe only has guesses A-D available instead of A-F so fewer cases in the switch statement.

So you are suggesting that if I create a variable i on the _root then I can just reference i wherever I want and as long as I use the right pathway I can maintain it's value? So the first time they guess wrong through the input box ( i =1 ) I can run that through the 2nd switch and the case will be 1. Then the second time they input i will increase to 2 in the first switch and will be case 2 in the second switch?

:bu:

norie
December 11th, 2003, 06:29 PM
egggggg----actly

lunatic
December 11th, 2003, 06:47 PM
Sweet! Gotta go try that out. Why did I think that was so hard? :h:

:stunned:

norie
December 11th, 2003, 07:01 PM
Originally posted by lunatic
Why did I think that was so hard? :h:

:stunned:
thinking makes things harder, i try not to think too much, it hurts
:beam:



:P

lunatic
December 11th, 2003, 07:11 PM
remove "things" and insert "i" (no pun intended) so it reads "i make things harder"! LOL!

Yeah, I think I popped quite a few brain cells on this one!
:trout: :P

Thanks again mah man for consistently coming through!