在AS3中使用摆动/倾斜效果摇动对象

时间:2014-12-17 15:21:54

标签: actionscript-3 flash effect tween shake

我在舞台上有一个瓶子的图像。我想拥有它,以便用户可以点击并拖动瓶子。我希望瓶子随着它的拖动而摇摆,因此倾斜和旋转就好像它来回拉动一样。我用Google搜索了一下,什么也没发现。如何通过代码实现这种效果?

到目前为止,我已经创建了一个计时器,用于跟踪拖动过程中的起点和终点,计算行进距离以大致了解速度和方向,然后使用Tween类将瓶子旋转到120度范围内(之后反弹回到0)。它很接近但不像我喜欢的那样流畅。我觉得我需要某种自定义缓动功能,但我对这种类型的数学并不是很好。

function distanceTimerBeat(e:TimerEvent):void
{//speed detection beat timer
distCounter++;
if(!distSwitch)
{
    startX = product.x;
    startY = product.y;
    distSwitch = true;
}
else
{
    endX = product.x;
    endY = product.y;
    distSwitch = false;
    var newDistance = calculateDistance();
    setRotation();
    //trace('>> ('+distCounter+') distance travelled = '+newDistance);
    //produceNewFirework()
    }   
}

function calculateDistance():Number
{//returns distance travelled over beat timer intervals

var rawDistance = (( startX - endX ) * (startX - endX )) + ((startY - endY ) * (startY - endY ));
var distance = Math.sqrt( rawDistance);
var xDistance = startX - endX;
if(xDistance < 0)
{
    speedDirection = 'right';
    directionModifier = -1;
}
else if(xDistance > 0)
{
    speedDirection = 'left';
    directionModifier = 1;
}

//trace('>> going '+speedDirection+' at '+xDistance);
return distance;
}

function setRotation():void
{ //once start & end values are detected, check speed and set the rotation.

var curSpeed = calculateDistance();

if(curSpeed > 4)
{
    tweenLock = false;


    var speedPercent = curSpeed/maxSpeed * 100;

    var rotationDegree = (speedPercent*120)/300;
    var newRotation = directionModifier*rotationDegree;


    var reversePercent = 100 - speedPercent;
    var tweenSpeedModifier = (speedPercent/100);
    //trace(tweenSpeedModifier*1);

    //if(!tweenLock)
        //rotationTween = null;

        if(rotationTween) rotationTween.stop();
        rotationTween = new Tween(product, "rotation", Strong.easeInOut, product.rotation, newRotation, 0.25, true);
        //rotationTween.resume();
        rotationTween.addEventListener(TweenEvent.MOTION_FINISH, resetProductTween);
        //trace('speed percet = '+speedPercent);

}
else
{
    //tweenLock = true;
}
}

function resetProductTween(e:TweenEvent)
{//after the product rotates, snap it back to it's original position.
tweenLock = false;
if(!tweenLock)
{
    rotationTween = new Tween(product, "rotation", Strong.easeOut, product.rotation, 0, 0.75, true);
    rotationTween.addEventListener(TweenEvent.MOTION_FINISH, tweenUnlock);
}
}

1 个答案:

答案 0 :(得分:1)

http://code.tutsplus.com/tutorials/quick-tip-trigonometry-for-flash-game-developers--active-4458

stage.addEventListener(MouseEvent.CLICK, calculateAngle);

var myAtan2:Number;

function calculateAngle(e:MouseEvent):void
{
    myAtan2 = Math.atan2(e.stageY - mCircle.y, e.stageX - mCircle.x);
    trace(myAtan2);
}