Monday, December 28, 2009

Question about programming ActionScript in Flash - How do I do collision detection?

I want to detect collision between two objects based on the image. I can use hittest for this, but I want the collision to be true when object A hits object B's image instead of object B's bounding box.





Example: Say I have an arch. If object A goes inside the arch (not touching it), it will register as a hit, although I never acutally touched the image. How can I say that the hit is true only when object A touches the image?Question about programming ActionScript in Flash - How do I do collision detection?
You need a method to get the pixel contents. There is a value for transparent pixels. You need to put logic in to compare the individual pixels, and flag if theres a collision between 2 non-transparent pixels. This is the only way. I'm sorry I do not have the actual function definitions available, but any good reference on flash graphics libraries should do the trick.Question about programming ActionScript in Flash - How do I do collision detection?
hold a sec, i have a solution Report Abuse

function checkOverlap():Void{


if(mcCircle.hitTest(mcSquare)){


trace(';The objects overlap';);


}


}


var nOverlapInterval:Number=setInterval(chec鈥?br>




note. mcCircle and mc Square are two MovieClips


for more - anilkumarnd@yahoo.com
Quick note without testing....





Can you just do a hitTest with the image and what u want.





for example object A and object B are on the root timeline- ur image is inside object B. make ur image be inside a movie clip which in turn is inside object B. then do a hit test with onjectB.image instead of just object B. Hope that makes sense.





heres some dirty code for ya:





box_mc.onPress = function() {


startDrag(this);


};


this.onEnterFrame = function() {


if (box_mc.hitTest(circle_mc.circle2_mc)) {


trace(';you hit the circle';);


}


};





there are 2 objects on the stage. box_mc and circle_mc, then circle2_mc (which is inside circle2_mc- where ur image would be.)





send em a note if u have questions and please forgive any yahoo formatting to the code.
visit www.actionscript.org tutorials section and search for hit test keyword Enjoy!
http://www.flashkit.com


has some decent tutorials on this.


Good luck.

No comments:

Post a Comment