View Full Version : game hitTest problem

03-15-2003, 06:52 AM
I'm learning actionscript, and to help myself along am making a very simple game: the idea is that you can create simple drawings by moving tetrus-like shapes around the screen.
So far so good, except that I'd like to prevent the shapes from being able to move over one another. I've tried various hitTest things, but no go.

Would some one out there have a look? Maybe I'm going at it completely wrong?

03-16-2003, 12:49 AM
You've made a good start but your structure is a bit wrong. The code as you have it now attaches movie clips, moves them around, then renames them all to "none". Appart from the fact that it's sort of bad form to have multiple clips with the same name, you need to be able to track your clips.
See the Advanced Pathing tutorial for some ideas. What you want to do is name your clips seuqnetially (shape1, shape2, etc) and then each time you try to move something, run a loop (see the Scripted Loops tute) which checks if the current shape (_root["shape"+count] or similar it will be) is touching any other shape. The code for the loop will be somethign like:
for (var a = 1; a<count; a++) {
// Loop through all existing (sequentially named) clips.
// If we're touching any existing shape, jump back
if (_root["shape"+count].hitTest(_root["shape"+a]) ) {
// Jump back code here.

03-18-2003, 09:47 PM
Well you were close. I've fixed it up for you but now you're experiencing the biggest issue with hitTest whichis that it draws a rectangle around the MC and counts all that space as part of the MC. So if you try to move two L shape blocks so that they sit with one nestled inside the other, it wont work... lemme try and ACII diagram that:

Dash is blank whitespace, x's make on L piece, o's make another.
THe only way to get around this is to use the shapeFlag attribute of hitTest but that will require that you basically select a whole heaps of points along the outer edge of the MC and check every one of those points each time you move the MC.

03-20-2003, 05:42 PM
Excellent. Thanks for all your trouble.