Ternary Operand
May 31, 2009
You might see complicated pieces of code that involve many ?’s and :’s. Well they may look complicated, but they are actually very simple to make.
Say you have a function. It will return A for 0, B for 1, C for 2.
You can do a switch statement, or use the ternary operand.
1 2 3 |
Looks complicated right?
It isn’t
This is how it goes:
(value you want to test)?if it’s true, do this:if it’s not, do this.
You can keep on adding more and more ternary operands into it to make it look complicated, like the one in my function.
(test1)?val1:(test2)?val2:val3;
So that translate to:
1 2 3 4 5 6 7 8 9 | if(test1) { return val1; }else if(test2) { return val2; }else{ return val3; } |
The ternary operand only returns values, though you can call functions.
Type Casting [2 Ways]
May 27, 2009
There are two different ways to type cast. One is using as and one is wrapping what you want to cast with the type you want it be casted into.
Way 1:
1 2 |
1 2 |
Now of course there wouldn’t be two ways to do it without there being a difference.
So the first way, if the type cannot be casted correctly(like trying to cast a MovieClip as an int) it throws an error.
If you use the second way, there is no error thrown. So for this reason I use the first method. Another reason would be the fact that the first method is faster than the second.
Loading Objects via Loader Class
May 27, 2009
First set up your main class. That is all you’re going to be needing in this tutorial.
1 2 3 4 5 6 7 8 9 |
Now import these:
1 2 3 |
Now set up your functions like this:
1 2 3 4 5 6 7 8 9 10 | public function main():void{ var loader:Loader = new Loader();//1 loader.load(new URLRequest('Put your own URL here'));//2 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);//3 addChild(loader);//4 } private function onLoader(e:Event):void{ trace('loaded'); } |
1. This line declares your loader.
2. This line tells it to load the object you want(SWF file, JPEG,GIF, ETC)
3. This line adds an event listener to know when the item loads. Notice you have to add an event listener to contentLoaderInfo.
4. The loader is added to the display list. You can either add the loader object right away or add it whenever the object loads(via onLoader function).
Loaders are as simple as that!
Display List (addChild, removeChild)
May 26, 2009
In AS3 there is a display list. This list contains all the objects that are being displayed.
Once you create an instance of a class or a movie clip, you need to add it to the display list for it to show up on the stage.
To do this, you call addChild().
Even if you haven’t added the Movie Clip to the display list, you can still modify it’s properties such as x,y, etc.
And of course if there is an addChild, there has to be a removeChild().
And sure enough:
Beware though, if objects are on the stage from the beginning(if you put a movieclip on the stage with the IDE and then compile it) you cannot remove it. Only objects added dynamically can be removed through removeChild.
In AS2, there was depth. Now there is an ‘index’.
So you have to do:
Remember to call it from the objects parents.
numChildren = nextHighestDepth.
If you set the index of an object higher than the numChildren-1, you will recieve an error. It’s not like in AS2 where you can set the depth to 9999. The index of objects have to be in order 0,1,2,3,4, and so on.
And as always, 0 is lower than 5. An object with an index of 5 will appear on top of an object with an index of 0.
A shortcut for adding a child then setting the index is addChildAt.
The first parameter is the child you want to add, and the second is the index you want it at.
Math Class Tutorial
May 26, 2009
I will be going over some useful math functions you might use in projects.
Math.ceil:
Math.ceil rounds the number supplied up no matter what if the decimal is above 0.
Ex: .01 would go to 1.
Math.floor:
Math.floor does the exact opposite of Math.ceil. This function drops off the decimal.
Math.max:
You have two parameters. You put whatever values you want in the parameters and it returns the one that’s the biggest.
Math.min:
Opposite of Math.max. It returns the smallest value.
Math.pow:
This has to do with exponents. The first parameter is the value. The second is the exponent.
So x^y = Math.pow(x, y); where ^ is exponentation.
Math.random:
This generates pseudo-random numbers(as computers can’t really generate random numbers). To use it is very simple.
would return a value from 0 to 10. The int is there because it acts like Math.floor() but it’s shorter. If you want to have a value beginning from a number other than 0, then do this:
Just replace the max and min obviously(or put it into a function).
Luckily for you guys, I have already made a function that handles this.
Go here
or
Functions in ActionScript 3
May 26, 2009
As I’m sure you’ve figured out by now, function declarations are a bit different in classes than normally(inside your document). For starters, you have to declare it public or private(incidentally, when you compile your project, all the code inside your document gets converted into a class so the compiler declares your functions public automaticly).
This is how you would normally declare a function:
1 2 3 | function myFunc():void{ } |
In a class, that same function would be declare as:
public function myFunc():void{
}
As you can see the only noticeable difference here is the public added in the beginning.
You can do much more with functions though.
But first let's get the basics out of the way.
1 2 3 |
1. This is the declaration of whether it's public or private.
2. This is saying that the type is a function
3. This is the name of the function. Remember you cannot name the function after a variable and you cannot name 2 functions with the same name
4. This is the return type of the function. A good practice is to set the return type as void if you do not return anything.
One thing you can do is a getter/setter function.
The concept is simple actually. Every heard of a read only property or a write only property? Well you can do that with a getter/setter function.
Say you have a private property called health. Only that instance of the class can access it, correct? What if you want it to be read only. If you have a health bar that needs to know what HP to display, you would need to have access to the health variable. So to make that variable available, instead of creating another public variable, make a getter function(To learn how to make a purely scripted AS3 HP Bar, check out this tutorial). Like so:
That right there is your getter function. To use this, just access it like this:
class.hp;
No need for parenthesis; it acts like a variable.
This is not the only thing you can do with a getter function. Say you need to calculate the hp through an algorithm(bad example, there are better) on the fly. So you would just do whatever you need to do to the health variable and return it. Remember, a getter function is just like a normal function except it acts like a variable and HAS to return a value.
A setter function is the same except the function has a set instead of a get, and you can use it to set a variable(obviously).
An example of you needing this would be when the character's hp changes. You would have the hp be a setter function and when it's accessed you can check to see if it's lower than 0, and if it is, then make the player lose.
A feature available only in AS3 is the ...rest feature. It let's you add unlimited amount of parameters in a function.
Say you have a function that creates enemies at specific coordinates. You might want to create more than one at enemy at once so you would do a ...rest parameter which allows you to have >1 parameters.
1 2 3 | public function createEnemies(...coors):void{ } |
So you can create as many enemies as you want by just passing along coors. Now you want to know how to process the coors and add enemies. It's simple. You treat it like an array.
1 2 3 4 5 6 7 |
What I did was make a for loop that went through coors like an array. Then I accessed the parameters like an array. It's pretty much like this:
coors[0] = first parameter
coors[1] = second parameter
and so on.
A few things to keep in mind are:
1. You cannot add another parameter after a rest parameter(because flash couldn't possibly now where the rest params end).
2. You cannot strict type it to a type other than Array. Like if you want the parameters to all be Points as the case above, you cannot set the type to Point. You will get an error.
And there you go folks! All you need to know about functions.
Classes Tutorial
May 25, 2009
The one thing every(well almost every) AS3 project uses are Classes. But for some the layout might be confusing. I’m here to help you with that. We will first create a Document Class. Here’s the basic layout of one:
1 2 3 4 5 6 7 8 9 |
You don’t always have to use main as your class name, but this is how I’m used to it.
1. This line imports MovieClip. What this does is allow you to let main ‘extend’ MovieClip. If you remove this line, you’ll get an error.
2. The class extends MovieClip. This means that your Class is a MovieClip. The Document Class always has to extend MovieClip no matter what
3. This is the function that gets called as soon as your class is used. And since this is the document class, that is the very first function called. Essentially it’s like the first frame in a project. So if you put trace(‘Hello World!’); inside this function, as soon as you compile, Hello World! will be traced.
An important thing you have to remember is that you have to save your class as the class name. For this one, you’d have to save it as main.as.
Okay so now you have the basics down of classes. Let’s move on to Static Classes!
A Static Class is a class that you do not have to declare. For example, let’s say we have a ball class:
1 2 3 4 5 6 7 8 9 |
To use this, you would have to call it(instantiate it):
1 | var myBall:ball = new ball(); |
If the ball class was static, you wouldn’t have to(in fact, you wouldn’t be ABLE to) instantiate it. To call a function, you would just use the dot operand. (ball.function())
So of course, the layout of a static class is slightly different.
Here is the ball class again, but this time in static:
1 2 3 4 5 6 7 8 9 |
You could do
1 | ball.spin(); |
1. Notice however though, that the class no longer extends MovieClip. That is because MovieClips have to be instantiated. And Static Classes cannot be instantiated.
Creating Static Classes is kind of like creating a Top Level Class(that can be accessed in the same package. Don’t worry if you don’t understand this, you will later).
2. The declaring of variables has changed. You have to add a static before var. If you do not, you cannot access the variable you declared.
3. Again, like declaring variables, declaring functions has to have a static before function.
Okay now there is the task of packages(levels). For the sake of neatness, there are packages. Say you have a button, a checkbox, and a scrollbar. Sure you could have them be separately in the main package. But then if you want them to be organized, you could create a class called UI, and have the button, checkbox, and scrollbar class be subclassing the UI class. So then you would have this as your checkbox class:
1 2 3 4 5 6 7 8 9 |
1. Everything is the same as usual, except you have the first line different. package UI. The reason for this is obvious. You are having the checkbox class subclass the UI class. Remember how I said you have to save the AS file as the class name? Well when you have a class in a different package, you have to change the directory(location) of the file to reflect the package. So what you have to do is create a folder in the same location as your FLA and rename it to UI. Then you can put your checkbox, button, and scrollbar class in it. And whenever you need to use it, All you do is import it.
1 2 3 4 5 6 7 8 9 10 11 |
Simple and organized.
If you want to learn how importing works, and why to do it, then continue.
Okay so importing allows you to use the class you’re importing. You might think this is a painful process, having to import everything you need, but it adds to the efficiency of ActionScript 3.0.
The star operand(*) is a wild card. If you do import flash.display.*, it imports everything in flash.display. I do not recommend this though. It’s better and efficient to import only what you need. But you should know that it’s there in case you ever need it.
In classes there is the private/public attribute. You must have noticed by now how variables/functions have public or private in front of them. Well what they do is explained by the word. It makes the var/func public/private. Say you have a variable in a class that only the class itself would use.
Like in a class called char we have a boolean variable(true/false) called dead, and an int(number, integer) variable called hp. Now the char class would use hp right? But say you have enemies and the enemies need to know if the character is dead. So they would need to access the dead property. So dead needs to be public and hp needs to be private. Dead needs to be public because enemy is a different class, so it can’t access char’s private variables. And hp is private because only the character needs to know that.
The above applies to functions too.
I hope you enjoyed this tutorial, and if you have any questions just leave them below.
Ping Class
May 25, 2009
I created this class for my game, Neo Tokyo Bash.
Enjoy.
Download the rar file and put the ping folder in your FLA folder.
Click here to download
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import ping.* var test:ping = new ping('http://www.google.com/intl/en/about.html'); test.addEventListener(PingEvent.PINGED, onSuccess); test.addEventListener(PingEvent.ERRORED, onError); function onSuccess(e:Event):void{ trace('it worked'); } function onError(e:Event):void{ trace("site doesn't exist"); } |
When you create a new ping object, you have to give it what URL you want to test. If you want to check if a site exists, you have to put the URL of an HTML file or some sort of file on their site(Like I used intl/en/about.html on google.com).
Here is the code of the files:
ping.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package ping{ import flash.events.Event import flash.events.EventDispatcher import flash.events.IOErrorEvent import flash.net.URLLoader import flash.net.URLRequest public class ping extends EventDispatcher{ public function ping(url:String):void{ var loader:URLLoader = new URLLoader(); try{ loader.load(new URLRequest(url)); }catch(e:Error){} loader.addEventListener(Event.COMPLETE, onComplete); loader.addEventListener(IOErrorEvent.IO_ERROR, onError); } private function onError(e:IOErrorEvent):void{ this.dispatchEvent(new PingEvent(PingEvent.ERRORED)); } private function onComplete(e:Event):void{ this.dispatchEvent(new PingEvent(PingEvent.PINGED)); } } } |
PingEvent.as
1 2 3 4 5 6 7 8 9 10 11 12 13 |
NTB Update(5/24)
May 24, 2009
Okay well it’s been a while since the last update. Don’t worry though. I haven’t quit on the game. I’m going to finish this :).
And on the subject of finishing it, I have finished the trainer. All hacks work:

updateAfterEvent()
May 24, 2009
First of all, what is updateAfterEvent?
Well it’s a function that updates(redraws) the whole stage and every object in it.
You might think that the function updateAfterEvent has been removed in ActionScript 3.0. This is not the case, it has just been moved to a different Class.
If you try using it, you get this error:
Warning: 1060: Migration issue: The method updateAfterEvent is no longer supported. This function is no longer a global function, but is still available as a method of the TimerEvent, MouseEvent, and KeyboardEvent classes..
If you are not good at reading errors, then you might think that you can’t use this. But you can! If you take a look at the error(more like a warning, but meh), you can see that it says something along the lines of, “You can’t use this the way you’re using it. But you CAN use it if you call it from TimerEvent, MouseEvent, or KeyboardEvent.
Adobe did this because you have no need to call this function unless something happens that’s outside of the ENTER_FRAME(onEnterFrame). There are 3 main classes where that could happen. TimerEvent, MouseEvent, and KeyboardEvent. There is also LocalConnection, Sockets, etc, but there is a way to call updateAfterEvent in those too. I’ll show you how.
First of all updateAfterEvent is simple to call. Say you have a function for a custom cursor. It will look choppy if your FPS is low. So you have to update the stage as soon as the cursor moves. Your code would be something like this if you used updateAfterEvent.
1 2 3 4 |
To see actual mouse cursor code, see my custom mouse tutorial.
Simple as that.
But what if you have to use it outside of an event listener?
Simple again. One line of code is needed.
1 |
Simple, right? It is.
