A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: duplicate or attach?[help AND disc!]

  1. #1
    Member
    Join Date
    May 2004
    Location
    Raleigh NC
    Posts
    34

    duplicate or attach?[help[resolved] AND disc!]

    my first galactica style arcade shooter... but whats the best method for shooting?

    some use duplicate, others use attach... does it really matter?
    is enterFrame slower or worse than making the bullet MC two frames and adding script in the frames?

    also if someone could point me to a tut on how best to delete the bullet after it leaves the screen, would be appreciated.
    Last edited by willyeago!; 05-11-2004 at 03:55 PM.
    so there!

  2. #2
    Lunatic Baukereg's Avatar
    Join Date
    Nov 2000
    Location
    Lowlands
    Posts
    853
    About the enterFrame discusion; you should read this recent thread.
    http://www.flashkit.com/board/showth...hreadid=553263

    I use attachMovie(), because I don't see the advantage of duplicateMovieClip(). I use a for-loop to move each bullets, which also contains the code to remove it.

    Code:
    speed = 2; // speed of bullets
    screenWidth = 450; // width of screen
    
    for (var i=0; i<bullelC; i++) {
      _root["bullet"+i]._x += speed;
      if (_root["bullet"+i]._x > screenWidth) {
        _root["bullet"+i].removeMovieClip();
      }
    }
    I learn a lot of these 'Building Games in Flash 5' tutorials by David Doull.
    http://www.flashkit.com/tutorials/Games/more2.php
    Last edited by Baukereg; 05-11-2004 at 03:08 AM.

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    I also use attachMovie. In my own games, I generally give the bullet a life-span (expressed in milliseconds) and use getTimer() in the bullet's onEnterFrame handler to see if the bullet's time is up.
    When the time is up, I use bullet.removeMovieClip();

    With this method, the bullet is allowed to fly off the stage, but dies soon thereafter.

  4. #4
    Senior Member
    Join Date
    Jun 2001
    Posts
    290
    so I wonder if there is really any advatrange of using one or the other......
    in flash 5 I used to use an original MC off screen w/ all the code on it and then duplicate it when needed.....then the code will run only on the duplicates.....it was somewhat inefficient but it got the work done...however I see that more and more people here use attach movie....

    paul.

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    I suspect many of the folks that are using duplicateMovie tend to attach the code to their movieclips.

    I keep all my code in frame 1 (or the first frame after the preloader).

    After attaching a movie, I assign any functions it needs at that time.

    This is definitely more of a stylistic choice than anything else. Whatever you're used to...

  6. #6
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    You don't actually have to do either for a shooter ( Just to muddy the water )

    How about this: Just have all the bullets and baddies as author time content ? Just slap those sprites straight down on the stage.

    Arrange your sprites like you would your tiles in a scroller. So all the bullet sprites contain all the frames for all the bullet types so you can just gotoAndPlay to the correct frame, and then same with all your baddies.

    Cause they are already sitting on your stage, you don't have the overhead of having to attach or duplicate when you need them.
    Remember, when you press fire you've got a load of stuff to do. Firstly you'd attach / duplicate your bullet. Run a thumping sfx. Set a flag to say the bullet is running, put collision checks in place for it etc.

    If it's already there, you miss out a stage of that ( The slowest stage at that ).

    Also your game won't suffer slow down, because every sprite is already present you just request a free sprite when it's needed ( Say when a baddie wants to shoot ). If there's no free sprite, then it can't shoot.
    That way on later levels when you've bumped that baddie fire power up high you don't have to worry about too many sprites on screen, cause it will never be greater than your already max number of sprites.
    ( Ok, you can still do this with dynamic allocation but it's easier this way ).

    I did it this way for shooter games cause of the old days of having a C64 and Amiga, where you had a set number of hardware sprites to use, so I got used to recycling the same ones for different uses.
    It also means you can test the game on it's max settings ( All your sprites running ) nice and easily and see how adding new code to your game affects it's overal speed.

    Squize.

  7. #7
    Optimist Prime StenFLASH's Avatar
    Join Date
    Mar 2004
    Location
    Canberra, Australia
    Posts
    252
    When I first started off, coding games in Flash, I'd use duplicate. I quickly found myself with code spread between movieclips too much so I switched to using attach. My code's much more centralised now and I prefer it that way

  8. #8
    383,890,620 polygons nGFX's Avatar
    Join Date
    Oct 2002
    Location
    Germany / Ruhrgebiet
    Posts
    902
    note about attaching:
    if you forget to disable "export on first frame" for your exported "sprites" they will be loaded BEFORE the first frame of the movie!

    so you can have some 100k of exported gfx to be loaded before you even see a preloader ...

    <olli/>

  9. #9
    Senior Member
    Join Date
    Jun 2001
    Posts
    290
    @ squze:

    so what you mean is if I know I'll have at most say 50 NPCs on the screen, then I should already have 50 MCs somewhere off the screen and at run time or when first loading the game, simply attach code to them (the ones I need to use) while going and stop on the specific frame inside the NPCs MC based on the enemy type??....

    so when one of them gets killed by the player.....would you just disable its enterFrame code and put it back of screen for reuse later?

    thanks for clarifying,

    paul.

  10. #10
    Member
    Join Date
    May 2004
    Location
    Raleigh NC
    Posts
    34
    thanks for all the input, and ive decided to go w/ attach if anyone cares. wee games fun.
    so there!

  11. #11
    Flash hates me. crashlanding's Avatar
    Join Date
    Nov 2003
    Location
    UK
    Posts
    439
    Originally posted by pentru
    @ squze:

    so what you mean is if I know I'll have at most say 50 NPCs on the screen, then I should already have 50 MCs somewhere off the screen and at run time or when first loading the game, simply attach code to them (the ones I need to use) while going and stop on the specific frame inside the NPCs MC based on the enemy type??....

    so when one of them gets killed by the player.....would you just disable its enterFrame code and put it back of screen for reuse later?

    thanks for clarifying,

    paul.
    i think he meant instead of having 50 mcs, have one with 50 frames showing different sprites, and just use that mc over and over.

    I think.
    "wen i found my gerbil dead my other gerbil was eating it i just cried and screamed"
    http://www.livescripts.net

    --------------------------------------------------------------------------------
    Last edited by some moderator : Today at 9:01 PM.

  12. #12
    Member
    Join Date
    Apr 2004
    Posts
    82
    Some things I don't understand..

    If there are 10 attached bullets, bullet0 .. bullet9, how do you skip checking for bullets that have been removed? If bullets 2 to 7 have hit an enemy and have been removed, how would you make a for loop to only check bullets 0,1,8 and 9?

    edit: I think a for in loop would work, but according to test 4 on this site it's slower than a regular for loop.
    Last edited by ballofsnow; 05-11-2004 at 04:55 PM.

  13. #13
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    See the asteroids game on my website (link below) for one example of bullet creation and destruction.

    Here are 4 ways you can do it. My preference is #2, although #1 requires less array manipulation.

    1. Keep an array for bullets, and walk thru it when you want to check/update the bullets. As you walk thru it, check to see if the indexed entry bullets[i] is undefined. When the bullets are removed, set that entry to undefined. When a new bullet is created, add to the array in a slot which is currently unused.

    2. Keep active bullets in an array, and walk thru the array when you want to check /updatethe bullets. When the bullets are removed, splice them out of the array. When a new bullet is created, add it to the array.

    3. Attach the bullets to some parent movie and walk thru the objects that belong to the parent object, using a for-in loop.

    4. Give each bullet it's own onEnterFrame handler and do your checks from that. When the bullet is removed, that handler will no longer be called.
    Last edited by jbum; 05-11-2004 at 04:58 PM.

  14. #14
    Member
    Join Date
    Apr 2004
    Posts
    82
    I made a thread about #4 and the general consensus is that this isn't the best solution and it's better to have one onEnterFrame for the bullets.

    I like #2.

    Can you explain how to do #3?

  15. #15
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    pentru:
    Kinda

    Yeah if you wanted 50 sprites ( mcs ) on screen at a time, then yeah you'd have 50 set up on your stage ( You'd need an 8ghz machine to run the game though ), and crashlanding is right, each mc would have all the frames you'd need for every possible baddie ( Plus explosions for them ).

    Now the only code you need on them is
    code:

    onClipEvent(load){
    gotoAndStop(1);
    _visible=false;
    }

    So they are hidden and not bothering you.

    Forget attaching script to each one, every enterFrame slows down your game, so you want as few as possible.
    You'd set up a handlerMC ( I know there are better ways to go, but I'm not really up for doing a crash course on OOP I'm afriad ), and get that to loop through each mc running it's code. That way all your code is within just one mc.
    Also each baddie would have a flag, let's call it currentStatus.

    So:
    currentStatus=0; Not active
    currentStatus=1; Attacking / whatever
    currentStatus=2; Dying
    And possibly
    currentStatus=3; Dead

    You shot a baddie, you set it's flag to 2 for dying ( ie it's running it's explosion frames ). So you no longer check for a collision on it, but you can't re-cycle it yet cause it's still exploding. Once the explosion is complete you hide that sprite again and it's free for the game to use again.

    Hope that's a bit clearer, if it's not, just ask mate.

    ballofsnow:
    Further to jbum's good points, another way to do it would be to give each bullet it's own function pointer, eg

    var bulletFunc1=null;
    var bulletFunc2=null;
    etc.
    So when bullet1 is fired, you change it's pointer to bulletFunc1=shooting ( Where shooting is your bullet handling function ).

    Then in your main enterFrame, just put
    bulletFunc1();
    etc.
    So each bullets function is called every frame.

    Squize.

    Squize.

  16. #16
    Member
    Join Date
    Apr 2004
    Posts
    82
    I have a problem with splice. I'll try to describe what happens.

    Let's say I have 4 bullets in an array:
    bullet0,bullet1,bullet2,bullet3.

    In a for loop when i=1, bullet1 hits something and is spliced out.
    bullet0,bullet2,bullet3 remains.

    As I continue through the loop when i=2, it skips bullet2, and goes to bullet3. So bullet2 is not updated.

    What's the best way to fix this? Should I create another loop to splice out removed bullets?

  17. #17
    Member
    Join Date
    May 2004
    Location
    Raleigh NC
    Posts
    34
    ok any one have an opinion on hitTests? as in where to put one. is it better to put it on the hero and his bullets? or should the hitTest better placed on the enemies?

    if it doesnt matter, just tell me to shut up about it...nicely
    so there!

  18. #18
    Member
    Join Date
    Apr 2004
    Posts
    82
    I don't think it matters. They all get compared to each other anyways.

  19. #19
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    ballOfSnow, Here's two ways to handle the problem with the array index. My preference is #2.

    1. Subtract one from i.
    code:

    for (i = 0; i < array.length; ++i)
    {
    if ( /* i must be deleted */ )
    {
    array.splice(i,1);
    --i;
    continue;
    }
    // additional processing on element i here...
    }



    2. Walk thru the array backwards

    code:

    for (i = array.length-1; i >= 0; --i)
    {
    if ( /* i must be deleted */ ) {
    array.splice(i,1);
    continue;
    }
    // additional processing on element i here
    }

    Last edited by jbum; 05-12-2004 at 03:32 AM.

  20. #20
    Member
    Join Date
    Apr 2004
    Posts
    82
    That's awesome. Thanks jbum.

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