ActionScript3 Category

  • BlitzMouse Released + Demo

    After working my butt-off on my latest game I decided it was time to redirect some love and attention to PixelBlitz. So this weekend I finished off a new class I had planned out a while ago. Introducing… BlitzMouse! This pairs up with BlitzKeyboard to complete the input set, and is a feature rich (and very fast) mouse handling system.

    Here’s a very simple demo of it in action:

    Press the cursor keys to limit custom mouse pointer movement.

    So what can this bad boy do? The aim (as with all things PixelBlitz) is to make working with the mouse in your games easier. Here’s a quick overview of the core features:

    Accurate mouse tracking

    Keep an eye on that rodent! Easily check if it has left the stage, or re-entered again. Doesn’t use any CPU time processing events that require the mouse if it’s not over the stage!

    Button / Click handling

    Detect when the mouse button is down, or up.. or being held down. Find out how long it took the user to make that last mouse click (the speed from down to up again). Find out how many clicks the user has made since you last checked. Writing those god-awful “Ninja Finger” style games has never been so simple 😉

    Mouse stats

    Want to know if the mouse is moving up? Just check isMovingUp! You can poll all 4 directions easily and quickly. Want to know how far in pixels the mouse has travelled on the X axis? Call distanceX() – want to know how fast it moved? Call speedX!

    Rotation

    To know which angle the mouse is at call angle(), and you can have the result back in degrees or radians. Use the optional “lowerAccuracy” parameter and it uses a much faster calculation, but sacrifice a little accuracy in the process (great for a quick arcade game, not-so for a physics simulation).

    You can set the point of the angle calculation (it defaults to the centre of your stage), which allows you to track the angle from anywhere to the mouse. You can even get the angle from any display object to the mouse using angleToObject() (with all the same parameters that angle() supports). This means if you want 3 things all pointing right at the mouse then it’s no problem! (see the demo above) Want to know the distance from any display object to the mouse? Call distanceToObject() and it’ll tell you.

    Custom Mouse Pointers

    Use changePointer() and you can set the mouse pointer to any display object – with custom X/Y offset support incase the pointer needs aligning differently to the standard top left. As the mouse moves, the display object is updated. If the mouse leaves the stage the display object is made invisible, so you don’t get that “cursor stuck on the edge” problem :)

    Movement Limits

    Want to limit the movement of the custom pointer? No problem! You can limit it in all four directions (up/down/left/right) with optional snapBack support on both axis. For example if you were making a Pong game (please… don’t) by just calling limitMovement(true, true, false, false) you will lock the bat into only moving up and down with left/right movements ignored.

    Want to limit the mouse to a specific area of the stage? Call limit(new Rectangle(x,y,w,h)) – if the mouse goes outside of this zone it will be hidden from view. You can see this in the example above as it hides when it leaves the light grey box. This limit also effects custom pointers. Of course when the mouse leaves the stage there is nothing you can do about that – but we have to work within the limits of Flash here :)

    So there we have it – BlitzMouse! Hopefully more useful than you thought when you first read the title, aye? :) I plan to add more features, such as custom events/function calls on mouse actions, the ability to attach as many display objects as you like to it (so they all update at once) and custom zones, so you can have as many “mouse zones” as you want. The code is uploaded to Google, so enjoy :)

  • PixelBlitz: BlitzFont is live :)

    Tonight I created and finished BlitzFont. BlitzFont is a bitmap font handling class. Most Flash games use True Type fonts for their text, or “bitmap True Type fonts”, where the font was specially designed to be used at a small resolution. This is all well and good, but what if you want to design a custom font? Something with some graphical flair that can’t be achieved via True Type. Enter BlitzFont :)

    BlitzFont allows you to use pass in any bitmap from your library, tell it how the font characters are arranged, and then it grabs it all for you. Once grabbed you can ask it for text back again, and it’ll return bitmapData for ever more!

    If this sounds a bit strange, take a look at this font here:

    In this example the characters have between drawn in a grid and they are 32×32 pixels in size. Using them in your game requires just one call:

    [as]
    font.init(new tbjFontBD(0, 0), 32, 32, BlitzFont.SET10 + ” 1234567890,.:’-<>!”, 9, 2, 2, 1, 1);
    [/as]

    All this does is give the name of the bitmap in your Flash library, the width and height of each character, the characters and the order in which they appear (SET10 is a built-in character sequence that you can extend), the 9 value is the number of characters per row, 2 + 2 is the distance (x and y) between each character and finally 1,1 are x/y offsets to start grabbing from.

    Phew sounds quite a lot, but if you’ve got a well arranged font with no spaces, no offset and a standard ASCII ordering, then you only need to use 5 parameters!

    Once you’ve initiated the font that’s it – you can use it. BlitzFont has 3 methods for this:

    getLine()
    getMultiLine()
    getCharacter()

    getLine is ideal if you only need to get back 1 lines worth of text.
    getMultiLine can be given as much text as you want (that will fit onto a bitmap) and supports carriage-returns.
    getCharacter simply returns 1 character.

    Each of the functions above all return bitmapData objects. You have alignment support, so you can left align, right align or center each line perfectly. You can also control the x/y spacing between characters!

    [as]
    var bd:BitmapData = font.getMultiLine(“welcome to thenpixelblitznblitzfont demo”, 1, 16, BlitzFont.ALIGN_CENTER);
    [/as]

    The class does all the dirty work for you, like removing un-supported characters from your input string, making sure it deals with spaces efficiently, and wrapping text on carriage returns.

    But wait, there’s more! You can also create scrolling text messages with 1 line of code :)

    [as]
    defineScroller(width:uint, speed:uint, text:String, autoUpperCase:Boolean = true, wrap:Boolean = true, spacing:uint = 0):BitmapData
    [/as]

    Ok I know, scrollers are old-hat now.. but sometimes in a game you want a little scrolling message, or status update or something, and with this you can easily create it :) You just define the scroll settings, and then call updateScroller() every loop. You can even set custom events to fire if a certain character scrolls on! and the scroller will fire events on text complete and text wrap too.

    Here’s a demo of this in action (refresh this blog entry if you didn’t get to see it from the start!)

    Again this is all now in Google code, and i’ll upload example source when I update the examples packages in the coming days. If you are in need of some great bitmap fonts, here’s a good resource.

  • PixelBlitz: BlitzMath is live

    Tonight I pushed up a new build of the BlitzMath utility class. As dull as this may sound it gives you a set of functions to perform math tasks that either AS3 is very slow at, or that simply don’t exist.

    For example there is an extremely fast new rand function for when you only need a small integer back. There are enhancements to the standard Math.random() calls to provide for min, max, int and Float support. There’s a faster abs(), a very fast sqrt replacement and a few other experimental things.

    There’s also a nifty little function I call “chanceRoll” which when give a value between 0 and 100 gives you a boolean back based on that weight. For example say you had a player hit a baddie in a game, you could give him a 30% chance of getting a critical hit by calling chanceRoll(30). I know, it’s simple, but it’s lots of simple little things that will make this framework great :)

    There’s also a blindingly fast simultaneous sine/cosine table generator. So fast in-fact you can generate the tables in real-time! The length, sin/cos amplitude and frequency are all under your control.

    This is all now in Google Code, and here’s a quick demo to play with:

  • PixelBlitz: New package, new domain

    Yesterday I completed the move to re-package PixelBlitz. Norm agreed, and we registered a new domain (pixelblitz.org) and I went through and updated the library to use the new package location.

    I think it makes it cleaner and easier to type :)

    The domain name will be used to point to a PixelBlitz web site, once we’ve got some time to actually build it – in the meantime I’ll redirect it to point to the Google Code site.

  • BlitzKeyboard is live!

    After a mammoth coding session tonight I have committed the first version of BlitzKeyboard into the PixelBlitz Engine.

    I’m really happy with this work – it’s pretty much everything I’ve ever wanted from an advanced keyboard handler :)

    So what can it do for you? In short it’s about saving you time. At the core is an extremely fast keyboard event handler, so all you need to do is ask it if a key is pressed or not:

    [as]
    if (keyboard.isDown(BlitzKeyboard.LEFT))
    {
    player.x -= playerXSpeed;
    }

    if (keyboard.isDown(BlitzKeyboard.RIGHT))
    {
    player.x += playerXSpeed;
    }
    [/as]

    Simple. Fast. Efficient. Of course if that was all it could do it would be nothing special – but it can do so much more …

    Multiple Key Support
    Accurate detection for one, two or three simultaneous key presses, including Location support. That means you can check for the difference between the left shift key and the right shift key. A full range of constants are built into the class, so rather than remember which keycode relates to which key, you just use our constants list. This list has been extended to support Enhanced keyboards, so you’ve got it detecting the Windows (or Apple Command) keys as well as the Application key.

    Set the Key Rate
    In an arcade game you need to pick-up the key presses as fast as possible, and BlitzKeyboard does just that. But you can also configure a Key Rate, so if you only want a certain key to fire once every second then that’s no problem.

    Bind Events or Functions to Keys
    You can set a key (or key combination) to fire an Event or call a Function. You control if the Event should fire when the key/s are pressed down, or fire on the release (when you let them go again). You pass in the Event and then set an event listener to listen out for it, and it handles the rest. It even takes the Key Rate into account, so your Event will only fire at the frequency you set.

    Wait Key support
    Say you need to wait for the user to press a key before proceeding with your game – then just use the waitKey() function! You pass it an Event and a type, and the next time a key is pressed the Event is dispatched, no matter which key caused it.

    Key Hit Counter
    Want to know exactly how many times a key has been pressed? Then call keyHit() on it and this method will tell you just that.

    I’ll be adding a few final features, and preparing the demos in the coming days. But the core class now exists in Google Code for the curious.

    Next up is BlitzMouse which will handle advanced mouse events – things like mouse zones, reporting on how fast the mouse is moving, scroll wheel handling and more.