nulldesign // lars gerckens » It all started as a performance test…

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

Required

Required, hidden

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

About

  • nulldesign is the company and showcase site of freelance developer Lars Gerckens who is based in Hamburg, Germany. I'm available for freelance work with focus on Flex, Flash and AIR development. I like to create dynamic user interfaces, cutting edge web experiences and innovative ways to visualize data.

    This site showcases some of my personal work and projects as well as an selection of commercial projects.

    If you are interested in my work or you have any questions, don't hesitate to drop me a line:

  • Profiles: Xing | Linkedin
  • Mail: lars [at] nulldesign [dot] de

Categories

Search