00:00
00:00
Newgrounds Background Image Theme

seaquester just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As3: Switch/case Conditional

10,588 Views | 3 Replies
New Topic Respond to this Topic

As3: Switch/case Conditional 2010-06-17 20:05:39


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.

AS3:Main

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

Response to As3: Switch/case Conditional 2010-06-17 21:53:59


I've always tried to avoid using Switch/Case statements because I could never be bothered to learn them.

So essentially is the case carried out only when it is equal to the switch's value?


Sig by CoolCatDaddio. <<RESPECT>>

BBS Signature

Response to As3: Switch/case Conditional 2010-06-17 23:09:21


No, the switch identifies the variable you want to test, the case that is equal to the variable will be executed.

Response to As3: Switch/case Conditional 2010-06-18 01:06:24


At 6/17/10 08:05 PM, Oldsage10 wrote: 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.

Not actually the case, leaving out the break can be a legitimate form of coding. The only thing to note is that if a break is not encountered the code will continue to run the following cases until the end or until a break is found somewhere.

Also it's worth mentioning the default case, helpful and also actually good practice to include in any switch statement. The default works just like another case except that it will run when the value of the variable does not match any of the other cases.

In this example the default case will run and "not between 1 and 3" will be traced:

var butt:int = 4;
switch(butt) {
	case 0:
		trace("zero");
		break;
	case 1:
		trace("one");
		break;
	case 2:
		trace("two");
		break
	case 3:
		trace("three");
		break;
	default:
		trace("not between 1 and 3");
		break;
}

Good tutorial, nice to see someone's still making them


.

BBS Signature