So just as I was looking over the lase post about ‘this’. I realized that I skipped one of the best uses for the infamous property of ‘this’. Enough talk, more actionscript (sorry terrible pun).
So imagine this. Lets say that we have class A which creates an instance of class B. Now lets say when want to store an instance of class A in class B so that you can call class A methods. For smaller apps this is can be an acceptable code structure. So how do you pass an instance of a class in that same class? You guessed it. this!
private var b:B;
public function A()
b = new B(); //Creates instance of B class
b.a = this; //this passes a reference of class A
//to class B. Now class B can call class A's
public function myCoolFunction():Void
//Magic happens here. :)
If you have any other handy uses for this (I’m sure there are), please let me know in the comments.
I know for a lot of actionscript coders out there, especially beginners, ‘this’ in can be rather confusing. This issue can spark some pretty heated debates about this proper use. So before diving in here is a short explanation of ‘this’. In actionscript the object the code is currently running in is referred to as ‘this’. So lets say a class has a property myVar, putting this.myVar tells actionscript to look in the current object for the property myVar. Why do we need this? This doesn’t make any sense! Well look at the code below:
public var age:Number;
public function myClass(age:Number)
if(age > 0)
this.age = age;
So lets say this is a class (myClass) with a public variable age. In actionscript you can declare local variables and parameters that only exist for the duration of the function. These local variables have the smallest scope, but have the highest preference, meaning if another variable is named the same the local variable, the local variable will be used instead of the class variable. In the above example we have a constructor the class myClass. Can you see were I am going with this? In the above constructor we have two age variables! The parameter is defined in the function. So to set the class variable we need to use ‘this’ to specify between the local variable and the class variable.
This example is the only the real time that you need to use ‘this’, of course this is only my opinion. What do you think? Coding vets how do you feel about ‘this’?
From my previous post, animation.Float, I had the basic example of the animation class that I wrote to simulate dandelion seeds floating in the wind. Well I had some free time this weekend and decided to keep my AS3 skills sharped and went on to add interactivity! Depending on the location of your mouse will vary on the speed of the wind blowing the seeds around. Farther away you go from the middle the faster the wind gets. The interactivity is also directional and when you click on one of the seeds they disappear.
The Float class was one of the very first classes that I wrote on my own. Pretty much after I got done reading a book about OOP in AS2, I started this class. Looking back now, it was pretty rough. To add in the interactivity and to port the class over to AS3, I pretty much gutted the whole thing and started new, but it is neat to see that I have come a long way in such a short time.
Now the seeds are in a new Dandelion class that extends Float. Float contains a wind object which is shared among the other instances of dandelion seeds (kind of like a singleton, not really fond of them personally, but that is a different post). After that is was simple to add in the required mouse detection class and update the wind velocity based on the mouse position.
There are still a few bugs left, if you are in FireFox switch to another tab and then come back. Also I want to enforce the wind velocities a little better in case the mouse position is too extreme. To enhance it I want to make it 3D by splitting the picture into layers and having the seeds go between the tree. The 3d would also have the smaller seeds be removed from the stage earlier to make it look like it landed in the background of the image.
[UPDATE]: Github Repository for the Interactive Forest
Licensed under Creative Commons, so feel free to use them in your project and if you do please let me know.
My first job in Flash was animating a children cartoon series. I had always drawn pictures and such as a child but freshly graduated I had no experience in animation. At first it was very frustrating to get any cartoons done, but I finally had a break through and this job became such a wonderful experience for me. I started with nothing and at the end of my two years with Kid Chess I had made 75 cartoon episodes of edutainment to teach kids (K-5th) how to play chess.
I would have never guessed the popularity of theses cartoons in the schools. To me I was just messing around try to make the knight’s lips sync with the audio. Despite my novice everyone wanted more, and so a got some great help from Josh Latta a great cartoonist to help me out on the videos.
And the weapon of choice for animating the cartoons….none other than Flash. At the time I didn’t know better but there is better animation software out there. I finally wised up and bought Preimer Pro and After Effects about a year into the job, but none the less it was and still is a great tool to create simple animations.
Below are some samples of my work. Head on over to KidChess.com to see more.
Part of my slowly growing animation package for AS2 and AS3. To browse my different classes and packages in Flash actionscript visit my classes download page or go to my downloads page to directly download the AS code.