Search code examples
flashactionscript-3actionscriptflash-cs4

repositioning objects based on a scale


In my game, I have airplanes that are flying past the screen from a top down view. when the air planes crash, I scale them down to make it appear as if they are falling closer to the ground and farther away from the screen. I have turrets on these crafts as well. they are seperate objects from the air plan. I scale them down as well. The only problem is they dont reposition correctly. they stay in their x and y positions even though they are being scaled it looks as if they are being pulled away from the air plane. is it possible to reposition them correctly based on the size on the object they sit on (i.e. the air planes)

I just got a good answer from another post to place them into the movieclip itself. which is great but for the record, if anyone knows the answer to this, that would be great.


Solution

  • You should be keeping everything for each plane inside it's own MovieClip. But it's still useful to know how to do this without the builtin scaleX/scaleY. With your current setup, it can be done like so(where plane is your plane, scale is the factor (between 0-1) that you are going to scale the plane to, and turret is a turret that should be scale relative to the plane):

    function scalePlane(plane, turret, scale:Number):void {
    
        //scaling coordinates:
        var relativeX:Number = turret.x - plane.x;
        var relativeY:Number = turret.y - plane.y;
        var newRelativeX:Number = relativeX*scale;
        var newRelativeY:Number = relativeY*scale;
        turret.x = plane.x + newRelativeX;
        turret.y = plane.y + newRelativeY;
        //scaling size:
        turret.scaleX *= scale;
        turret.scaleY *= scale;
        plane.scaleX *= scale;
        plane.scaleY *= scale;
    }
    
    scalePlane(plane1, turret1, 0.9);
    //overall scale is 0.9;
    //next frame:
    scalePlane(plane1, turret1, 0.9);
    //now the overall scale is 0.81 because you are multiplying the scales
    

    I haven't tested that, but it should work as long as the plane and turret have the same parents. Note that scale in this case is relative, so you might have to change your calculations a bit. (if you were to call this code with scale 0.5 twice, the plane would then have a scale of 0.5*0.5 = 0.25)