ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Three useful classes for game developement
http://www.actionscript.org/resources/articles/1112/1/Three-useful-classes-for-game-developement/Page1.html
Jean André Mas
Graphic designer converted since 2004 to coding. I play around with C++, OpenGL, Java, Javascript, AND Actionscript.
My website: ASWC 
By Jean André Mas
Published on May 14, 2012
 

I present in this article 3 classes that I use regularly in my game development process. Those classes can be used for free in any project.


First Class: the DebugConsole class

I've been developing social games for major U.S. companies over the passed years and collected more than 500 reusable classes from those projects. Some of those classes are used on every projects and I'd like to share in this article three of those that you can of course use for free in your projects. I'll explain how to use them and also describe why they are so helpful in my developement flow. Those classes can be used with Flash or Flex. Because those 3 classes provide a non intrusive and unique functionality, they are treated as Singleton (I make this distinction because writting singleton class is more of an exception for me than a rule).

Debug Console:

An automated debug console which allows you to run debug code, trace results, run cheat codes, etc.

Main Faetures:

  1. Automated setup. You can set the console with only one line of code.
  2. Activated by pressing "`".
  3. Run commands and return results.
  4. Built-in commands, clear, dump, exit, help.
  5. 3 working modes: open, password protected, desactivated.
  6. Auto open output window if specified.
  7. Command hinting as you type.
  8. Parameter help and description.
  9. Register and unregister commands on the fly.

Let's try the console in this little movie. Press "`" to activate the console. Then type "showMovies" to show the hidden movies or type "changeSpeed" with an int parameter (add a space between command and parameters and in between parameters) to change the speed value. The 'changeSpeed' action is registered with one required parameter. If you do not add a valid parameter the console will show you the error. You can use as well the command "hideMovie" with is set with 3 optional parameters. If no parameters are passed all movies show up but if you set any parameter to zero the corresponding movie will be set invisible. These commands are called "action" and can be registered with the console at anytime and removed at anytime too. You can also type "help" to get a list of available commands and required parameters if any. Type exit to close the console or press "`" again.

The code used to set this console was very simple:

[as]import ASWC.components.DebugConsole;
import ASWC.components.ConsoleAction;
addChild(DebugConsole.getInstance());
var speedaction:ConsoleAction = new ConsoleAction('changeSpeed', changeSpeed, 'Change current speed', 1, 0, ['int']);
speedaction.openoutputs = true;
//open outputs automaticallyDebugConsole.getInstance().register(speedaction);
//where speedaction is a function/method to run
//note that if that function/method return a string it
//will be shown in console.
var movieactions:ConsoleAction = new ConsoleAction('hideMovie', hideMovie, 'hide movie', 0, 3, null, ['int', 'int', 'int']);
DebugConsole.getInstance().register(movieactions);[/as]

In this example the console was completely open, meaning you could use it
freely. Perfect when your game is still in developement. If you are in QA phase
with an already live game you might want to password protect the console. You do
this by passing a password in the console:


[as]addChild(DebugConsole.getInstance('my password'));[/as]

In production version you disable completely the console by calling:


[as]DebugConsole.getInstance().desactivate();[/as]

The reason I use this console on all my game projects is because I can set
easily test cases during the developement process and I can also set easily
cheat codes during the QA process. Whether I test locally or in live version I
can always access my console and run all the test I want and check the results.
It saves me a lot of developement time, the QA people can request whatever cheat
code they want since it's very easy to set new console actions I'm not really
wasting time doing it. Also even if the game goes live I can still activate the
console if a bug was overlloked and I need to run additional tests. That class
is for me a time saver but also a tool that aloows me to communicate better
with other people involved in the project.


Tracer class

The Tracer class is plugged and included with the DebugConsole class but you can use it on its own. It is a 'super' trace class. Now why would I need another type of trace class? For two main reason:

  1. The trace class provide only one functionality but I would like to have more.
  2. When working with multiple developer on the same project you could end up with so many traces in the output window that you'll need to spend time finding yours.

For complex developement and/or team developement it is recommended to use and create a Logger type of class as in the Flex framework for example. I have done that in the past too but I always wanted something more universal and simple. That is why I created this class and I use it now in all my projects. Main features are:

  1. Output all trace to output window or not.
  2. Keeps all traces in memory for later retreival. (all traces can be sent to a log.txt for example at any time)
  3. Can trace an object like trace does.
  4. Can trace all property of any object (even dynamic properties)
  5. Can trace by category. (you could set it to trace only your traces and not your colleague traces)
  6. Trace by object (you can set a trace scope)

Use the DebugConsole in this example and type "dump" to show all the traces:

