A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: Problems with draw()

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    8

    Problems with alpha

    hello, I'm very new to AS3.0 and I have problems with alpha. The next code is from my animation class and its main purpose is draw frame to other bitmapdata object.

    Code:
    // Copy the frame from original BitmapData.
    			destBitmapTempData.copyPixels(bitmapData, rect, zeroPoint);
    			
    			// Set the newly copied BitmapData to Bitmap.
    			modifiedBitmap.bitmapData = destBitmapTempData;
    			
    			// Change alpha
    			modifiedBitmap.alpha = 0.5;
    			
    			// Get Matrix from the modifiedBitmap.
    			matrix = modifiedBitmap.transform.matrix;
    			
    			// Change it if necessary
    			if (flipHor)
    			{
    				matrix.scale( -1, 1);
    				matrix.translate(width, 0);
    			}
    			if (flipVer)
    			{
    				matrix.scale( 1, -1);
    				matrix.translate(0, height);
    			}
    			
    			// Clear previous frame.
    			destBitmapData.fillRect(zeroRect, 0x00000000);
    			
    			// Draw the new frame.
    			destBitmapData.draw(modifiedBitmap, matrix);
    	
    			// Copy the new frame to canvas.
    			canvasBD.copyPixels(destBitmapData, zeroRect, position, null, null, true);
    The thing is that when I use copyPixels, the frame loses its alpha value. For example if I do new bitmap object from destBitmapData and addChild it, then the alpha remains, but not when I use copyPixels

    The canvasBD is created:

    Code:
    canvasBD = new BitmapData(screenRect.width, screenRect.height, true, 0x00FFFFFF);
    Any ideas how do I get alpha remain to that frame?
    Last edited by kadaAS3; 12-01-2008 at 06:16 AM. Reason: wrong topic

  2. #2
    Developer dVyper's Avatar
    Join Date
    Oct 2008
    Location
    UK
    Posts
    168
    Interesting, why have you set the fill colour for the bitmapdata as light blue? You should set it to white 0xFFFFFF really...

  3. #3
    Junior Member
    Join Date
    Dec 2008
    Posts
    8
    hmm, fill colour should be white and completely transparent first 00 hexs are for alpha and last 6 are for color and therefore 0x00FFFFFF should be completely white transparent or am I mistaken?

  4. #4
    Developer dVyper's Avatar
    Join Date
    Oct 2008
    Location
    UK
    Posts
    168
    Have you tried changing it to 0xFFFFFFFF?

  5. #5
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    0xFFFFFFFF is white, and opaque. You had it right the first time.

    I'm sure you've checked this, but the only advice I can give right this second is to make sure that when you declare both bitmapData objects, that you set the last/second last(?) parameter to true (alpha)...check the constructor for it, and you'll know what I mean.

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  6. #6
    Junior Member
    Join Date
    Dec 2008
    Posts
    8
    yep, both bitmapData objects are:

    Code:
    this.destBitmapData = new BitmapData(frameWidth, frameHeight, true);
    this.destBitmapTempData = new BitmapData(frameWidth, frameHeight, true);
    And as I said before, the funny thing is if I do:

    Code:
    // Draw the new frame.
    destBitmapData.draw(modifiedBitmap, matrix);
    
    // This works, alpha remains.
    modifiedBitmap.bitmapData = destBitmapData;
    Game.game.addChild(modfiedBitmap);
    
    //  and this doesn't, no more alpha.
    canvasBD.copyPixels(destBitmapData, zeroRect, position, null, null, true);
    So if I don't copyPixels and I just add it to child, then it works as it should, so one fix would be that I make Bitmap object for every objects, add them to child and modify their bitmapData objects if necessary.

    But the thing is that I would like to use one bitmap object as a canvas for the game and all the graphics are copied using copyPixels to that rather than have a multiple bitmaps and therefore multiple childs to deal with. Not sure if this a great way to do things though

  7. #7
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    It sure is (I think)...the draw() method is WAY slower than copyPixels()

    Well, I'll have a look into it tonight, but I'm using as2...then again, the BitmapData class barely changed.
    What's zeroRect, and those nulls, and what's canvasBD ?
    As far as I see with the nulls, that could be your problem. If I'm right (I don't have Flash at the moment), you need to pass the Alpha channel bitmap? In fact, (even though that shouldn't be your problem), try passing the same bitmap for the alpha channel. At first I was just going to say you need to pass new BitmapData(), new Rectangle() for the 2 nulls, but try doing the bitmap first.

    This is really strange...

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  8. #8
    Junior Member
    Join Date
    Dec 2008
    Posts
    8
    canvasBD = new BitmapData(screenRect.width, screenRect.height, true);

    then I have canvasBitmap which is the only bitmap I add to child of the main class.

    zeroRect (better name would be frameRect ) = new Rectangle(0, 0, frameWidth, frameHeight);

    this is only the source rectangle for copyPixels() where to copy the pixels.

    those nulls are alphaBitmapData:BitmapData and alphaPoint:Point.

    Well, I'll try to do something

    Btw. is it possible to change matrix without draw() method?

  9. #9
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    can you post the fla?

    maybe with test gfx.
    lather yourself up with soap - soap arcade

  10. #10
    Junior Member
    Join Date
    Dec 2008
    Posts
    8
    I use FlashDevelop and Flex sdk 3 so I have only source code (.as files + assets), but yes, I can share the source code but it'll have to wait until my work day is finished.

  11. #11
    Junior Member
    Join Date
    Dec 2008
    Posts
    8
    I noticed that if the source image has already some transparency then it works, so I guess the problem is when you change alpha in code it doesn't copy that?

  12. #12
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Ermmm, copypixels() does exactly that, it copies the pixels and sticks it to the other bitmapdata. No fancy effects. That's why it's a lot faster then draw().

    If you want to use transparancy you should use draw with a colortransform.

  13. #13
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    That's one of the things I hate about BitmapData's...only way to change it is to do the draw() method pointing to itself, and then applying the new matrix...

    I still have no idea...hopefully I'll be able to see from your source code!

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  14. #14
    Junior Member
    Join Date
    Dec 2008
    Posts
    8
    Quote Originally Posted by TOdorus
    Ermmm, copypixels() does exactly that, it copies the pixels and sticks it to the other bitmapdata. No fancy effects. That's why it's a lot faster then draw().

    If you want to use transparancy you should use draw with a colortransform.
    GREAT! colorTransform did it! Problem solved, maybe I should have talked about transparency and not alpha and this could have been solved a long time ago but hey thanks to everybody!

    I promise to share my game when it's ready for you guys! It will be like Metroid style game but a lot simplier

  15. #15
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    But...are you planning to do this in real-time? BitmapData.draw() is really slooow...
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  16. #16
    Developer dVyper's Avatar
    Join Date
    Oct 2008
    Location
    UK
    Posts
    168
    It's not really slooow - it's just slower than bitmap rendering. It's no more slower than drawing with normal vectors.

  17. #17
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Quote Originally Posted by dVyper
    It's no more slower than drawing with normal vectors.
    Well that depends. A vector graphic takes more power to render when there are more vectors. A bitmap graphic takes more power to render when there are more pixels. That's kinda comparing pears and apples.

  18. #18
    Junior Member
    Join Date
    Dec 2008
    Posts
    8
    Well, I use draw method only for matrix and I could do some improvements to that but it's not that slow, not my computer at least. Works smooth in full-screen mode

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