CraigSimpson.net

Flash & Flex Development

WTF? Firefox!

You won’t hear me often say this, but what is Mozilla’s deal? The last couple of days I’ve had to run multiple updates a few days straight? Have they done proper testing before releasing? This is starting to get on my nerves. My thought is let the first developer/ dev team / open source project that has never done that throw the first stone. End of Rant. Don’t worry I still love you firefox!

Another use for ‘this’

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!

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A 
{
	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
		//myCoolFunction.
	}
 
	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.

This is a good time to use ‘this’

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:

?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
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’?

What makes you a good Developer? Part 1.

Ya, coding conventions!

(for the OCD developer)

  1. Follow coding conventions in your projects. That way you write clear and concise code. The problem is there are many conventions. In reality “conventions” is really a code word for someones arbitrary opinion on how to write neat code. So…which one do you follow? It doesn’t really matter; just follow one. Adobe recently lays one out and I can agree with most of it.  It is not complete and just because it is by Adobe does not mean it is the end all be all.

    While reading the document, I couldn’t help but feel like I was being scolded by my parents. Take a look:

    Do this:

    public function set label(value:String):void

    Not this:

    public function set label(lab:String):void
  2. Stick to those conventions!  I have to admit it is hard but practice it all the time.  Remember this is not the fun part of Flash & Flex development and it never will be but make it a habit.
  3. Lastly, you will eventually run into someone who doesn’t agree with your conventions.  In that situation just think about my first point.

Now time for me to go back to my old code and see what mistakes I’ve made.

Listening to Music during Development?

What do you listen to when you doing work or more importantly developing? For me it would be lounge music, great stuff!

Interactive Forest

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.

Future:

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.

Download Source [300K]

Browse my Classes

Licensed under Creative Commons, so feel free to use them in your project and if you do please let me know.

Cartoons made in Flash

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.




utility.Debugger

Screen Shot of Debugger

Description:

A Flex application that I developed to print trace messages to a custom debugger panel.  I use it in all my projects since trace does not work on applications running out side the Flash’s IDE.  Some of the features include 3 message levels so that you can choose not to get flooded with debugging messages.  Secondly you can also highlight messages. This works great for error messages and looking for those hard to find variables.    Even if your compiling in Flash’s IDE, Debugger still sends out your message as trace. This is a life saver in time and debugging when your clients call say the live app is not working. This app is a must for any Flex or Flash project.

[ View  debugger panel ]

Download:

[ Debugger.zip 281 K ] The source files for both the output panel and the static class that you place into your project to send messages to the panel. Instructions on using debugger panel is in the class file.

?Download Debugger.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 Copyright(c) 2008-09 Craig Simpson, Some rights reserved.
 Your reuse is governed by the Creative Commons Attribution 3.0 United States License
*/
 package net.craigsimpson.utility{
 
	import flash.net.LocalConnection;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
 
	/*
	 	Debugger Singleton Class:
 
		Developed to be used in AS3 Flash & Flex applications. The class sends debug meessages 
		to the custom built debugger panel. Kind of like looking trace commands in a browser. 
		Great for both development and when application is live on the web.
 
			//Example actionscript to send messages to plane;
			var debugger:Debugger = Debugger.instance;
			debugger.connect("MyCoolApp");
			debugger.send("Test 1...2....3");
 
		Debugger Panel Application:
 
		The custom debugging panel source can be downloaded at www.craigsimpson.net/download/Debugger.zip
		Also a live version is hosted on my site at www.craigsimpson.net/debugger.html
 
		Contact:
 
		Report any bugs, comments, or suggestions at www.craigsimpson.net/contact
	 */
 
	dynamic public class Debugger
	{
		private const CONNECTION_NAME:String = "_debugger";
 
		private var debuggerLC:LocalConnection;
		private var _appName:String;
		private var _domain:String;
 
        private static var __instance:Debugger;
 
		public function Debugger(enforcer:SingletonEnforcer)
		{
			//Constructor
		}
 
		public static function get instance ():Debugger
		{
			if(Debugger.__instance == null) {
				Debugger.__instance = new Debugger(new SingletonEnforcer());
			}
			return Debugger.__instance;
		}
 
		/*
			Send messages to output in the debugger window
		*/
		public function send(message:Object, level:Number = 0, hightlight:Boolean = false):void
		{
			debuggerLC.send( CONNECTION_NAME, "outputText", message, level, hightlight );
			trace(message);
		}
 
		/*
			Connects application to debugger panel, must be called once in your application.  Since multiple apps are allowed to connected to the debugger
			panel applicationName helps indenifty which apps are currently connected.
		*/
		public function connect(applicationName:String = null, domain:String = null):void
		{
			_appName = applicationName ? applicationName : "Unknown Application";
			_domain = domain ? domain : "*";
 
			debuggerLC = new LocalConnection();
			debuggerLC.allowDomain(_domain);
			debuggerLC.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onConnectionError);
 
			send( _appName + " Connected ", 1, false );
		}
 
		/*
			Sends a message that is highlighted in red, for sending error messages that need to catch the endusers atttention.
		*/
		public function sendError(errorMessage:Object):void
		{
			send( errorMessage, 2, true );
		}
 
		/*
			Sends highlighted message to panel.
		*/
		public function sendHighlighted(message:Object):void
		{
			send(message, 2, true);
		}
 
		/*
		 *  Event Listeners
		 */
		private function onConnectionError(event:SecurityErrorEvent):void 
		{
			debuggerLC.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onConnectionError);
			debuggerLC = null;
 
			trace("LOCAL CONNECTION SECURITY ERROR!");
			throw new Error("onConnectionError: "+event.toString);
		}
	}
}
 
internal class SingletonEnforcer {}


Future:

Documentation write up for implementation of Debugger class and using the panel. Any suggestions for future releases are greatly appreciated.

animation.Float

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.


New Site 3.0.0!

For the longest time I have been running a cooking blog and my professional portfolio on two diiferent websites [ www.vestedfox.com & resume.craigsimpson.net ].  With my busy shecdule it was becoming harder to keeping up both.  So I’ve decided to combine the two.  I am currently developing a FLEX cook book application that will hold all of my recipes, so keep a look out in my recipe section for the app and new recipes.