Category Archives: AS3

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’?

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.

[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.

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.