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!
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.