Notice: This is a tutorial for AS3:Main, although AS3:Main does not see a lot of traffic, I still use it as a learning resource, so I'm giving back to the Newgrounds Community. Please do not spam "Way to bump a dead thread" or anything of that nature, thanks.
Hello, In this tutorial, I will be covering the Switch/Case conditional. Switch/Case acts in many ways like the if loop/conditional. This conditional is used to text the value of a variable against multiple cases. The most obviously helpful use for switch/case is toggling variables, and that is what we will be doing in this tutorial.
Final code will be at the end of this tutorial, please follow along for explanations.
Step 1:
Open up Flash and create a new .fla, Actionscript 3 of course.
Please set up your .fla with the following setup:
3 Layers, top to bottom named:"Actions", "Button", and "Shapes"
Now then, lock the Actions layer to prevent any accidental editing, it should be blank.
On the Button layer, make a button of some sort and make its instance name"qualToggle".
On the Shapes layer, draw some shapes, this way we can see the effects of our Quality Toggle.
Step 2:
With the visuals taken care of, its time to get down to some coding.
In the Actions layer, open the Actions panel, and now we can begin, follow my coding, each section/line will have explanations.
a. The first thing we should do is attach an EventListener to our button:
qualToggle.addEventListener(MouseEvent.CLICK, toggleQuality);
b. Now that the "qualToggle" button links to an EventListener, clicking it will call the function "toggleQuality", which we will code now:
function toggleQuality(event:MouseEvent):void{
c. We have just opened a function, the parentheses which read "(event:MouseEvent)" associate this function to a MouseEvent, only.
d. We will now start using switch when using this conditional, you place the variable you wish to test in parentheses next to the statement "switch", as follows:
switch(stage.quality){
e. We have just written a switch to test the stage's quality property, when you check the stage's quality, it is returned as a String variable in all caps.
f. We will now write our first case. A case is essentially a condition that the switch is compared to.
case "HIGH":
stage.quality = StageQuality.LOW;
break;
g. This code is pretty simple, the case statement checks to see if our stage's quality is "HIGH" and if it is, it sets it to LOW. The break; statement is an essential part of the switch conditional, break; signifies the end of a case. If the break were not there, undesired results would occur, because each case would run into the one above it, which would produce errors.
h. With that explained, we can write another case, this is easy, simply follow the format of the first one:
case "MEDIUM":
stage.quality = StageQuality.HIGH;
break;
i. And now, we will write our final case which will make this toggle work:
case "LOW":
stage.quality = StageQuality.MEDIUM;
break;
j. The switch must be closed with a bracket, as well as the function:
}
}
And this is our resulting code (below) I hope you enjoyed this tutorial and found it helpful, if you test your Flash movie, you should now see that the Quality Toggle button you made will swap the quality of the movie.
Final Code:
qualToggle.addEventListener(MouseEvent.CLICK, toggleQuality);
function toggleQuality(event:MouseEvent):void
{
switch(stage.quality)
{
case "HIGH":
stage.quality = StageQuality.LOW;
break;
case "MEDIUM":
stage.quality = StageQuality.HIGH;
break;
case "LOW":
stage.quality = StageQuality.MEDIUM;
break;
}
}
Happy Coding,
-Sage