Broken Button

Sunday, 21 September 2008

Return of the Key Class to Actionscript 3.0

This started out merely as a convenient class for handling keystrokes, and checking to see whether a particular key was being held down, mainly for arcade-type games.

As I started rewriting it for public release I wondered if I could emulate
as closely as possible the functionality of the old AS1/AS2 Key Class I've spent the past seven years using. I had a play with some static methods and properties, and came up with this

You set it up like this:

import Key;

Key.listen(stage);


Now place it in your game loop and use it the same way you did before:


if (Key.isDown(Key.LEFT)){trace("Left cursor key is being pressed")}


Hit me up in the comments if you have any problems with it.




Labels: , , , , , ,

Tuesday, 16 September 2008

ActionScript 3.0 Gotchas

I'm loving AS3. I have to make that clear right now. For two main reasons - Execution speed and code consistency. Adobe really bit the bullet and made necessary changes to the language to make it self-congruent and able to stand against other object-oriented languages.

Now to moan a bit... I've just finished my first commercial web game in AS3 and encountered several problems with the language that I'd encountered in other applications, plus a few new ones (games tend to push the platform more than other applications). So for those few that haven't made the transition from AS2 to AS3, here's a heads-up:

1. XML.send(); - Has gone. No more sending raw XML data to a URL with one line of code. Nor it seems can you do it with several unless you're using flex and can make use of the HTTPService Class. If anyone knows otherwise, please comment.

Workarounds - So far I've been using sendToURL() which forces your data into name-value pairs and requires that you send the XML as one of these variables.

2. Key behaviour - You can no longer query the keyboard directly and find out if a key is being held down. This is a pain if you write games. Instead you can only listen for key events.

Workarounds - I've written a class that listens for events and updates a set of Booleans so that it can be queried in much the same way as the old Key.isDown method. At the moment it only works with the cursor keys. I'm going to improve it during the current project and post it here.

UPDATE - here it is

3. for..in loops no longer work with MovieClips.
Here's the situation we were in: The designer wanted to be able to take a MovieClip and place thousands of sub MovieClips at registration positions, which would then be used to create an array of co-ordinates for corresponding objects. Another caveat was that he didn't want to have to individually name each instance (obviously). The behaviour of for..in has been reduced so that class defined properties are no longer enumerable and are thus hidden. This seems to include existing stage objects.

Workaround 1. You can use describeType() to pull an XML object representing the structure of the MC. But this only works with named instances. Doh!

Workaround 2. Do all of this in a separate AS2 fla, and output the values via trace and copy-paste them. Ugly, but it worked.

4. The native tween class has been removed. - UPDATE - No it hasn't, I just couldn't find it first time I looked. It now seems to be documented too.

5. and/or have finally been removed - Ok, so I forced myself to get into the habit of using "&&" and "||" when I found out that they'd been depreciated, but I think it's revealing a lot of conceit amongst those who write the specs on these languages that they've replaced two easy to read english conjunctions with these symbols.

Workarounds - Well if you can't beat 'em, join 'em. To celebrate the removal of these two operators, from now on I'm going to use the conditional (?:) operator in place of "if..else" wherever I can. Hah!

6. Code bloat. - I'm starting to wonder how many times a line of AS2 is going to have to be replaced with either two lines of AS3 or an entire custom class. Some code bloat is a necessary evil when it comes to good coding practice, but then nice compact code is a great practice in itself.

Workarounds - I don't know...quit coding and go and live on a technology-free commune?

7. Preloading. - AS3 has introduced a whole new load of issues with preloading, the main one being that your document class has to be exported on the first frame.

Workarounds - Whilst it's possible to have a movie preload itself effectively, I find it best to have a separate swf preloader.
There's a good tutorial on this here. This bypasses a few issues, and gives you more options like deploying your main swf on its own when a preloader isn't needed, or even having your preloader manage multiple files if necessary.

8. Bitmap scaling bug - I'll write more on this in another post, but it seems that if the document class pulls a bitmap into the display list, and there's an MC above it with certain filter effects, the scaling doesn't work (the bitmap scales, but the data inside it doesn't)

Workarounds - Either bind your baseclass to a movieclip instead, or avoid using filters in other display objects.


Hope some of this helps somebody.

More to come on the bitmap scaling bug, the game, and the enormous fun you can have with conditional operators in later posts.

Labels: , ,

First Post

It's about time I started a blog.

I'll try and keep posts regular, informative, entertaining and relevant to my work.

This post isn't a very good example.