Source Code

Accurate Timing With Actionscript 3

     Finally made some time to update my blog!!! I've been working hard and added lots of new classes to my library which i used for my own games. I'll be updating this site with new classes and examples over the coming months - so be sure to stay tuned for all the updates!

     First on the list is the problem of timers and timing in general with Flash. Well not with flash as such, but with the web browsers. It seems that the flash player plugins for web browsers aren't very accurate at all when it comes to timing. You can ask flash in AS3 for any timer delay you like (from 1ms upwards). But the flash player will not deliver such small resolutions and even on larger delays, has a big variation between TimerEvents firing. I've written my own timing code, and implemented my own timers in AS3 and added them to my ever expanding library for anyone to use! Read on if you want to know how and why you would use these classes.

Actionscript 3 Sprite Trails

     I need a simple sprite with a small trail for a game i'm working on so i had to create some way of tracking sprites and giving them a trail. Heres the simple demo i've created - i wonder if someone could turn it into a game idea? It's just some round blobs "swimming" from left to right. It's pretty rough, but atleast you can see what i mean by sprite "trails".

Actionscript 3 Animated Sprite Engine Library

     Finally had time for an update! Here are the animated sprite classes that i've mention before, which are being used in my nearly finished game. I've done some basic testing and built the main base for my animation system (i'll extend it to animated tiles for backgrounds later). I couldn't find an animated bitmap class on the web for Actionscript 3 that i could use for my specific requirements - so i've made my own. Heres a quick run down of the features:

  • Simple XML animation format
  • Unlimited number of "states" to define different actions for each sprite
  • Each frame can be any size rectangle, located anywhere on the animation image (not just repeating rectangles like most systems)
  • Possiblity to have huge sprites (can use multiple images for a single sprite - only limited to 2880 bitmap size maximum per asset)
  • A frame can be located anywhere on the image and can reuse a frame multiple times in a single state aswell as across multiple states
  • Optional handle/hotspot for handling sprites instead of top left
  • Minimizes overdraw by specifying smallest rectangles not just fixed width & size for each frame (handy for bin packing images)
  • No loop, normal looping or ping-pong style animation looping
  • Play animation forwards or backwards
  • Start animation at any frame, and jump to any frame
  • Allows both animated sprites & bitmaps (sprite has a child bitmap)
  • Clone animatedBitmaps to save processing the XML file multiple times
  • FRAME_CHANGED and ANIMATION_COMPLETE events
  • Play animation once, loop animation infinitely or specify number of loops
  • Each frame can have a specific number of milliseconds delay, allowing fine control over complex animations
  • Sprite animation playback speed can be adjusted to slowdown or speedup the whole animation on the fly
  • Set current state by statename as well as index
  • Uses ENTER_FRAME instead of timers for animation update (useTimer option if you want to use timers for specific animations)
  • Stagelayer object to separate animated sprites/bitmaps into layers (needed for ENTER_FRAME and handy for collisions)
  • 4 Different Rendering methods for memory use vs speed (and for different requirements as one type doesn't fit all)
    • FRAME_BUFFER_REFERENCE (uses original bitmapData & scrollRect)
    • FRAME_BUFFER_OVERSIZED (uses a single growing bitmapData & copyPixels)
    • FRAME_BUFFER_CACHED (uses a seperate bitmapData for each frame & caches it when first generated with copyPixels)
    • FRAME_BUFFER_UNIQUE (uses a new bitmapData & copyPixels everytime the frame changes)
  • Uses frame time delta with smoothing for smoother animation (includes option to turn it off)

AS3 Preloader With MochiAd, MochiBot, Simple Domain Locking and Glossy Vista Style Progress Bar

     As promised in my last blog entry, heres my version of an AS3 preloader. It's the second time i've typed this out, as the first time i had lost my cookies while messing around in another firefox window and when i came to post this entry it just said access denied and it had all gone! Lovely! Anyway the main points of my preloader are

  • MochiAds & MochiBot already integrated
  • Simple domain site locking implemented
  • Nice glossy style progress bar
  • Reusable code, not integrated heavily into a project
  • Easy to add to existing AS3 project
  • Pure AS3 classes
  • Works with FlashDevelop or Flex Builder
  • Minimal Delays, fairly small size

And most of all i'm giving all the source code away, not just snippets :)

Vista Glossy Progress Bar In Actionscript 3

     This time i wanted to make a nice progress bar for the preloader i've made. First of all, the usual check of Google. Plenty of hits, mostly for AS2 - a few for AS3 but nothing that takes my fancy. I did see a nice turning "barbershop pole" stripe bar, which i started out aiming for but before i'd written much code i figured a nice thing to do would be to simulate the nice glossy progress bars seen in windows vista. If it hasn't got anything else right, it has nice progress bars. Even though they still run on "microsoft time" and hardly give you a good indication of how long something is going to take, they do stand out and look the business.

Actionscript 3 hitTestObject and Pixel Perfect Collision Detection

     For a flash game project, i needed to see if a number of sprites had collided. I soon found out that hitTestObject doesn't support perfect collisions for non-shape sprites, so i needed another solution.

     After wasting some time discovering hitTestObject only supports bounding rectangles for non shape sprites, i needed to find a pixel perfect collision detection method that worked on any sprites. One of the ways i've done it in the past is quite common, and has been used a lot over the years. You can approximate most sprites fairly well using a series of circles to form a collision detection area. Then it's fairly easy and reasonably fast to see if any circles have collided. This time however, i wanted to use pixel perfect accuracy, but i wasn't sure if flash was powerful enough to cope with such a task. It turns out that Flash Player 9 and actionscript 3 handle the job reasonably well with a clever bit of code from some smart cookies.


Theme & Icons by N.Design Studio
Syndicate content