0
votes

I'm drawing bitmaps of movieclips which I then feed into my hittest function to test for collisions. However, I'm not quite sure how i would add to the code below to take into account and draw bitmaps for movieclips which have been scaled and/or rotated. The below code obviously only works for non-transformed movieclips. I've included in comments code which i've already tried but to no success.

When adding the drawn bitmap to the stage, no matter whether the movieclip in question is transformed or not, the drawn bitmap is "cut off" and incorrectly drawn - it appears to only draw a section of it. However, this does not particularly affect the collision testing for the non-transformed movieclips, but has an adverse effect on transformed movieclips.

All of the movieclips I want to be drawn have been created through the graphics property.

           //for example: 
            var mymc:MovieClip = new MovieClip();
            var g:Graphics = mymc.graphics;                        
            g.moveTo(0,0);
            g.lineTo(17.5,0);
            g.lineTo(8.75,17.5);
            g.lineTo(-8.75,17.5);
            g.lineTo(0,0);

main code:

for each(var mc:MovieClip in impassable) {  

        //var bMatrix:Matrix = new Matrix();

        //bMatrix.scale(mc.scaleX, mc.scaleY);

        //bMatrix.rotate(mc.rotation * (Math.PI/180));

        var bData:BitmapData = new BitmapData(mc.width, mc.height, true, 0);

        //bData.draw(mc, bMatrix);

        bData.draw(mc);

        var bitmap:Bitmap = new Bitmap(bData);

        bitmap.x = mc.x;
        bitmap.y = mc.y;


        var HitTest:Number = newCollision(bitmap, centerX, centerY, 13.7);

Any thoughts? thanks

2
Is it possible to have a parent MC that you don't transform, and then use draw() to draw its contents?Amy Blankenship
@amy. Hmm i tried that before but it just seems to literally remove the child MC. I'm sure this can be done through Matrix, I'm just not sure how.Larry
My answer to this previous question has a code snippet which creates a Bitmap clone of a transformed DisplayObject. It sounds like it might do what you need? (Though obviously you can get rid of the extra step of positioning the Bitmap on the stage).David Mear
@DavidMear Many thanks for that in depth and thoroughly detailed piece of code. It seems to be exactly what is required in terms of making an exact copy of a transformed movieclip and aligning it correctly. However, despite a correct clone, the bitmaps still do not seem to work with my collision function.. anyway that would be for a different question if I can't figure that out. If you put this into an answer I will accept this as my answer. Thanks!Larry
Great! I've posted an answer, though if it's still not working I'd be interested if you added your collision function to the question. If you're trying the second version of the function in my previous answer, it rescales the bitmap to cancel out stage scaling, so that might be causing the problem.David Mear

2 Answers

1
votes

This function will create a BitmapData clone of a DisplayObject, taking into account its transform matrix, though it doesn't take into account bitmap filters. (Based on this answer.)

function createBitmapClone(target:DisplayObject):BitmapData {
    var targetTransform:Matrix = target.transform.concatenatedMatrix;
    var targetGlobalBounds:Rectangle = target.getBounds(target.stage);
    var targetGlobalPos:Point = target.localToGlobal(new Point());

    // Calculate difference between target origin and top left.
    var targetOriginOffset:Point = new Point(targetGlobalPos.x - targetGlobalBounds.left, targetGlobalPos.y - targetGlobalBounds.top);

    // Move transform matrix so that top left of target will be at (0, 0).
    targetTransform.tx = targetOriginOffset.x;
    targetTransform.ty = targetOriginOffset.y;

    var cloneData:BitmapData = new BitmapData(targetGlobalBounds.width, targetGlobalBounds.height, true, 0x00000000);
    cloneData.draw(target, targetTransform);

    return cloneData;
}
0
votes

When you call successive transforms on a Matrix, the ordering is very important and can really mess things up.

Luckily there is a helper method that allows you to specify translation, rotation and scaling in one go and avoid those issues - createBox

For your case, something like this:

var matrix:Matrix = new Matrix();
matrix.createBox(mc.scaleX, mc.scaleY, mc.rotation*Math.PI/180, 0, 0);

(the two zeros are for x and y translation)