13 Replies Latest reply on Dec 9, 2010 12:03 PM by kglad

# [as2] Rotating a movieclip in a given x,y point

Hi.

I´d like to know, how can I make the green movieclip rotate in the point that the claw has catch it.

To give an impression of physics.

Here´s the image, to give you an idea:

The point (x,y) that the claw catches the colored movieclips below is not unique it can be any number. As long as the claw catches the colored movieclip.

Thanks

• ###### 1. Re: [as2] Rotating a movieclip in a given x,y point

that's not easy in as2.

the easiest way would be to add (as a child) each captured shape to a unique parent (that you would then manipulate instead of the captured shape) and offset the child by the opposite of x and opposite of y and you would remove the original captured shape.

• ###### 2. Re: [as2] Rotating a movieclip in a given x,y point

Ok.

Suppose I know how to change the registration point of the movieclip.

How can I make it rotate according with the image, so the movieclip seems

to rotate like it is hanging by the claw.

Rotating right and left untill its speed of rotation reachs 0 and reachs its final rotation?

Do you know what I mean, it´s like physics.

Rotating right and left, because of the movieclip weight untill it finishes rotating, according with its new registration point

(which I know how to change it). What would be the formula?

Thanks

• ###### 3. Re: [as2] Rotating a movieclip in a given x,y point

i explained the easiest way to do that.  here it is in code.   create a movieclip p with a child movieclip c.  put it anywhere on the timeline that contains the code so it exists when the code executes and make it invisible.

you can then use the following code.  don't change anything except the commented line which you'll replace passing your movieclip instance name, x (relative to your movieclip, y (relative to your movieclip) and the rotation in degrees.

import flash.geom.*;

// rotationF(mc,mc._width,mc._height,33);  // sample usage

function rotationF(mc:MovieClip,x:Number,y:Number,rot:Number){
var mat:Matrix = rotationMatrixF(mc,x,y,rot);
mc.transform.matrix = mat;
}

function rotationMatrixF(mc:MovieClip,x:Number,y:Number,rot:Number):Matrix{
p._x = mc._x;
p._y = mc._y;
p._rotation = mc._rotation;
p.c._x=-x;
p.c._y=-y;
p._rotation = rot
p._x += x;
p._y += y;
return p.c.transform.concatenatedMatrix;
}

• ###### 4. Re: [as2] Rotating a movieclip in a given x,y point

Ok.

I´ll try.

• ###### 5. Re: [as2] Rotating a movieclip in a given x,y point

I´ve tried this:

import flash.geom.*;

function rotationF(mc:MovieClip, x:Number, y:Number, rot:Number)
{
var mat:Matrix = rotationMatrixF(mc, x, y, rot);
mc.transform.matrix = mat;
}
createEmptyMovieClip("p",0);
p.attachMovie("shape","c",1);
function rotationMatrixF(mc:MovieClip, x:Number, y:Number, rot:Number):Matrix
{
p._x = mc._x;
p._y = mc._y;
p._rotation = mc._rotation;
p.c._x = -x;
p.c._y = -y;
p._rotation = rot;
p._x += x;
p._y += y;
return p.c.transform.concatenatedMatrix;
}
go = false;
onEnterFrame = function ()
{
if (go)
{
rotationF(p.c,pointX,pointY,1);
}
};
onMouseDown = function ()
{
pointX = _xmouse;
pointY = _ymouse;
go=true
};

But the "c" movieclip doesn´t rotate in the right direction.

Have I made something wrong?

I´ve putted the code in _root

• ###### 6. Re: [as2] Rotating a movieclip in a given x,y point

p and c in my code are not displayed.  they are used to allow flash to do the calculations.  they shouldn't even be visible.

• ###### 7. Re: [as2] Rotating a movieclip in a given x,y point

Ok, I´ll try

• ###### 8. Re: [as2] Rotating a movieclip in a given x,y point

I´ve tried this:

import flash.geom.*;

function rotationF(mc:MovieClip, x:Number, y:Number, rot:Number)
{
var mat:Matrix = rotationMatrixF(mc, x, y, rot);
mc.transform.matrix = mat;
}
createEmptyMovieClip("p",0);
p.createEmptyMovieClip("c", 1)
attachMovie("shape", "shape", 2)
function rotationMatrixF(mc:MovieClip, x:Number, y:Number, rot:Number):Matrix
{
p._x = mc._x;
p._y = mc._y;
p._rotation = mc._rotation;
p.c._x = -x;
p.c._y = -y;
p._rotation = rot;
p._x += x;
p._y += y;
return p.c.transform.concatenatedMatrix;
}
go = false;

onEnterFrame = function ()
{
if (go)
{
rotationF(shape,pointX,pointY,shape._rotation+1);
}
};
onMouseDown = function ()
{
pointX = _xmouse;
pointY = _ymouse;
go=true
};

the shape rotates but goes outside stage

• ###### 9. Re: [as2] Rotating a movieclip in a given x,y point

pointX and pointY are supposed to be relative to the shape.  and that code works once.  for each movieclip.  you'll need to edit the code to have it work on repeat rotations.

• ###### 10. Re: [as2] Rotating a movieclip in a given x,y point

Why this doesn´t work?

import flash.geom.*;

function rotationF(mc:MovieClip, x:Number, y:Number, rot:Number)
{
var mat:Matrix = rotationMatrixF(mc, x, y, rot);
mc.transform.matrix = mat;
}
createEmptyMovieClip("p",0);
p.createEmptyMovieClip("c", 1)
attachMovie("shape", "shape", 2)
function rotationMatrixF(mc:MovieClip, x:Number, y:Number, rot:Number):Matrix
{
p._x = mc._x;
p._y = mc._y;
p._rotation = mc._rotation;
p.c._x = -x;
p.c._y = -y;
p._rotation = rot;
p._x += x;
p._y += y;
return p.c.transform.concatenatedMatrix;
}
go = false;

onEnterFrame = function ()
{
if (go)
{

rotationF(shape,pointX, pointY, shape._rotation+1);
}
};
onMouseDown = function ()
{
pointX = shape._xmouse;
pointY = shape._ymouse;
go=true
};

• ###### 11. Re: [as2] Rotating a movieclip in a given x,y point

that code works once for each movieclip.  you'll need to edit the code to have it work on repeat rotations.

• ###### 12. Re: [as2] Rotating a movieclip in a given x,y point

But I don´t know what I have to do on the code .

Each movieclip is "p", "c" and "shape"?

• ###### 13. Re: [as2] Rotating a movieclip in a given x,y point

here's a way that works with repeats:

function rotateF(mc:MovieClip, xreg:Number, yreg:Number, rot:Number) {
var a:Object = {x:xreg, y:yreg};
mc.localToGlobal(a);
mc._parent.globalToLocal(a);
mc._rotation = rot;
var b:Object = {x:xreg, y:yreg};
mc.localToGlobal(b);
mc._parent.globalToLocal(b);
mc._x -= b.x-a.x;
mc._y -= b.y-a.y;
}