It all started as a performance test…
Ok, AS3 can render thousands of particles per frame… but what if we want more than a few single moving pixels and hittests? So I started to write a small test, instancing bitmaps on random locations. And hey: These objects look like bombs, now I need something to destroy them … and there was the battleship ;). For the hittest I used getObjectsUnderPoint. It seems like this method is a bit slower than hitTest or hitTestObject, but you can actually save a lot of loops because you don’t need to test each bomb with each shot. Instead of:
// pseudo code for each(bombs) { for each(shots) { if(bomb.hitTestObject(shot)) { // shot hit bomb... } } }
you go:
// pseudo code for each(bombs) { var objects:Array = shotClip.getObjectsUnderPoint(bomb.x, bomb.y); if(objects.length) { // shot hit bomb... } }
This only works, if you have a seperate clip for the shots. Enough of the talk: Launch!

(Move using the mouse and rotate the gun with cursor keys)
01. December 2007 | Posted in Actionscript, Flash 9


2 Comments
1. Jonathan | 02. April 2009 at 11:16
hi..i was wondering how do you position the bullets that’s coming out of the ship..i tried to create a tank that will fire cannons..but i can’t position the cannons correctly if i rotate the tank..
cheers,
jonathan
2. Mark | 29. July 2009 at 11:02
Hi Jonathan,
I personally use a mixture of SIN and COS to position bullets if they are coming out of a turret on a rotated item (that is – if they are being fired from a turret that doesn’t originate from 0,0)
Adjust your firing by X/Y pixels from the center:
var ang:Number = rotation + 90; (or whatever figure you need)
var x_offset:Number = Math.cos(ang / 180 * Math.PI) * DISTANCE;
var y_offset:Number = Math.sin(ang / 180 * Math.PI) * DISTANCE;
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackback this post | Subscribe to the comments via RSS Feed