旋转车轮并在EaselJs的特定点停车

问题描述 投票:0回答:1

我是EaselJs的新手。我正在旋转一个9x数字和3(0x)的轮子,总共12个数字。我可以通过调用函数来旋转滚轮,但我想在预定义的特定点/轮数上停止它。

var _oContainer;

this._init = function(iXPos,iYPos){
    _oWheel = s_oSpriteLibrary.getSprite("wheel_circle");
    _oContainer = new createjs.Container;
    _oContainer.x = CANVAS_WIDTH - 200;
    _oContainer.y = CANVAS_HEIGHT - 350;
    s_oStage.addChild(_oContainer);
    img = createBitmap(_oWheel);
    img.regX = _oWheel.width / 2;
    img.regY = _oWheel.height / 2;
    _oContainer.addChild(img);
}

this.spin = function(b, a){
    //var h = new createjs.Bitmap(s_oSpriteLibrary.getSprite('wheel_circle'));
    createjs.Tween.get(_oContainer).to({
        rotation: _oContainer.rotation + a
    }, 2600, createjs.Ease.quartOut).call(function() {
        _oContainer.rotation %= 360;
    })
}

我在每次点击按钮时都将旋转功能称为this.spin(5, 1131.7511808994204);

现在它正在旋转并在每次点击按钮时随机停止。如何在车轮上的特定数字/位置停止?

我应该在rotation:中给出什么价值?

javascript createjs easeljs
1个回答
0
投票

做这样的事情有很多因素在起作用。我做了一个快速演示来展示我将如何做到:

  1. 用段绘制轮子(在中心)。重要的是要知道你有多少段,这样你就可以选择一个“结束”的地方
  2. 开始旋转。只需根据您想要的速度增加每个刻度。
  3. 当“停止”时,你必须做数学来确定降落的位置
  4. 要获得逼真的“减速”,请确保补间中的剩余旋转足够,以便它不会加速或减速太快

这是小提琴:https://jsfiddle.net/lannymcnie/ych1qt8u/1/

// Choose an end number. In this case, its just a number between 0 and the number of segments.
var num = Math.random() * segments | 0,

    // How many degrees is one segment? 
    // In my demo I use radians for `angle`, so I have to convert to degrees
    angleR = angle * 180/Math.PI,

    // Take the current rotation, add 360 to it to take it a bit further
    // Note that my demo rotates backwards, so I subtract instead of add.
    adjusted = c.rotation - 360,

    // Determine how many rotations the wheel has already gone since it might spin for a while
    // Then add the new angle to it (num*angleR)
    // Then add a half-segment to center it.
    numRotations = Math.ceil(adjusted/360)*360 - num*angleR - angleR/2;

然后我只是运行一个补间到新的位置。您可以玩持续时间,轻松获得自己喜欢的东西。

createjs.Tween.get(c)
    .to({rotation:numRotations}, 3000, createjs.Ease.cubicOut);

从技术上讲,我应该根据实际的剩余旋转来改变持续时间,因为根据结果,它可能不是非常平滑的。这很接近,所以我原样离开了。

希望有所帮助!如果我能进一步澄清,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.