Skip to main content
Known Participant
April 14, 2017
Question

Get BitmapData inside Sprite

  • April 14, 2017
  • 1 reply
  • 1078 views

Hi,

Following Problem:

I want to get the BitmapData from a Child Bitmap of a Sprite. I can get the Bitmap but not the BitmapData. I dont want to save it in an extra variable.

(BitmapData(flySprite.getChildAt(0).bitmapData)           // Doesnt Work

(Bitmap(flySprite.getChildAt(0))                                    // does Work but not with .bitmapData

How can I get the bitmapData without saving in a new Bitmap (that works...)? is there a way to call the BitmapData with "flySprite.flyBitmap.bitmapData"? How do i have to add the Bitmap, that this works?

my favorite solution would be not to use a Sprite at all, but i need the registrationpoint in center and after drawing the bitmap, the registration is in the top-left corner. I tried to chage it with

matrix.identity();
matrix.translate(-(flyBmp.width / 2), -(flyBmp.height / 2 ));

flyBmp.transform.matrix = matrix;

etc. pp.

It changed nothing... does the registration change back in every frame or something?

thanks, Megaloadon

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
April 14, 2017

what makes you think that child is a bitmap?

if it starts as a bitmap and is added to the stage using the ide, it's probably a shape. use trace(flySprite.getChildAt(0)) to confirm.

if you want it to remain a bitmap, assign it a bitmapdata class and then use actionscript to add it to the display, or add it to the display in the ide.

and registration points only make sense when discussing in the context of a parent. ie, the reg point is relative to the parent (and/or some other antecedent).

if you want to change a displayobjectcontainer's reg point, you can use:

function changeRegPt(dobj:DisplayObjectContainer,x:Number,y:Number){

    var r:Rectangle = dobj.getBounds(dobj);

    for(var i:uint=0;i<dobj.numChildren;i++){

        dobj.getChildAt(i).x -= r.x+x;

        dobj.getChildAt(i).y -= r.y+y;

    }

    dobj.x += r.x+x;

    dobj.y += r.y+y;

}

Known Participant
April 14, 2017

kglad

Thanks for the Answer! I know its a Bitmap, because I added it to the Sprite and draw it from the BitmapData of a MovieClip. The trace returns [object Bitmap] or sthg.

I also dont want to change the Registrationpoint of the Container. I centered it already when adding the bitmap child.

Im programming a minigame  and I found a running solution, but the game runs slow and chrashes after a moment. I dont understand why... I guess my code is inefficient.

would it be okay if I post some functions and let you have a look? I really have no idea why it keeps crashing

thank you for response!

Megaloadon

kglad
Community Expert
Community Expert
April 14, 2017

sure.  post the (hopefully not to extensive) functions.