我正在尝试使用移动的指针使标准的命运之轮旋转。我可以根据需要使用补间动画来使车轮旋转和着陆,但是我仍然坚持如何获得一个准确而令人信服的选择指针,该指针随车轮上的每个销钉移动而移动。
解决此问题的最佳方法是什么?有没有一种简单的方法可以使用基于物理学的代码做到这一点,或者还有另一种方法可以尝试伪造它?在任何可以解释这种简单行为的教程中,我都找不到。
我最初尝试在每个钉上运行一个for循环以检查是否发生碰撞,然后告诉该点自己成角度指向钉。这会产生两个问题:
•车轮旋转太快,完全跳过了碰撞。如果车轮在Y轴上旋转了X度,则需要编写代码来伪造一个命中项。
•发生碰撞时,指针具有厚度。它的连接点会根据其被击中或被检测到的位置而偏离中心,并且会发生对齐以及钉子推指针的令人信服的效果(会有一些重叠或其他视觉问题)
package {
public class Main extends MovieClip {
var numSegment: Number;
var numResult: Number;
var numRotation: Number;
var spinTime: Number;
var pointerMax: Number;
public function Main() {
// constructor code
menu_mc.spin_btn.addEventListener(MouseEvent.CLICK, spinFunc);
}
public function spinFunc(e: Event): void {
//reset wheel
wheel_mc.rotation = 0;
//parse values from menu
numSegment = parseInt(menu_mc.segment_txt.text);
numResult = parseInt(menu_mc.result_txt.text);
numRotation = parseInt(menu_mc.rotation_txt.text);
spinTime = parseInt(menu_mc.time_txt.text);
//Find angle of desired segment and randomize within the range
var segAngle: Number = 360 / numSegment;
var myResult: Number = -(numResult - 1) * (segAngle);
var randAngle: Number = Math.random() * segAngle;
myResult += randAngle - (segAngle / 2);
function startSpin(): void {
var myTween: Tween = new Tween(wheel_mc, "rotation", Regular.easeInOut, wheel_mc.rotation, 20, 1, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, endSpin);
}
function endSpin(): void {
var myTween_spin: Tween = new Tween(wheel_mc, "rotation", Regular.easeInOut, wheel_mc.rotation, -((360 * numRotation) - myResult), spinTime - .4, true);
}
pointer_mc.addEventListener(Event.ENTER_FRAME, hitCheck);
function hitCheck(e: Event): void {
var dx: Number;
var dy: Number;
var trueX: Number;
var trueY: Number;
var angle: Number;
var hitDetect: Boolean = false;
for (var i: Number = 1; i < numSegment + 1; i++) {
trueX = wheel_mc["hitBox_" + i].localToGlobal(new Point(0, 0)).x;
trueY = wheel_mc["hitBox_" + i].localToGlobal(new Point(0, 0)).y;
if (pointer_mc.hitTestPoint(trueX, trueY, true)) {
hitDetect = true;
dx = trueX - pointer_mc.x;
dy = trueY - pointer_mc.y;
}
}
if (hitDetect == true) {
//Store angle in angle variable
angle = Math.atan2(dy, dx) * 180 / Math.PI;
//Apply angle
pointer_mc.rotation = angle - 90;
} else {
if ((pointer_mc.rotation - 2) >= 0) {
pointer_mc.rotation -= 2;
} else {
pointer_mc.rotation = 0;
}
}
}
startSpin();
}
}
}