我有一幀逐幀動畫。我希望能夠在舞臺上來回點擊和拖動並遍歷動畫。即我想單擊並從左向右拖動以使動畫向前並從右向左使動畫向後移動。單擊並拖動框架AS3
我該怎麼做到這一點?
我做了一個假設,將有一些數學計算鼠標位置和遍歷到正確的框架,但我怎麼會這樣做呢?
我有一幀逐幀動畫。我希望能夠在舞臺上來回點擊和拖動並遍歷動畫。即我想單擊並從左向右拖動以使動畫向前並從右向左使動畫向後移動。單擊並拖動框架AS3
我該怎麼做到這一點?
我做了一個假設,將有一些數學計算鼠標位置和遍歷到正確的框架,但我怎麼會這樣做呢?
這裏你(編輯後的版本)
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
var clickedMouseX:int;
var clickedFrame:uint;
var backgroundClip:Sprite = getChildByName("background") as Sprite;
var clip:MovieClip = getChildByName("animation") as MovieClip;
clip.stop();
clip.mouseEnabled = false;
backgroundClip.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
backgroundClip.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
function onMouseDown(event:MouseEvent):void
{
backgroundClip.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
clickedMouseX = backgroundClip.mouseX;
clickedFrame = clip.currentFrame;
}
function onMouseUp(event:MouseEvent):void
{
backgroundClip.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
function onMouseMove(event:MouseEvent):void
{
var delta:int = backgroundClip.mouseX - clickedMouseX;
var wantedFrame:uint = (clickedFrame + delta * clip.totalFrames/backgroundClip.width) % clip.totalFrames;
while (wantedFrame < 1)
{
wantedFrame += clip.totalFrames;
}
clip.gotoAndStop(wantedFrame);
}
乾杯!
現在看起來很簡單!謝了哥們!明天早上我回來工作時,我會給他一個禮物! – 2011-03-15 19:15:20
我在鼠標上添加了一個開始位置變量,並使用onMouseMove函數中的backgroundClip.mouseX值來計算兩者之間的差異。我如何解釋向後移動? – 2011-03-16 09:16:07
所以你有'var delta:Number = previousMouseX - backgroundClip.mouseX''?然後'var wantedFrame:uint = clip.currentFrame + delta * clip.totalFrames/backgroundClip.width;'?如果你想避免負數的幀數,你應該這樣寫:'var wantedFrame:int =(clip.currentFrame + delta * clip.totalFrames/backgroundClip.width)%clip.totalFrames;'所以當你的wantedFrame變負時,最後一幀可用。 – Kodiak 2011-03-16 09:45:42
這應該是你的拖拽區域的長度映射到時間線的長度的問題:
stage.addEventListener(MouseEvent.MOUSE_MOVE, updateAnimation);
function updateAnimation(event:MouseEvent):void {
animation.gotoAndStop(Math.floor(mouseX/stage.stageWidth * animation.totalFrames));
}
這裏有一個註釋版本:
stage.addEventListener(MouseEvent.MOUSE_MOVE, updateAnimation);
function updateAnimation(event:MouseEvent):void {
//take the ratio between the mouse position and stage width -> //a number from 0.0 to 1.0
var mouseRatio:Number = mouseX/stage.stageWidth;
//'scale'/multiply that number to fit the animation frames -> from a maximum of 1.0 to animation's total frames
//also, we 'round down' because we need an integer for the frame number, not a fractional number
var frame:int = Math.floor(mouseRatio * animation.totalFrames);
animation.gotoAndStop(frame);
}
而且,不是MOUSE_MOVE被觸發幾個幀每秒。你可以更新ENTER_FRAME因爲你提到拖,你也可以當鼠標按下或釋放有一個變量來跟蹤:
var mousePressed:Boolean;
stage.addEventListener(MouseEvent.MOUSE_DOWN, togglePressed);
stage.addEventListener(MouseEvent.MOUSE_UP, togglePressed);
stage.addEventListener(Event.ENTER_FRAME, update);
function togglePressed(event:MouseEvent):void {
mousePressed = event.type == MouseEvent.MOUSE_DOWN;
}
function update(event:Event):void {
if(mousePressed) animation.gotoAndStop(Math.floor(mouseX/stage.stageWidth * animation.totalFrames));
}
HTH
科迪亞克:反正是有釋放後,你可以添加一個減速功能,所以如果你輕彈拖動,那麼它會有一個補間效果,然後減慢速度。謝謝。 – 2011-08-02 05:33:18