I’ve been experimenting with a crank that you can turn with the mouse which is supposed to make things gradually change on the stage. Everything is going smoothly, except that when the crank makes a full rotation, its angle returns to 0. I’ve added an AccumulatedAngle variable to keep track of this, but I can’t get it to update at the same time as the actual rotation. What I’m getting is brief moments where the totalAngle value is 360 above what it should be, and the objects on screen update with that value, causing them to jump around on the screen and then jump back each time the crank makes a full rotation. I’ve tried moving around the order of the variable updates and I’ve tried moving when the boxes are updated, even putting them inside the if statements that change the crank’s angle. I just can’t figure this one out. Any help would be greatly appreciated. Also, if it turns out that there is a more efficient way to do the rotation for the crank rather than using three if statements, that would be awesome as well.
addEventListener(Event.ENTER_FRAME,startFunction);
HandleGrab.addEventListener(MouseEvent.MOUSE_DOWN, grabHandle);
HandleGrab.addEventListener(MouseEvent.MOUSE_UP, releaseHandle);
var TurnCrank = false;
var TotalAngle = 0;
var AccumulatedAngle = 0;
var CurrentAngle = 0;
var prevRotate = 0;
function startFunction(event:Event)
{
if(TurnCrank == true)
{
HandleGrab.rotation = 0;
HandleGrab.x = mouseX;
HandleGrab.y = mouseY + 90;
var Angle = Math.atan((Crank.y - mouseY) / (mouseX - Crank.x)) / (Math.PI / 180);
if(Crank.y - mouseY >= 0 && mouseX - Crank.x >= 0)
{
prevRotate = Crank.rotation;
Crank.rotation = -Angle+90;
checkRotate();
}
if(Crank.y - mouseY <= 0 && mouseX - Crank.x >= 0)
{
prevRotate = Crank.rotation;
Crank.rotation = -Angle-270;
checkRotate();
}
if((Crank.y - mouseY >= 0 && mouseX - Crank.x <= 0) || (Crank.y - mouseY <= 0 && mouseX - Crank.x <= 0))
{
prevRotate = Crank.rotation;
Crank.rotation = -Angle-90;
checkRotate();
}
}//end if(TurnCrank = true)
else
{
HandleGrab.x = Crank.x;
HandleGrab.y = Crank.y;
HandleGrab.rotation = Crank.rotation;
}//end else
if(Crank.rotation < 0)
{
CurrentAngle = 360 + Crank.rotation;
}
else
{
CurrentAngle = Crank.rotation;
}
Box1.y = 600 - (TotalAngle/4);
Box2.x = TotalAngle/3;
Box3.rotation = TotalAngle/20;
if(AccumulatedAngle >= 2160)
{
Jack.gotoAndStop(2);
}
}//end startFunction
function grabHandle(event:MouseEvent)
{
TurnCrank = true;
}//end grabHandle
function releaseHandle(event:MouseEvent)
{
TurnCrank = false;
}//end releaseHandle
function checkRotate()
{
if(Crank.rotation >= 0 && Crank.rotation <= 50)
{
if(prevRotate < 0)
{
AccumulatedAngle += 360;
}
}
if(Crank.rotation < 0 && Crank.rotation >= -50)
{
if(prevRotate >= 0)
{
AccumulatedAngle -= 360;
}
}
TotalAngle = AccumulatedAngle + CurrentAngle;
}