An introduction to ActionScript 3.0 – week 8

In week 8 of the ActionScript intro we move on to collision detection and using hitTestObject to find out if two objects overlap, and what we should do about it if they have!

In this bundle I’ve combined both Week 8 Lessons 1 and 2 into a single document and provided the full source code to the exercises, but it’s recommended that you work through it yourself instead of just copying and pasting the code. I know typing in code isn’t fun, but by typing it rather than pasting it you get a far better idea of how everything fits together and you’re forced to think about the code and what it’s doing a lot more.

By the end of this tutorial, you’ll be able to build a simple Breakout/Arkanoid clone like this:

I’m not saying that it’s a fun game, but it’s starting to shape up – we’ve got functions, graphics, input and some basic collision detection. These are the tools you need to be able to write a proper game! We’re making progress! =D

Download link: An Introduction to ActionScript 3.0 – Week 8 (both lessons combined)
Audience: Beginners who know a little about variables, functions, objects and how to perform some basic programming math. You’re going to have to focus for this one – but you can do it!
Format: PDF
Content License: The document, its contents and the provided source code are released under a creative commons non-commercial attribution share-alike 3.0 license by me (r3dux) and comes with no guarantee of correctness, fitness for purpose or anything of the sort. The background was sourced randomly from the Internet last year and I’d be happy to add a correct attribution or remove it if it’s yours.

I’ve tarted up the breakout game above (from its more basic state as included in the download pack) to wait for a mouse click before starting, reset on click, and also reset when you lose the ball or break all the blocks, plus I’ve added a change in ball speed dependent on whether the ball hits the left or right side of the bat. The updated flash file can be found here – however, be warned that ALL THE CODE is in the same file, and it’s just bodged together to keep things as simple as possible while actually doing something vaguely useful. What I’m saying is – don’t write your stuff this way. When you write your awesome code use classes and methods and public/private access restrictions and do things properly! Also, faking mouse events is a sure sign of badly designed code, okay? I just used it to keep the code size down.

Honest ;)

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