A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: add an explosion were hitTest occured

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    75

    Question add an explosion were hitTest occured

    hi
    i am using AS3 and i have this hitTest occuring:

    Code:
    function hittest(e:Event):void {
    	for (var i=0; i<bulletArray.length; i++) {
    		if (rocketShip.hitTestObject(bulletArray[i])) {
    			trace("hit");
    			removeChild(bulletArray[i ]);
    			bulletArray.splice(i,1);
    			var fire:MovieClip = new explosion();
    			addChild(fire);
    			fire.x = rocketShip.x + 20;
    			fire.y = rocketShip.y - 5;
    			
    			}
    so at the moment an explosion called "fire" is added to the front of my ship when the hitTest = true, but sometimes my bullets don't hit the front, they might hit the side or back of my ship. so i would like my explosion to be added to wherever the hitTest occurred on the two objects.
    thanks

  2. #2
    Member
    Join Date
    May 2009
    Posts
    62
    The easiest way would be to simplify your ship to a circle, or oval, and then find the point of intersection based on the angle to the bullet.

    Code:
    var shipWidth:Number = 10; //define constants
    var shipHeight:Number = 5;
    
    var angle:Number = Math.atan2(bullet.y - ship.y, bullet.x - ship.x); //angle to the bullet (y comes first)
    
    var explodePoint:Point = new Point();
    explodePoint.x = shipWidth * Math.cos(angle);
    explodePoint.y = shipHeight * Math.sin(angle);
    Something to that effect. Assuming the math is correct
    Last edited by Sharsnik; 06-24-2009 at 08:06 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center