switch(case)

So looking through my logs, I realized a ton of users have gotten to my blog by searching “flash switch case”. And well I figured I’ll give all of you who made it here, a little bit of help. First I’ll add a button directly to the Actionscript 3 API reference to the buttons on my main nav. And the rest of this article will consist on giving you a few basic examples on how to use it. Sorry for the delay, I should’ve done this a long time ago!

Adobe AS3 Languange Reference – Switch Statement

Switch statement can be quite useful, from shrinking down your navigation controls into one event listener, to determining something useful about a user’s environment. Now just because my blog is named switch case, I don’t mean to tell you that its the end all for statements. With good coding practices you will learn when it is more useful to a switch statement as opposed to just some if…else statement… but they kinda do the same thing… compare one thing to another, then return execute some piece of code to make your life better. The big difference is switch only tests for a specific case, so you cant really feed it a range (well there’s hacks, but then you’re not really testing against the expression you’re actually trying to evaluate. and its just plain dumb… unless you are that one in a million case, else just stick to the basics).

Here we go example one, testing for whether you clicked on a certain object, who’s name you know:
Source

package
{
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.events.EventPhase;
	import flash.display.Graphics;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Sprite;
 
	/**
	 * @author jbonfante
	 */
	public class SwitchCase extends Sprite
	{
		private var statusText : TextField = new TextField();
		private var count : int = 0;
		private var nameArray : Array = ["FirstBox","secondBox", "ThirdBox"];
 
		//Simple Switch Case Example
		public function SwitchCase()
		{
			statusText.width = 300;
			statusText.height = 22;
			addChild(statusText);
			statusText.text ="Status Text";
			for(count;count < nameArray.length;count++)
			{
				//use a for loop to generate buttons
				addChild(makeBox(nameArray[count]));
			}
			addEventListener(Event.ADDED_TO_STAGE, BeginTestCase, false, 0, true);
		}
 
		private function BeginTestCase(event : Event) : void
		{
 
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			removeEventListener(Event.ADDED_TO_STAGE, BeginTestCase);
			stage.addEventListener(MouseEvent.CLICK, SwitchTest, false, 0, true);
		}
 
		private function SwitchTest(event : MouseEvent) : void
		{
			/**
			 * If you only want to avoid the switch statement
			 * firing off when you click the stage, or the textfield uncomment this if statement
			 * and you'll see the default statement will never run.
			 */
			//if(event.eventPhase == EventPhase.BUBBLING_PHASE)
			//{
 
				switch(event.target.name)
				{
					case nameArray[0]:
						statusText.text = "You clicked : " + nameArray[0];
						break;
					case nameArray[1]:
						statusText.text = "You clicked : " + nameArray[1];
						break;
					case nameArray[2]:
						statusText.text = "You clicked : " + nameArray[2];
						break;
 
					default:
					statusText.text = "You clicked something else... maybe the stage?";
 
				}
			//}
		}
 
		private function makeBox(name : String) : Sprite
		{
			//Box generating code,notice the name of the box gets set here
			var aBox : Sprite = new Sprite();
			var theG : Graphics = aBox.graphics;
			theG.beginFill(Math.random()*0xDD, .75);
			theG.drawRect(0, 0, 75, 75);
			theG.endFill();
			aBox.name = name;
			aBox.x = 80 * count;
			aBox.y = 30;
 
			return aBox;
		}
	}
}

And that is a simple case. If you have any other questions let me know. The next one is a simple example using the eventPhase, something the built in to the flash player, and one of the many that change throughout the life of a flash program, well more specifically as an event runs through the EventDispatcher.

No SWF for this once since it’s all traces:
Source

package
{
	import flash.display.Graphics;
	import flash.events.EventPhase;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.Sprite;
 
	/**
	 * @author jbonfante
	 */
	public class EventCase extends Sprite
	{
		public function EventCase()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			addChild(makeBox("RandomBox")); // so we can see the target phase
			/**
			 * In order to reach the capture phase you have to set an event listeners flag of
			 * useCapture = true
			 *
			 * if true is set, only the capture phase will be caught, target or bubble
			 * will never be reached.
			 *
			 * But you'll notice in the both target a bubble are reached when setting
			 * useCapture = false
			 *
			 * there is no flag to just test for Bubbling or Target, they're part of the same
			 * unless of course an even is set to not Bubble.
			 *
			 * In this case the MouseEvent always bubbles....
			 * But now we're getting into custom events, not part of this tutorial
			 */
			stage.addEventListener(MouseEvent.CLICK, TraceOutPhase,false,0,true);
			stage.addEventListener(MouseEvent.CLICK, TraceOutPhase,true,0,true);
 
		}
 
		private function TraceOutPhase(event : MouseEvent) : void
		{
			switch(event.eventPhase)
			{
				case EventPhase.AT_TARGET:
				trace("Reached event target phase");
				break;
 
				case EventPhase.BUBBLING_PHASE:
				trace("Event bubbling phase");
				break;
 
				case EventPhase.CAPTURING_PHASE:
				trace("Reached capture phase.");
				break;
 
				default:
				trace("Somehow this event has no phases... not likely");
			}
		}
 
			private function makeBox(name : String) : Sprite
		{
			//Box generating code,notice the name of the box gets set here
			var aBox : Sprite = new Sprite();
			var theG : Graphics = aBox.graphics;
			theG.beginFill(Math.random()*0xDD, .75);
			theG.drawRect(0, 0, 75, 75);
			theG.endFill();
			aBox.name = name;
			aBox.x = 80 ;
			aBox.y = 30;
 
			return aBox;
		}
	}
}

Emjoy.


    Juan B.

    Leave a Reply

    Meta