Created
July 11, 2013 08:26
-
-
Save Tomyail/5973584 to your computer and use it in GitHub Desktop.
幸运转盘
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flash.display.DisplayObject; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.geom.Point; | |
import flash.utils.getTimer; | |
class GamblingCircle | |
{ | |
private var path:Vector.<Point>; | |
private var items:Vector.<DisplayObject>; | |
private var target:DisplayObject; | |
private var sprite:Sprite; | |
private const INIT_SPEED:int = 300; | |
private var currentStatus:int = 1; | |
//加速状态 | |
private const INCREASE:int = 1; | |
//减速状态 | |
private const DECREASE:int = -1; | |
//减速到最后找目标状态 | |
private const FIND:int = 2; | |
private var lstUpdateTime:Number = 0; | |
private var currentIndex:int = 0; | |
private var currentDelta:Number = 0; | |
private const ACC_UP_SPEED:int = 10; | |
private const ACC_DOWN_SPEED:int = 5; | |
/** | |
* | |
* @param items | |
* @param target | |
*/ | |
public function GamblingCircle(items:Vector.<DisplayObject>, target:DisplayObject):void | |
{ | |
this.items = items; | |
this.target = target; | |
sprite = new Sprite(); | |
path = new <Point>[]; | |
for (var i:int = 0; i < items.length; i++) | |
{ | |
path[i] = new Point(items[i].x, items[i].y); | |
} | |
target.visible = false; | |
} | |
public function start():void | |
{ | |
sprite.addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
lstUpdateTime = getTimer(); | |
currentDelta = INIT_SPEED; | |
target.visible = true; | |
currentStatus = INCREASE; | |
} | |
private function onEnterFrame(event:Event):void | |
{ | |
var t:Number = getTimer(); | |
updateCurrentDelta(); | |
if (t - lstUpdateTime > currentDelta) | |
{ | |
lstUpdateTime = t; | |
updateCurrentIndex(); | |
pinTo(currentIndex); | |
if (currentStatus == FIND) | |
{ | |
//最后几个速度再次降低,是效果更真实.. | |
if (targetIdx - currentIndex < 3) | |
currentDelta += ACC_DOWN_SPEED * 5; | |
if (currentIndex == targetIdx) | |
stop(); | |
} | |
} | |
} | |
private function stop():void | |
{ | |
sprite.removeEventListener(Event.ENTER_FRAME, onEnterFrame); | |
callback(); | |
} | |
private function updateCurrentDelta():void | |
{ | |
//加速 | |
if (currentStatus == INCREASE) | |
{ | |
currentDelta -= ACC_UP_SPEED; | |
if (currentDelta <= 0) | |
currentDelta = 0; | |
} | |
//减速 | |
else if (currentStatus == DECREASE) | |
{ | |
currentDelta += ACC_DOWN_SPEED; | |
if (currentDelta >= INIT_SPEED) | |
{ | |
currentDelta = INIT_SPEED; | |
currentStatus = FIND; | |
} | |
} | |
} | |
private function updateCurrentIndex():void | |
{ | |
currentIndex++; | |
if (currentIndex >= path.length) | |
currentIndex = 0; | |
} | |
private function pinTo(idx:int):void | |
{ | |
target.x = path[idx].x; | |
target.y = path[idx].y; | |
} | |
private var targetIdx:int = 0; | |
private var callback:Function; | |
public function stopToIndex(idx:int, stopCallback:Function):void | |
{ | |
this.targetIdx = idx; | |
this.callback = stopCallback; | |
currentStatus = DECREASE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment