问题
In my game I have person who shoots and I need if the bullet hit somthing to delete the sprite.
var i = 1;
var f:Function;
addChild(bullet);
bullet.addEventListener(Event.ENTER_FRAME, f = function(){
bullet.x += movex*i;
bullet.y += movey*i;
i++;
});
How to make hitTestObject()
with everything?
回答1:
There are a bunch of techniques for collision detection, however, the method to use depends on the target object's shape. Pixel perfect hittesting is the most accurate collision detection method, although this is very slow. Geometrical hittesting is probably the fastest and can be very accurate if done correctly. One disadvantage using hitTestObject
is that it's quite slow. So my opinion in case you need to handle a large pool of object is to use alternative methods for collision detection.
Definitely for object to object collision detection you need to loop over each target object placed into an array (but for performance improvements into a <Vector>
) and test if the bullet is hitting the destination object.
bullet.addEventListener(Event.ENTER_FRAME, testCollision);
private function testCollision(e: Event) {
bullet.x += moveX*i;
bullet.y += moveY*i;
for (var j = 0; j < objects.length; j++) {
if (bullet.hitTestObject(objects[j])) {
// Do something
}
}
i++;
};
It's worth to read these articles for further explanations:
http://plasticsturgeon.com/2011/03/actionscript-collision-detection-u-circle-circle-collision-detection/
http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/
http://www.mikechambers.com/blog/2009/06/25/strategies-for-optimizing-collision-detection-with-bitmapdata-hittest/
And here is a library which do the heavy work: http://code.google.com/p/collisiondetectionkit/
回答2:
I would tackle it like this (I'm assuming the bullets are MovieClips you are adding from the library).
I'm using zombies and plants as examples of MovieClips already on the stage, which you are manually passing into the liveObjects array.
For larger scale games obviously you wouldn't hard code, but I'm trying to abtract away the extra stuff to make the example easier to understand)
import flash.events.Event;
import flash.display.MovieClip;
var liveObjects:Array = [
zombie1, // A MovieClip on the stage
zombie2, // ditto
zombie3, // ditto
plant1, // ditto
plant2 // ditto
];
// Create bullet
var bullet:Bullet = new Bullet();
bullet.x = 200;
bullet.y = 200;
addChild( bullet );
bullet.addEventListener( Event.ENTER_FRAME, f );
var i:int = 1;
var moveX:int = 1;
var moveY:int = 1;
function f( e:Event ){
var thisBullet:MovieClip = ( e.currentTarget as MovieClip );
thisBullet.x += moveX * i;
thisBullet.y += moveY * i;
i++;
for( var j:int; j < liveObjects.length; j++ ) {
if( thisBullet.hitTestObject( liveObjects[ j ] ) ) {
trace( "Hit " + liveObjects[ j ] );
}
}
};
回答3:
Hopefully you have some array that holds a reference to all of the enemy sprites. Just loop through that calling hitTestObject() on it with the bullet.
来源:https://stackoverflow.com/questions/14089241/as3-hittestobject-to-hit-all