A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: How do I re-use my bitmaps?

  1. #1
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590

    How do I re-use my bitmaps?

    Have a look here:

    http://www.birchlabs.co.uk/Commando4.swf

    There's a noticeable slowdown when initiating troops. That's because the sprite sheet I'm using is over 100 KB now that muzzle flashes and deaths are in it. Splitting the sprite sheet up into separate sprite sheets doesn't speed it up, it brought it down to 6 FPS whenever the troops were changing sprite sheet (hence me combining the sprite sheets into one big one).

    The problem, as I see it, is that every time I create a troop, I'm generating a new sprite sheet bitmap.

    PHP Code:
    function createTroop(team:Numbersoldier:Number):void {
        if (
    soldier==1) {
            var 
    myTroop:Troop = new Troop(this);
            
    myTroop.200+Math.random()*150myTroop.cacheAsBitmap true;
            if (
    team==1) {
                
    myTroop.= -44myTroop.team=1;
              var 
    rifleRight:RiflemanRight = new RiflemanRight();
               var 
    rifleRightBMP:Bitmap = new Bitmap(rifleRight);
               
    myTroop.addChild(rifleRightBMP);
              
    troops1.push(myTroop);
            } else {
              
    myTroop.750myTroop.xspeed = -myTroop.xspeedmyTroop.team=2;
              var 
    rifleLeft:RiflemanLeft = new RiflemanLeft();
               var 
    rifleLeftBMP:Bitmap = new Bitmap(rifleLeft);
              
    myTroop.addChild(rifleLeftBMP);
              
    troops2.push(myTroop);
            }
          
    stage.addChild(myTroop);
          
    myTroop.frame();
        }

    Most important part to read in that script is this, btw:

    PHP Code:
    var rifleRight:RiflemanRight = new RiflemanRight();
               var 
    rifleRightBMP:Bitmap = new Bitmap(rifleRight);
               
    myTroop.addChild(rifleRightBMP); 
    Every time the createTroop() function is called, I'm creating a new bitmap (riflemanRight or riflemanLeft). Now, if I only declare this bitmap once (ie, moving it out of the createTroop() function), only one troop can use that sprite sheet at any given time (ie, when I create a new troop, it takes the sprite sheet from the previous troop, leaving that troop invisible).

    Can anybody think of a more efficient way to do this?

    Also, if anybody can suggest a way for me to create a copy of an array, that'd be great (since I create a big bi-dimensional array to push troops into every frame, and it'd probably be faster if I just created it once, and then made a copy of the original every frame).
    http://www.birchlabs.co.uk/
    You know you want to.

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    1 image should be able to be used for all Bitmap instances. Are you sure using the same image for another bitmap causes the first to go away?

  3. #3
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    ... to be more specific 1 *BitmapData* instance (riflemanRight or riflemanLeft) is needed for each bitmap. You will still need bitmaps for each troop

  4. #4
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    http://www.birchlabs.co.uk/Commando4OneBitmap.swf

    Yup, pretty sure.

    (This swf is what happens when I move the following lines:

    PHP Code:
    var rifleRight:RiflemanRight = new RiflemanRight(); 
               var 
    rifleRightBMP:Bitmap = new Bitmap(rifleRight); 
    and also the equivalent Left versions, outside of the createTroop() function so that they only get defined once).
    http://www.birchlabs.co.uk/
    You know you want to.

  5. #5
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    right, Bitmaps you need to have individual instances of, but not BitmapData - rifleRight can be alone, but rifleRightBMP you need for each troop

  6. #6
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Wow, that did the trick!

    http://www.birchlabs.co.uk/Commando4.swf
    http://www.birchlabs.co.uk/
    You know you want to.

  7. #7
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Also, are your right and left just reverses? if so, it might be better to do that through code as well.

  8. #8
    Senior Member
    Join Date
    Dec 2005
    Location
    No where
    Posts
    105
    why not make the troops extend Bitmaps? then you can just call super(bitmapdata)... bitmap has almost all of the same functions as sprite...

  9. #9
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    they have to use scrollRect()

  10. #10
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    scrollRect is a property of DisplayObject. Bitmap instances have it as well.
    Example:
    Attached Files Attached Files

  11. #11
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Is there a speed difference between Bitmaps and BitmapData?

    And the sprite sheets aren't a simple horizontal flip - they're actually a horizontal flip of each individual sprite (so the scroll script doesn't have to change).
    http://www.birchlabs.co.uk/
    You know you want to.

  12. #12
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Quote Originally Posted by VENGEANCE MX
    Is there a speed difference between Bitmaps and BitmapData?
    As fas as i know, you always have to use both, since you have to give some BitmapData to a Bitmap for it to work

  13. #13
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Quote Originally Posted by VENGEANCE MX
    Is there a speed difference between Bitmaps and BitmapData?
    Bitmaps are the display objects that exist in the display list. They are the "things" in Flash's memory which get thrown on the screen and displayed in the Flash player. The data that determines what is displayed is separated from that object and placed in another object called BitmapData. This object contains the pixel information that is used in Bitmap display objects. This is separated because BitmapData can be used in other places other than in Bitmap isntances such as filters. But for a Bitmap instance to show an image, it needs that BitmapData. They work together (as Cimmerian said) so differentiating speed differences really isnt an issue.

    Quote Originally Posted by VENGEANCE MX
    And the sprite sheets aren't a simple horizontal flip - they're actually a horizontal flip of each individual sprite (so the scroll script doesn't have to change).
    You wouldn't be flipping the sheets, but the Bitmap or sprite itself. This prevents any need to change the internal code, you're just flipping the whole thing. I'll update the example I posted earlier to show this
    Attached Files Attached Files

  14. #14
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Just skimming through your code, it looks like you're using scaleX to flip 'em. I got a massive lag when I tried using scaleX for the flips (was my original idea), lost about 10 fps.
    http://www.birchlabs.co.uk/
    You know you want to.

  15. #15
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    10 fps from flipping them? Thats unusual

  16. #16
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Lots of troops amplifies the lag somewhat.
    http://www.birchlabs.co.uk/
    You know you want to.

  17. #17
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Now the next speed boost will be put all the code inside a sprite that will be the documentClass
    Last edited by Cimmerian; 02-20-2007 at 06:00 AM.

  18. #18
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    :s how does that help?
    http://www.birchlabs.co.uk/
    You know you want to.

  19. #19
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Because when doing that you get rid of the movieClip wich is the stage class, mcs are slow cos they have a useless (in this case) timeline and besides that, they are dynamic and got to have internal hash to handle the new dynamic stuff

  20. #20
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    I think my Troop class used to extend Sprite instead of MovieClip. Dunno why I changed it originally, but now that I've replaces extends MovieClip with extends Sprite, I get this error:

    **Error** /Users/twinz/Desktop/Birchlabs/Commando 4/Troop.as : Line 5, Column 29 : [Compiler] Error #1017: The definition of base class Sprite was not found.
    public class Troop extends Sprite {


    Do I have to define the Sprite class somewhere or something?
    http://www.birchlabs.co.uk/
    You know you want to.

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