Header image

It all started as a performance test…

December 1st, 2007 | Posted by lars in Actionscript | Flash 9

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)

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

2 Responses

  • Jonathan says:

    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

  • Mark says:

    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 Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">