Wednesday, January 21, 2015

Main.as or Document Class (AS3) Part 1

Something that I've found to be vitally important in writing ActionScript 3 is accessing the Document Class from other classes and packages.  And the beauty is that once you make your Main.as you will need only a bit of time to adapt it for your other projects.

Keep in mind that my Main.as is constantly changing, mostly because I keep thinking of stuff to add that I know I'll always need.

Here's a bit of code to start us off:

public class Main extends Sprite 
{
 public static var instance:Object;
 public function Main() 
 {
  addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
 }
 public function init(e:Event = null) : void
 {
  removeEventListener(Event.ADDED_TO_STAGE, init);
   
  instance = this;
 }
}

So what does this mean and do?

My understanding is that this is called a Simpleton (after re-reading I realized it's Singleton, but I left that in because it's funny). But whether or not that's correct, what this does is allows us access to the Main.as at all times from anywhere in the program.  Of course with what we have written so far, this doesn't mean much, so let's add some important info that would need to be accessed in most, if not all, Classes:


 public const SW:int = 512;
 public const SH:int = 288;
  
 public const SWH:int = SW / 2;
 public const SHH:int = SH / 2;
  
 public var TS:int = 16;
 public var TSH:Number = TS / 2;


In this section we have the stage width (SW) stage height (SH) and half of both of those (SWH/SHH). These are the dimensions of the screen and it makes it a lot easier to access than typing in:

        stage.stageWidth

We also have in there the tile size (TS) and half of the tile size (TSH). Because TS is set to 16 and our stage dimensions are 512 x 288 we have a 16:9 screen ratio to work in.

Do you understand yet why this Main.as is important?  Here's an example:

I have a plumber who has to check collisions with the environment.  In order to do so I need to find out if he's going to collide with a Block or if he's just falling through air.  In order to find out if he's hitting said Block I need to know where the nearest Block is.  Since the map has already been laid out (in this example it has, we'll get to that for real later) we know that all tiles are lined up by the TS that we have established in our Main.as.  So from the plumber's Class we can access Main.instance.TS to find out if the plumber bashes his head on a Block or just goes through air.

If this makes sense to you, you might be wondering why you don't just put in "16" instead of calling out to Main.as.  If it's a simple program, you're right, there's probably not much reason to do this.  But let's say you wanted one level where everything was giant except the plumber.  You could then easily change Main.TS to 64.  You wouldn't have to go back in to the plumber Class or the enemy Class or whatever other class needs to know the tile size to change it all.  You have to type in one number on one Class to change the way the plumber will interact with the level.  That's pretty cool, right?

Well, that's it for now.  I hope you enjoyed this little insight into what I'm learning and doing.  Next time I'll talk some more about the Document Class and how setting up the keyboard controls in Main.as makes life much easier down the road.

Here's a picture to go along with all these boring words:

http://www.gigs4gags.com/funny-programming/

No comments:

Post a Comment