[as]Tracer.useOutput = false; Tracer.show(speed_txt.text, "speed", this); Tracer.reveal(mc1_mc);[/as]

Note how using Tracer.reveal() shows all property of the object including dynamic ones. You could of course use this on an array, object, dictionary and all contained object will be revealed.

I use this on all my projects including projects involving many developers. All traces are kept organized and I can isolate mine easily from the other developers. I can easily set a log system and output everything to my log at anytime. The reveal() method is truly the nice additional feature that allows me to look globally at an object state instead of just tracing one value. This is also very useful when working with JSON and other object based data which is often the case when your application interacts with Javascript APIs.


The Soundmanager class

This class was especially made for games but can be used for any type of application really. It manages and plays sounds externally loaded or stored in libraries (Flex/Flash). It really provides the basic needs of sound playing in games and still allows you to include your own code for specific functionality if needed. Implementing hundreds of sounds in a game including sound tracks, music tracks, sound effects takes me about one hour for a very complex game. It doesn't get more efficient than that. Main Features:

  1. Load external sound on need basis to save bandwidth. (can be overwriten).
  2. Play, playAt, FadeIn return a SoundChannel for you to use if needed.
  3. PauseAll, stopAll, muteAll, unmuteAll, resumeAll, pause, resume, mute, unmute individual sound as well.
  4. FadeIn, fadeOut.
  5. Autoloop of sounds.
  6. no overlapping of sounds (for specific sounds)
  7. random sound playing (useful for sound effects)
  8. auto setting of volume (can be overwriten anytime)
  9. 2 master volume.
  10. set volume on the fly for specific sound.
  11. Handles automatically 3 type of sound: SOUND a sound effect, usually short duration, can be played multiple times and overlap itself, cannot be paused, resumed, muted.
  12. Handles automatically 3 type of sound: SOUNDTRACK a long duration sound, can be paused, resumed, stopped, muted, cannot overlap itself.
  13. Handles automatically 3 type of sound: LOOPING_SOUNDTRACK same as SOUNDTRACK but looping undefinitely until a call to stop().

Try this little example which includes the 3 type of sound the SoundManager can manage, for the purpose of this example the SOUNDTRACK playing stops the LOOPING_SOUNDTRACK and vice versa:

Some relevant code. Registering sounds and playing them:

[as]import ASWC.media.SoundManager;

SoundManager.getInstance().addClass('effect', Effect1, SoundManager.SOUND, 0.4);
SoundManager.getInstance().addClass('effect', Effect2, SoundManager.SOUND, 0.4);
SoundManager.getInstance().addClass('effect', Effect3, SoundManager.SOUND, 0.4);
SoundManager.getInstance().addClass('track', MasterTrack, SoundManager.SOUNDTRACK, 0.4);
SoundManager.getInstance().addClass('soundtrack', Looping, SoundManager.LOOPING_SOUNDTRACK, 0.4);
//external sound are used this way: SoundManager.getInstance().addSound('effect', 'link to sound', SOUND, 0.4, false, mysoundloadingcontext);

function playLoop(e:MouseEvent):void
{
    SoundManager.getInstance().stop('track');
    SoundManager.getInstance().play('soundtrack');
}

function playEffect(e:MouseEvent):void
{
    SoundManager.getInstance().play('effect');
    //3 sounds are registered with same name so a random one is choosen at play time.
}

function playTrack(e:MouseEvent):void
{
    SoundManager.getInstance().stop('soundtrack');
    SoundManager.getInstance().play('track');
}[/as]

It's quite obvious why this class is saving me so much time. You can see that that class uses string names to reference sounds. Those names can easily be setup as static const somewhere (even coming from external data) and played directly that way. A really simple example would be for a simple MouseEvent.CLICK where MouseEvent.CLICK is used as a sound name directly and all that is left to do in the MouseEvent listener is to use SoundManager.getInstance().play(e.type);. note that the SoundManager does not throw error if you try to play a sound that does not exist. There is also two master volume that you can use. One linked to the SOUND type and one linked to SOUNDTRACK and LOOPING_SOUNDTRACK. Those master volume affect the sound volume as it was set. Let's say you have two sound volumes set respectively at 0.4 and 0.8. When the master volume is at 0 both sound volume are at 0, when the master volume is at 1 both sound volume are at 0.4 and 0.8. If you have a mute button in your game you could mute all sounds easily by setting the two master volume to 0 or use directly the pauseAll() method. Check the docs for more in depth covering of that useful class.

That's it for this little article showing 3 useful classes that save me a lot of time on a daily basis and maybe they will save you some time as well!