contact

Posts Tagged ‘package’

migrating from Actionscript 2 to Actionscript 3

Thursday, January 31st, 2008

 I just recently bought the book Essential Actionscript 3  by Collin Mook. I havent gotten really far into the book yet but there are few things that one must understand in order to get even started with actionscript 3. The first thing is AS 3 is pretty much OOP only, which means for you to forget about timeline code which i personally used very often especially for small projects. So where do I put my code now, was the first thing I asked my self. The answer to this question is the document class. When using the flash authoring tool you find this setting in the document preferences.

 

so none of your code will go to the timeline. You will need to follow a few simple steps:

  • create an actionscript file, save it to the same folder where your fla file is at
  • inside this file you have to create a package that will hold the document class
  • inside the package create the document class
  • place the code you would have originally put into the first frame of the timeline in the constructor function of the document class the following code:
  • then place the name of your document class in the designated field in the document preferences as shown above
  • you may use this sample code to start from:
package{
	import flash.display.Sprite //
	public class yourClass extends Sprite{
		public function yourClass(){
		// your first timeline frame code goes here
		}
	}
}

Now this is a very simplified approach to this topic, there is a lot to know about the naming of packages, classes and the scope of objects and methods within them. I highly recommend the book i mentioned above. just note that the document class must either extend the sprite or the movieclip class, and you have to import every class you want to use. In general Actionscript 3 is a lot more strict but thereby a lot more effective. It forces good OOP programing, and won’t let you get by with several hundred lines of code in you first frame.