A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Generic objects? Clipping?

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    4

    Post Generic objects? Clipping?

    Hello,

    I am an amateur programmer (no formal training of any kind) and I have been messing with Flash for a good while. I have a pretty good handle on most of actionscript but there are a couple of things that I can't seem to work out.

    First, is it possible to refer to an object in a generic manner? For example, I have bricks and bullets, and i need the bricks to explode when a bullet touches them.

    Currently, I would name all of the bullets manually (bullet_01 ... bullet_99) and have each brick make 99 checks (hittest with bullet_01, hittest with bullet_02, etc). I have always thought that there should be some way to test against a type of object, but I haven't figured out how to do this.

    If I wanted to make 10 bullets and 1000 bricks that would be 10,000 clipping checks per frame - is there a better way? Am I worrying too much about cycles?


    Secondly is the issue of clipping detection for making a platformer style game. I have seen many of these work well but whatever systems I devise to keep the player in check seem to slow the game down - I have to be doing it wrong, but I've been unable to find any tutorials/books that deal with handling clipping in games beyond simple space-invaders style stuff.

    Can someone point me in the right direction?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    My advice to anyone who just wants to get stuff working with a minimum of fuss is to not re-invent the wheel. There are several good physics engines out there (APE, foam, box2d, motor) which will all handle object interactions for you. If you're curious, then you can look through the sources to see how they do it, but there's some intimidating math involved in getting things to work efficiently.

    To answer your other question about collections of objects, that is what Array is for. Instead of manually placing 99 named bullets on the stage, you can have a bullets Array and place new instances of a Bullet class in it as you need them.

    Code:
    var bullets:Array = new Array();
    
    function addBullet():void{
      var bullet:Bullet = new Bullet();
      //set bullet properties like x, y, velocity
      bullets.push(bullet);
    }
    
    function removeBullet(index:int):void{
      bullets.splice(index, 1);
    }
    
    function checkBullet(b:*, bindex:int, arr:Array):void{
      trace("do something with bullet b here");
      if (needToRemoveBullet){ //you define the condition here
         removeBullet(bindex);
      }
    }
    
    bullets.forEach(checkBullet);
    This may not be the best approach for your game, but it should give you an idea how to handle collections more generically.

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    4
    Ah thank you, this is exactly the sort of thing that I thought I must be missing.

    As for the clipping engine, I did not realize that there were actionscript libraries available for that sort of thing. Can you recommend one that you know to be well documented?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I've only used APE, which was one of the earlier as3 physics libraries. It worked well enough for me, and it has a comprehensive AsDoc. Note that these are not just "clipping" libraries, they are full physics engines. The default behavior might not be what you want (everything collidable against everything else, automatically adjusting object velocities and angles), but in general you can set them up to do what you want.

    I've had some ideas about bitmap based collision detection specifically for shooter type games, but I haven't built anything in that area yet. The basic idea would be to draw all bullets in one bitmapdata, anything they can collide with in another bitmapdata, then apply some filters and merge the bitmaps into a third bitmapdata in order to see where collisions happen. I haven't ironed out all the details yet, so don't wait for me to get off my ass on this.

  5. #5
    Junior Member
    Join Date
    Jun 2008
    Posts
    4
    I will look into that, I am sure that I could pear it down to just the features that I need.

    Thanks again for the advice, Flax. Fnord to you and yours.

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