Workaround for AS2’s exclude.xml in AS3

May 9th, 2008

When creating external libraries, you often want the contents to have have a base class from your application. When you do this however, the whole application will often be compiled into your library due to its imports. This means two issues arise:

1/ You have a whole lot of duplicate code comipiled into your libraries, which will not be used (as when the lib is loaded any classes of the same name will not override the existing ones)

2/ It takes a bloody long time to compile your library (when it should be fast)

Solution:
The best way we have found to get around this is to create a empty classes, which work similar to intrinsics (but are totally empty -no functions, vars or imports), to replicate the base class.

Eg.
If in your application you have a class for example: ’sprites.enemies.Monster’
In your lib you will want to create an asset with identifier: ‘Lib_Monster’ and base class ’sprites.enemies.Monster’. Normally this would compile in the full Monster class and all its dependencies.

If you set up your lib.swf in a seperate folder to the application then create another class, lets call it a placeholder class, ’sprites.enemies.Monster’ and just have it empty eg:

package sprites.enemies{
import flash.display.MovieClip;
public class Monster extends MovieClip{
public function Monster{}
}
}

When it compiles the Lib_Monster will have the correct baseclass, but it will not include any code.

The trick is that, when the lib is loaded into your application the class ’sprites.enemies.Monster’ will conflict. The application should have its own compiled version (the one with functionality) and when the lib loads in, it will ditch its version (the empty one) and adopt the existing (correct) version.

It only takes a few seconds to create the placeholder class for the library reference.

If you want to have the lib.fla in the same folder as the application, then you can even set up a different classpath for the lib. To do this go tot the publish settings and add a new classpath (eg. ./intrinsics). This path will be checked first for your placeholder class.

If for some crazzzy reason your classes arn’t used (and so compiled) in the application (if you reference them with weak references), you will need to force them to be compiled in like this.

Forcing classes to compile in AS3

May 9th, 2008

when you use the "import package.Class; " or "import package.*; " in AS3 the class(s) is not forced to be imported. A class is only imported if you use it somewhere in your code.

The simplest way is to just type its name:


Actionscript:
  1. import sprites.Ball
  2.  
  3. public function init(){
  4.    Ball;
  5.    // Or for many at once
  6.    (Ball,Dirt,Kid,People);
  7. }

Just simply typing the class type somwhere in your application will force it to compile in. Why is this of any use you may ask?

We use it for level building in games, where you have a generic component as a placeholder used in flash to lay out content/levels. Your Ball class for instance, might then simply build itself using externally loaded assets, and attach itself to the stage. It may not need to be referenced anywhere in the application, but is still compiled in.

AS3 Frame Based setInterval and setTimeout

May 5th, 2008

Sometimes you want to be able to use a timeout or interval but, as they are tied to milliseconds, if the frame-rate of your movie goes down from what you expected - games is a good example - then you can run into trouble.

I recently had this problem with a game, that when the frame rate lowered, cyclic "emitters" (ie creating bubbles underwater) would not be synced with the expeted speed of the game.

The solution. A setInterval and setTimeout that you can set using milliseconds (or frames if you want) and will be called at the expected number of frames. The only caviet with this approach is that it needs to be inited with the stage object, both so it can get an ENTER_FRAME event and the current FPS.

Read the rest of this entry »

Accessing drawing items & stage content in as3

April 25th, 2008

To my surprise i found the other day that, with the new AVM2, you can now access the content that you have drawn to the stage. It makes perfect sense, but i just assumed that it was locked. As a test, if you can draw something on the stage and then add the code

Actionscript:
  1. getChildAt(0).x = 100

This is reallly handy if you want to remove all the content from a clip, maybe move it into another, or add it dynamically to a child, then add effects. One note your drawing items are merged together (it doesnt preserve layers) unless there is a MovieClip between the layers. This is the only time that it splits layers into separate clips.

AS2 + AS3 Draw a Dotted Rounded Corner Box

April 25th, 2008

So you might think that rounded corners is easy in as3 with the new drawing tools. This is only so true if you don't want it dotted.

Thanks to senocular AS2 DashedLine.as which was easily converted to an as3 version DashedLine.as for as3 we can draw dotted lines and curves with actionscript. This left me with the task of using the quadratic drawing tools (no i didnt add a cubic curveTo to the Dashed line class) to draw circles (or quarters of circles, for the curners), which isnt the easiest, but i'll spare you the details and simply say, its done!

