ActionScript 3.0: Per-Pixel Collision Detection inna rub-a-dub Peggle Stylee

I’ve been working on writing a collision detection class, and it’s not as easy as you might think. It’s simple enough to boundary test (i.e. bounding-box overlap test), but to do per-pixel collisions you have to dump your symbol data into a BitmapData object, and you HAVE to use the top-left as the registration point for all your colliding symbol instances, which means you end up doing a lot of offsets & shiz… Ho hum.

Anyways, I knocked this Peggle / Pachinko thing together over the course of yesterday and today. You can place as many “pegs” as you want, wherever you want ’em, same with the “balls”, and in theory, I should be able to scale both pegs and balls to any size while still keeping the collisions accurate, but I’ve not tested that yet because after debugging this for a while I need a break.

But hey, look at the purty colours :)

No source-code for this one just yet as it’s not in a fit state (it has waaaay too much debug output per collision that I don’t want to strip out just yet, and enough swearing to make a salty sea-dog blush ;)). When I’ve knocked it into shape some more I’ll chuck the Peg, Ball and Collision classes here in an update.

Update: Source code and flash files now available after the jump. Some of the code is a bit yucky, so apologies in advance for my bodge-hackery =P

Continue reading ActionScript 3.0: Per-Pixel Collision Detection inna rub-a-dub Peggle Stylee

ActionScript 3.0: A Dynamic Frame Rate Switching Class to Lower CPU Usage

Flash gets a lot of negative press because it’s seen as using a heap of CPU time and bogging everything down. And it’s a fair cop. Most flash will eat up your CPU cycles even when it’s sitting there doing nothing. But this isn’t a fault of flash, but rather of flash developers. Let me explain…

When you start a piece of flash work, you assign it a frame rate at which you want it to run, so it’ll update the screen, say, 30 times a second. This is all fine and good for when you’re animating things on the stage. But what about when you’re not? Well, it’s still running at 30 frames per second and chewing up your CPU like a crazy melon farmer. This is Not A Good Thing. So, anyhow, I’m watching this video about SWF Framerate Optimisation, and the guy’s showing how you can modify your code to lower the frame rate when there’s not a lot happening, and bring it back up when you’re animating. So I had a crack at it, and lo & behold, it works fine for the specific piece of flash I’d coded it into, so I wondered if I couldn’t just go and make a RateController class. This way, I could add a RateController object to any project to dynamically change the project’s frame rate depending on whether the mouse was over the stage or not.

And after much swearing about not having global access to the stage properties, I found that I COULD!!!

Here’s a working example placed into the attracting particles code I wrote yesterday:

Note: The animation starts at full speed for two seconds on startup. It’ll drop to the sleeping rate (5 fps) two seconds after the mouse leaves the stage, and then ramps back up to its waking rate (30 fps) instantly when the cursor is back over the stage. The FPSCounter shows intermediate numbers because it’s based on an average.

To add a RateController to any flash project, you can just use something like:

Not bad, eh?

Full class code & file downloads after the jump…

Continue reading ActionScript 3.0: A Dynamic Frame Rate Switching Class to Lower CPU Usage

ActionScript 3.0 Particle Systems #6: Particle Attraction

More ActionScript… This time we’re attracting the particles towards the mouse cursor when the distance between them is low enough… Looks alright, just a kinda fun effect & dead simple to code.

Source code and flash files on the flipside…

Continue reading ActionScript 3.0 Particle Systems #6: Particle Attraction

ActionScript 3.0 Particle Systems #5: Smoke Effect

I’ve basically spent the day trying to get this one right, and it’s not too bad, although it’s pretty heavy on the CPU. Really, you want about 150 particles and a slower fade out, or 150 particles and add two per frame instead of just one, but on my rig it’s getting jerky with that many particles, so in the animation below we’ve got 80 particles as a compromise =( Also, because we’ve only got 80 particles I’ve deliberately ramped up the alpha decay (i.e. how quickly the particles fade out) and upped the vertical speed to get them off the stage before the particle is forced to vanish by our particle limit. If you put the mouse cursor right down near the bottom of the stage and look at the particles at the top you’ll just catch them vanishing instead of fading out fully, but these are the compromises we make for a smoother framerate ;)

If it’s running poorly on your machine, try right clicking on the stage and lowering the quality, or just grab the files from after the jump and mess around in Flash CS4 (free trials available) with all the different variables (especially the ones that start with min or max!!).

I’ve set the window-mode to use GPU acceleration on this one, so if nothing displays and you’ve got an older graphics card (and you want to see it), just view the source code for this page, grab the object section and change wmode=”gpu” to wmode=”direct”.

Credits: LearnFlash.com’s particle systems vids for the how-to, kaioa.com for the FPSCounter Class, and me for coding it up and running with it!

EnJoY!

Update: I’ve modified the above example to use the RateController Class I wrote yesterday. Aside from adding a RateController object, the only other modification was adding a listener to toggle the animation on a MOUSE_LEAVE event (cursor leaving the stage), and then changing the toggleAnimation function to wait for a click (and not MOUSE_MOVE i.e. cursor returns to stage) before adding particles again. Too easy… :)

Update 2: Many thanks to Joel for pointing out that in my zeal to optimise things I’d actually added some null pointer errors as well as some extra fps, and for correctly suggesting that using Sprites instead of MovieClips would result in a small speed boost, too. The swf file above and code & zip file below have received suitably stern words, and everything now works (relatively) swiftly and error free.

As usual, source code and flash files after the hop skip jump…

Continue reading ActionScript 3.0 Particle Systems #5: Smoke Effect

ActionScript 3.0 Particle Systems #4: Bubbles

Yet more ActionScript 3.0 again! This time it’s a kind of 2D particle fountain. New in this iteration is code so we can pause and resume the animation instead of just start/stopping it, and each bubble’s colour gets subtley modified via colorTransforms.

Last one for the day methinks – I’m knackered. Happy, but knackered :)

As usual, source code and flash files after the jump for those of a particularly masochistic bent…

Continue reading ActionScript 3.0 Particle Systems #4: Bubbles