views:

56

answers:

1

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.

+1  A: 

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)

Wallacoloo
thanks. I do keep everything inside the movieclip but it doesnt seem to listen to my turret class when i do it that way. should it be ? am I doing something wrong
numerical25
nevermind, I know why now. I have it set up in my game engine where there is only one loop used. and all classes use a updateObject method to update its status each frame. since I am putting my turrets inside the movieclip instead of adding them that way, its not able to work correctly
numerical25
Only reason why I did it this way is so I dont have to worry about destroying all displayobjects eventlisteners and loops. If I add a event listener in my turret class and add them directly to my plane movieclip. will they be destroyed when I destroy the plane
numerical25
it didnt work. just tried it out. is scale suppose to be the increment its being scaled by or the current scale size.
numerical25
I edited my post to show how you would use it to scale both coordinates and size.
Wallacoloo