Tag: tutorial
Using the Ternary Operator
by Josh on Jul.07, 2005, under ActionScript, Development, Flash
Just a quick mini-tutorial on using the ternary operator in ActionScript. One of those things that shows up in my search logs, so the next time someone searches for it, they should be able to find it.
The ActionScript Ternary Operator
The ternary operator, "? :", is basically a shorcut for writing quick conditional statements that evaluate to either true or false. Similar to an if/else statement, you use them to make a decision in your code, and act based on that decision.
Here's the syntax:
-
// using the ternary operator
-
(condition) ? isTrueAction : isFalseAction;
-
The ( ) around condition are optional, however they do help to clearly illustrate the condition. The code above is the same as writing:
-
// the same thing, only more typing
-
if (condition) {
-
isTrueAction;
-
} else {
-
isFalseAction;
-
}
-
Now, you won't be able to use this for something that has multiple statements after the condition like you can in an if statement, but it does have it's uses for quick little things. For example, you couldn't use it for something like this:
-
// this can't be written out using ternary operators
-
if (condition) {
-
isTrueAction;
-
var bob:String = "something";
-
goDoSomethingNow();
-
} else {
-
isFalseAction;
-
}
-
But it's great for something like this:
-
// this is a great use for ternary operators
-
(superBowlWon == true) ? gotoDisneyland() : goHome();
-
Or even for checking if a number is even or not.
You can also use this technique to set values for variables:
-
// setting a variable like this...
-
var pillToTake:String = (stayInMatrix == true) ? "blue" : "red";
-
//
-
// is the same as doing this:
-
if (stayInMatrix == true) {
-
var pillToTake:String = "blue";
-
} else {
-
var pillToTake:String = "red";
-
}
-
So, as you can see, using this operator can save you time by requiring less typing in a few situations. Now, it's not the solution to everything, but as you start using it more, you'll start to wonder how you worked without it. (Maybe not, but it'll make me feel better if I can imagine that I've somehow improved your life.)
Also, after I finished writing this, I decided I wanted to try and determine which method is faster, to see if there's another argument for learning how to use this stuff. So in a pretty informal, and un-scientific way, I ran the following test:
-
// testing ternary operator vs. if/else
-
// just copy and paste onto the main timeline, and run
-
//
-
var loops:Number = 10000;
-
//
-
// ternary test
-
var startTime1:Number = getTimer();
-
for (var i:Number = 0; i < loops; i++) {
-
var testNum:Number = Math.floor(Math.random() * 100);
-
var isEven:Boolean = ((testNum % 2) == 0) ? true : false;
-
}
-
var endTime1:Number = getTimer();
-
var totalTime1:Number = endTime1 - startTime1;
-
//
-
// if/else test
-
var startTime2:Number = getTimer();
-
for (var i:Number = 0; i < loops; i++) {
-
var testNum:Number = Math.floor(Math.random() * 100);
-
if ((testNum % 2) == 0) {
-
var isEven:Boolean = true;
-
} else {
-
var isEven:Boolean = false;
-
}
-
}
-
var endTime2:Number = getTimer();
-
var totalTime2:Number = endTime2 - startTime2;
-
//
-
trace("Total Times: \n");
-
trace("\tTernary: " + totalTime1);
-
trace("\tif/else: " + totalTime2);
-
Running this test on my PowerMac with dual G5 2.0, and 1 GB ram, I came up with the following averages after running it 20-30 times: Ternary: 201 ms, if/else: 195 ms. Now, your results may vary, and this most likely isn't the best way to test something like this, but it's enough for me to see that there is a speed difference, but it's not enough of a difference to really make me stick to if/else exclusively.
Setting up the Subversion Client on Mac OS X
by Josh on Jul.05, 2005, under Development, Mac
These instructions will guide you through the process of installing and using the Subversion Client on OS X. Later I'll do a post on how to setup your own SVN repositories, but for now it's just how to setup the client to access repositories. The initial setup takes some time, (approx. 20-30 minutes), and you will need to use the Terminal, so pay attention, and we'll get through this together. (continue reading...)
ActionScript testing for even number
by Josh on Jan.14, 2005, under ActionScript, Flash
Not much in today's post. I was checking my web stats last night, and the search terms "actionscript testing for even number" showed up in the top ten searches from Google. I haven't posted on this, so I don't know how I got these search terms in my logs ( I once got "deep frying turkeys"), but as a service to whoever was looking for that, here you go:
Dynamic Drawing Using ActionScript
by Josh on Jan.09, 2005, under ActionScript, Flash
When Flash MX was introduced, several new features were added to both the authoring enviroment and to ActionScript. My favorite group of new methods added were the drawing methods added to the MovieClip class, also referred to as the Drawing API. This tutorial will try to help you in understanding and using these methods.
(continue reading...)