Using Draw.curvedBox(obj, offsetX, offsetY, width, height, cornerRadius) we can draw into any graphics object (whether it be a normal Sprite.graphics or a DashedLine Object). This is essential as the Dashed line tool doesnt actually give us a fill, so we might need to draw a normal shape for the background.

Actionscript:
  1. import com.senocular.drawing.DashedLine
  2. import com.tonp.utils.Draw
  3. var dl = new DashedLine(this,1,5);
  4. dl.lineStyle(3,0x000000,1);
  5. Draw.curvedBox(dl,10,10,100,100,20)

to draw a curved corner box with a fill, or solid outline, send in mc.graphics as the first attribute (or just the mc in as2)

Dashedline.as for as3

Draw.as for as3

Draw.as for as2

So we can now do a box with curved corners in either AS2 or AS3 and in dotted or normal... phew!

AS3 Bitmap Cached Animations

April 25th, 2008

Rendering a vector for every frame of a repeated animation (like in most games) take a lot of CPU. It takes even more CPU to have lots of complex characters on screen - many the same sprite.

cacheAsBitmap in flash is good, but only if you have static objects but recreates the bitmap data of an animation every frame, making it run even slower. The best solution is to cache each frame of an animation as a bitmapData, and then switch between them each frame. This works well until you have multiple instances of the same sprite - You dont want to pre-render the bitmap data and keeping it in memory many times, so it is best to keep a library of animations, and then reference the cached frames for each instance.

View Example

Download Source

Another advantage of this technique is it seems to make use of Flash's new multi-core support. On a dual core CPU it can, if you push it, take almost 100% of resources. Trying it on a quad-core seemed only to take up two CPUs though. I guess this is both a blessing and a curse as you're more likely to grind a users system to a halt, even if they have multiple CPUs.

Animations copyright Nathan J. - http://www.scarygirl.com/

Joystick / Joypad in Flash

April 14th, 2008

I've always wanted to use a Joytick with my Flash games.

A while ago I was working on small game for Lorial - in-store advertising for Biotherm - through a great company Eness. It was a simple "catch the falling objects with a timelimit game" made in Flash, but needeed to be controlled using a joytick as it had to be simple and easy to use for anyone walking through Myer.

After a little research I found the amazing program AutoHotKey. This lets you specify macros to do things like using a joystick as a mouse or keys, or pressing a combination of keys and starting a program. Using this amazing peice of software (Windows only sorry) and hacking around with some of their joystick input exampels it was easy to map joystick movements and buttons to the arrow keys and "A" on the keyboard, which were then read by Flash.

On a recent game I've been working on (Scarygirl) I've found that user testing on the keyboard really gets in the way of the gameplay itself, especially for those not used to games. It was a lot easier to hand them a Playstation 2 DualShock controlller attached via a USB converter and have an AutoHotKey script running that converted the D-Pad and buttons to input. The hardest part of this is finding which button on the controller relates to what input into windows. The easiest way I found to work with this was Start->Control Panel->Controllers and just writing down a list of buttons as I pressed them.

So yeah. Easy. And fun. Some games are just so much better with a good controller - just never let me catch you playing an FPS on a console!

JSFL: Select all classes of the selected type on layer

April 14th, 2008

I've been playing with JSFL a little recently. It was really hard to begin finding any resources on how to use it and those I did find were pretty sparse with information. So to help the community out I'm going to start posting my experiments, some useful, some only good for me, for everyone to take a look at, take apart and generally learn from.

The first one is one of the simplest. It lets you select all Elements of the same class on the stage. Simply select one of them, say a RadioButton, then choose the TMP - Select Same Class from the Commands menu and all other RadioButton instances should be selected.

Read the rest of this entry »

Mame with USB Controllers

April 6th, 2008

We've finally nearly got our 40gb of MAME running- productivity boost! It took a while to figure out, but we finally have PS2 controllers to work - conected with a USB adapter. To run MAME with USB controllers on XP i've found:

  1. connect controllers & test that they work in the Control Panel > Game Controllers Panel.
  2. get MAME to create a .ini file
    You need to run 'mame -cc' this can be done at the command prompt, or by making a .bat file
  3. Edit the 'mame.ini' file (should be created next to your mame.exe)
    Change the line 'joystick 0' to 'joystick 1' - this enables joystick input
  4. Press tab when in MAME to get the key setup options.
  5. Rock out!!