I'm building my first Flash program, and I need help with a problem with collision detection...
May 30, 2009 5:06 PM   Subscribe

I'm building my first Flash program, and I need help with a problem with collision detection...

I'm making a side scrolling video game and am trying to tweak my collision detection.

What I currently have:
The "player's"/controlled Movie Clip deducts health from a "health" movie clip when other specified movie clips hit the player. The movie clips hitting the player are then removed. However, the top half of the graphic I'm using (a png with transparent background) is recessed from the lower half, meaning that if an enemy movie clip hits the top half of the bounding box, the result is what looks like a collision with empty space. Here's the basic jist of my current code (I place this in the Actionscript of whatever object I want to collision detection against):

function onEnterFrame()
{
if(this.hitTest(_root.player))
{
this.removeMovieClip();
}
}

What I'm trying to do:
I'd like to draw a transparent circle on the upper portion of the player movie clip, but *within* the player movie clip so that I can run hitTest against that instead of the entire graphic. I've been trying to reference the circle in the hitTest like so: if(this.hitTest(_root.player.collide) but no joy. I also tried referencing the clip directly with (_root.collide). I've also tried both putting the circle on a separate layer and in the same layer within the player movie clip - In all cases, I end up with no collision detection at all. Ultimately, I want the effect of enemy objects only damaging that top, vulnerable part of the player.

What I'd like to avoid:
BitmapData.hitTest
hitTestPoint

I tried to figure these out, but they seemed overly complex for what I want to do. Also, I've still quite newbish and this is my first exposure to ActionScript.

Bonus question: can anyone explain (or point me the the right direction for) when I do and don't need to specify the Identifier and Class for a symbol? Also, any good Flash game programming tutorials that teach by doing and then explaining without laying out the entire groundwork for the language (like, Kongregate's Shootorials)?
posted by monkeyagent to Computers & Internet (6 answers total) 3 users marked this as a favorite
 
If you're using an actual circle, and not a complex shape you don't need a graphic, you just need to calculate the distance between the center of your imaginary circle. It runs a ton faster than the built-in hit testing.



if (detectHit(clipA.x, clipA.y, clipB.x, clipB.y, 50) == true){
//do collision stuff.
}

function detectHit(x1:Number, y1:Number, x2:Number, y2:Number, distance:Number):Boolean{
var distanceX=Math.abs(x1-x2);
var distanceY=Math.abs(y1-y2);
if ((distanceX*distanceX)+(distanceY*distanceY)) < (hitDistance*hitDistance)){
return true; // basic Pythagorean theorem
} else {
return false;
}
}


If you want the hit area to be different than the center point of the movie clip, just add a property to your movie clip with that center,
clipA.hitX=0;
clipA.hitY=-25;
clipA.hitSize=50;

clipB.hitX=0;
clipB.hitY=-25;
clipB.hitSize=50;

if (detectHit(clipA.hitX clipA.hitY, clipB.hitX, clipB.hitY, clipA.hitSize+clipB.hitSize)==true)
... do stuff


Note that this basically code and hasn't been through a compiler and might well throw errors.
posted by Ookseer at 6:43 PM on May 30, 2009


Best answer: 1. I just tried this code and it works:

onEnterFrame = loop;

function loop() : Void
{
if ( enemy.hitTest( player.collide ) )
{
trace( "ouch" );
}

}

Notes: first, I created a Movieclip with the instance name "player." Embedded inside it is another Movieclip called collide. You DID give both MC's instance names, didn't you? Check collide. If it just has a Library symbol name, the code won't work.

Next, I animated a MC called "enemy" so that it crashes into "player.collide." I used tweening for this quick test. Of course, I'd use code to animate the enemy in a real game, but for this test, tweening is fine.

Finally, I added a layer for the code and entered the Actionscript above.

If you can't get this to work for you, memail me your email address, and I'll send you my FLA.

2. PLEASE do NOT place Actionscript code on objects (e.g. on Movieclips). AS code belongs on the timeline. Actually, it doesn't belong there. It belongs in class files, but ANYTHING is better than putting codes on MovieClips. You'll get into all kinds of trouble if you do that. If you have a book that suggests you do that, it's a really old (and bad) book. STOP!

3. The tutorial you're looking for is "Making Things Move." Or the AS 3 version of it (see below).

4. It looks like you're using AS 2.0. If you can possibly get your hands of Flash CS3 or CS4, do yourself a favor and switch to AS 3.0. Since you're learning the language for the first time, you're in a good position to do this. It would be unfortunate for you to learn 2.0 and then have to switch to 3.0 later. START with 3.0. Actionscript 2 is almost dead. As time goes on, it will get harder and harder for you to get help with it.
posted by grumblebee at 9:37 PM on May 30, 2009


As Ookseer points out, if you can approximate to circles then you only need to check whether the distance is less than the combined radii (you can avoid applying square root to find distance by comparing with the combined radii squared, as Ookseer does).

If you need to accurately test shapes against each other then you need to use one of the two things you've said you want to avoid; that's what they're for. For example, you could do a basic bounding-box check with hitTest, then if that hits do point tests covering the player's ship in more detail and see whether it's a false alarm.

The Identifier/Class stuff relates to whether you want to dynamically create instances of symbols from the library and associate them with classes. Nowadays with AS3 you're supposed to try to keep all of your code in separate classes and make everything dynamic, which can be tricky to get your head around without a background in OOP and clashes with the timeline-orientated thinking the IDE has always encouraged (and been successful at wooing designers with).
Although many/most Flash projects are still produce with AS2 (it's quicker and simpler for just hooking up small bits of code to timeline-orientated stuff), as grumblebee says it makes sense for a beginner to use 'clean' AS3 from the start.
posted by malevolent at 1:10 AM on May 31, 2009


Response by poster: These were all great suggestions guys, and I'll definitely be returning to these techniques in the future.

grumblebee helped me work out the problems with my code and I've gotten it to work now.

A big part of the problem was that I had the hittest code attached directly to a movie clip - after I gave it its own class file and put the code in that (with a few additional adjustments), it worked perfectly.

Thanks everyone!
posted by monkeyagent at 3:35 PM on June 4, 2009


Response by poster: I don't know if any of the posters are still checking this thread, but I've finished a preliminary version of the project I was working on. You can view it for a limited time here:
comicream.dreamhosters.com
It's silly and simple, but I'm more or less happy with it. I want to thank you all again for the help - I've learned a LOT about Flash since I originally posted this question, and overcome some much more difficult hurdles since, but I don't know that I would have kept going if you hadn't helped me with this first one. Now I feel comfortable enough with it to have fun and try some other projects.
Another detail I hadn't mentioned in the original post - other than some minor tinkering in C and Python, Actionscript has become the first object oriented programming language I've learned well enough to use on a practical level.
posted by monkeyagent at 9:06 AM on December 29, 2009


Pretty cool game, MA. I was just searching for info on AS b/c I wanted to try my hand at a game or two as well. Nice game - makes me curious as to how long (ball-park) that took you, being somewhat newbish and all...
posted by prodevel at 6:51 AM on February 20, 2010


« Older 좋은 한영사전 알습니까?   |   Looking for a good women's tailor in Los Angeles. Newer »
This thread is closed to new comments.