A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [F8] Scaling Bitmaps

  1. #1
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513

    [F8] Scaling Bitmaps

    Hi,

    It's been a while since I've used Flash, and so I'm still stuck in the past trying to get comfortable with AS2.0 again. I'm trying to find out how to scale a bitmap image through ActionScript, but since AS3.0 is so dominant I cant find anything useful through google.

    Basically, I'm trying to keep things as dynamic as possible, and so I'd like to use random sizes for my objects (in a game). I've found that to keep the framerate high I'll need to copypixel my objects onto screen, and then wipe them (with fillRect) every frame. This has worked like a charm, but I see no easy way to have objects of varing sizes without manually drawing differently sized 'reference bitmaps', which will take a long time (hence, i'd rather AS do this on start-up)!!

    Instead of the above method, I've read in some old threads where Iopred and some of the other oldies were talking about copyPixel'ing into movieclips and doing everything normally (ie. use _xscale/_yscale) but I don't understand how that would work???

    Here's a code snippet, if it helps
    PHP Code:
    _root.onEnterFrame = function():Void {
        
    add 0;                            // target frame (value = standard/rest frame);
        
    _root.bmp.fillRect(stageRect0);
        for (var 
    0allBalls.lengthi++) {
            
    //enterFrame () for balls
             
    var allBalls[i];
                
    b.lSpan++
                
    b.+= Math.sin((b.lSpan-b.y*5) / 100);
                if(
    b.y<Stage.height) {
                    
    b.+= 1;
                } else {
                    
    b.= -50;
                }
            if (
    b.lSpan%fR==0) {                        //change frame (move contents) frame-rate (40)/fR
                
    if (_root.add==0) {
                    if (
    b.frame 20) {
                        
    b.frame++;
                    } else if (
    b.frame 20) {
                        
    b.frame--;
                    }
                } else {
                    
    b.frame +=Math.round((_root.add-b.frame)*.5);                        
                }
            }
                
    b.rect.b.frame 55;        //55 = width of frame
                
    _root.bmp.copyPixels(myBitmapDatab.rect, new Point(b.x,b.y), nullnulltrue);
        
    //    }
        
    }
    }; 
    Thanks for any help...

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    they mean adding bitmapData's to movieclips, drawing the contents into those bitmapData's then scaling up their container movieClips.
    lather yourself up with soap - soap arcade

  3. #3
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513
    Quote Originally Posted by mr_malee View Post
    they mean adding bitmapData's to movieclips, drawing the contents into those bitmapData's then scaling up their container movieClips.
    yeah, that's how I understood it; but what I should've said is that I don't understand how this could be quick, since from my understanding the mc's are being re-drawn every frame as per usual(!?)

    I did try it out, and my suspisions were confirmed (it was doing 25 fps/40 at 100 objects). Here's some code that I made for your suggested method :
    PHP Code:
    var bmp = new BitmapData(5050true0x00000000);    //MUTE
    var allBalls:Array = new Array();        // array to remember all balls
    var ballCounter:Number 0;                // makes sure each ball gets a new name                                    //MUTE
    newBall(100);

    function 
    newBall(howMany:Number) {
        for (var 
    0howManyi++) {
            var 
    mc:Object = new Object();
            
    mc.Math.random()*Stage.width;
            
    mc._root.createEmptyMovieClip("i",i);
            
    mc.g.attachBitmap(bmp0);
            var 
    gwidth Math.random()*50 +50;
            
    mc.g._width gwidth;
            
    mc.g._height gwidth;
            
    mc.Math.random()*Stage.height;
              
    _root.allBalls.push(mc);
            
    mc.lSpan 0;        //timer for life span
            
    mc.frame 20;        //default (normal) frame
            
    mc.rect = new Rectangle(005050);
        }
    }; 
    aswell as changing the end of the previous post's code to this:
    PHP Code:
    b.rect.b.frame 55;        //55 = width of frame
                
    _root.bmp.copyPixels(myBitmapDatab.rect, new Point(0,0), nullnulltrue);
            
    b.g._x b.x;
            
    b.g._y b.y
    I hope it all makes sense. Even though it works, I've probably done something wrong (I'm new to all this bitmapdata stuff, if you can't tell). I'll attach the .fla if needed.

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    the only other options to scale a bitmap from memory:

    bitmapData.draw using a matrix
    graphics.bitmapFill using a matrix

    if you really want speed switch to AS3 and use Bitmap objects. Using MovieClips creates unnecessary overhead in both languages. If for some reason you really really... really really need to use AS2, make some array's of scaled bitmapData objects and use copy pixels to print them on screen.
    lather yourself up with soap - soap arcade

  5. #5
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513
    Thanks for your help, malee.

    I just read up on using a transformed matrix with draw() as you suggested and it seems like it will answer my initial question. So, at this point I figure at start-up I'll duplicate my gfx bitmap into several increasingly larger/smaller bitmaps, and use copyPixel onto a giant screen sized bitmap using these pre-made bitmaps.

    What I don't get is why there was talk in old threads of this movieclip method being better than what I'm doing??? Btw, I'm not using AS3.0 'cause I don't have the money the splash out on upgrading, and this is just a neat little mini-game anyway.

    cheers.

  6. #6
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    can you link to the threads? I know that in AS3 using Bitmaps can be better than blitting in certain situations. They could also be talking about rendering complex graphics at a smaller scale and then scaling up the container movieclip. Although this produces a more pixelated effect, it dramatically increases rendering performance. For example a realistic shader that simulates water or convolution filter.
    lather yourself up with soap - soap arcade

  7. #7
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513
    This was the thread I skimmed through: http://board.flashkit.com/board/show...ects+particles
    There was another one where Iopred was suggesting the same deal with mc's, but I cant find that one atm.

    Ages ago I read this one too: http://board.flashkit.com/board/showthread.php?t=732354 and tried my own tests in AS2.0. I found (from slowest to fastest):
    1. plain old mc's to be slowest - slooooooooooowww
    2. (as I've just found out) copying into mc's - 30fps/40 at 100 balls
    3. ScrollRect() - avg 37fps/40 at 100 objects; 15/40 at 200 objects... HOWEVER, this game me the nicest/easiest gfx, since it was all still vector art but cached
    4. CopyPixel() - no noticable lag with 200

    EDIT: reading through that first link I also see mention of scrolling being a nightmare when using a screensize bitmap to draw to, but I already found a way around that (though it may be a bit cpu intense, hence why I'm holding onto the idea of using mc's if it can perform better)
    Last edited by Viza; 09-29-2010 at 09:34 AM.

  8. #8
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Mc's can't perform better, so you solution is bitmap with transform matrix..... and switch to as3, atleast it will give much better performance compared to as2. Get yourself flashdevelop+flexSDK(tutorial on my blog), both are free, and are perfect for someone interested in programming rather than arts.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  9. #9
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Senocular's Transformation Matrix tutorial will teach you basically everything you could want to know about using the Bitmap in AS2 with the Matrix. Should at the very least point you in the right direction.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  10. #10
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513
    Thanks for all the tips guys. @bluemagica: I already use FD, but I haven't properly checked out Flex. Did some reading on it and it definately seems handy. I'm trying to get this game completed during holidays (so I might stick with as2 for now), but I think I'll do some other AS3 learning experiments with Flex. thanks.

    Thanks for the link IP. I got it all working now.

    Anyone know of a program which can convert a basic tween animation into a spritesheet??? I can probably make something that does it at start-up, but there's no way of keeping a copy of this outside of the .swf this way (ie. i'd like to have a seperate .bmp file for template spritesheets).

  11. #11
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Using AIR, it'd be very possible to simply force the animation to play frame-by-frame and copy each frame into a bitmap of a predetermined size and then save that image to the hard drive.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  12. #12
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513
    Y'know I was thinking about it, and maybe its best to just keep it all dynamicly created at start-up. Though it has its cons, I think the end result will look better 'cause I'm scaling vectors and then transforming it into bmp's. erghhh, the workload gets bigger and bigger, and I only need this stuff as an extra appeal to graphics (ie. its seperate from the gameplay).

    btw IP, you got me email right?

  13. #13
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Yep, got your email. Go ahead and shoot those files over to me I'll check that out sometime later.

    By the way, are you seriously going with a .bmp file format for your external sprite sheets? I think it'd be in your better interest to use something like .jpg or my personal favorite, .png.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  14. #14
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513
    No, I'm using .png's. Just said .bmp 'cause it would be less confusing I suppose.
    sent you those files btw

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