点击并通过框架AS3拖 [英] Click and Dragging through frames AS3

查看:111
本文介绍了点击并通过框架AS3拖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一帧一帧动画。我希望能够通过动画点击并在舞台上来回拖动和移动。即我想点击,并从左至右,使动画向前走,并从右向左拖动,使动画倒退。

I have a frame-by-frame animation. I want to be able to click and drag on the stage back and forth and traverse through the animation. I.e. I want to click and drag from left to right to make the animation go forwards and right to left to make the animation go backwards.

我将如何实现这一目标?

How would I achieve this?

我想提出一个假设,即会有一些数学参与计算鼠标位置,并穿越到正确的框架,但我将如何做到这一点?

I am making an assumption that there will be some maths involved in calculating mouse position and traversing to the correct frame, but how would I do this?

推荐答案

在这里,您有(编辑的版本)

Here you are (edited version)

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);
}

干杯!

这篇关于点击并通过框架AS3拖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