如何编写 Flash 动作脚本来制作鼠标动作?
How to write the Flash actionscript to makes this mouse action?
我有点难以描述我需要的效果。
当我按下鼠标向上移动,然后松开,它会转到下一页 page.When 我按下鼠标并向下移动,然后松开,它会转到上一页。
这就像智能手机中的交换桌面。
我不太熟悉 Flash actionscript。
我有一个想法来实现这个效果,当我按下鼠标(MOUSE_DOWN)时,检测到鼠标的 Y 位置,当我释放鼠标时(MOUSE_UP),另一个 Y-检测到的位置。
当旧 Y > 新 Y 时,转到 prevFrame(),当新 Y > 旧 Y 时,转到 nextFrame()。
谁能帮我用actionscript写出来?我试了但是失败了,因为我真的看不懂脚本...
有几种方法可以实现您的需求,我建议您也查看一下 Touch/Gesture events, also, check this library Gestouch (very powerful and well documented and with several examples)
为了帮助您了解如何开始理解 ActionScript 和这个有问题的逻辑,我写了这个简单的例子,仅用于教育目的(不使用 touch/gesture 事件或适当的代码约定来命名等等),您可以根据您能够从我在此答案中提到的示例和相关链接中提取的内容应用您自己的逻辑和需求。
*
// control for Y position when mouse down
var downY:Number = 0;
// control for Y position when mouse up
var upY:Number = 0;
// control for X position when mouse down
var downX:Number = 0;
// control for X position when mouse up
var upX:Number = 0;
// vertical or horizontal control (if true, we use the Y position, if false we use the X)
var verticalControl:Boolean = true;
// adding an event listeter to detect the mouse down state (you can apply the same for your own Object (sprite with image, or whatever you have in mind)
stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler, false, 0, true);
function downHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, upHandler, false, 0, true);
// collecting current Y and X position (mouse down state)
downY = event.localY;
downX = event.localX;
}
function upHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler, false, 0, true);
// collecting current Y and X position (mouse up state)
upY = event.localY;
upX = event.localX;
// checking if should use Y or X position
(verticalControl) ? frameActionScope(upY, downY) : frameActionScope(upX, downX);
}
function frameActionScope(newValue:Number, oldValue:Number):void
{
// using your logic (When the old Y > new Y , go to prevFrame(), and when the new Y > old Y, go to nextFrame())
(newValue >= oldValue) ? nextFrameScope() : prevFrameScope();
}
function nextFrameScope():void
{
trace('next frame');
}
function prevFrameScope():void
{
trace('previous frame');
}
希望能对您有所帮助。
在 gPeart 发布的解决方案中,除了坐标之外,您还可以添加一些代码来捕获鼠标 press/release 的时间戳。这样,您就可以计算滑动的长度、方向和速度。对于非常低的速度或非常小的距离,您可以忽略滑动;让它感觉它只响应真正的滑动。
我有点难以描述我需要的效果。 当我按下鼠标向上移动,然后松开,它会转到下一页 page.When 我按下鼠标并向下移动,然后松开,它会转到上一页。 这就像智能手机中的交换桌面。 我不太熟悉 Flash actionscript。 我有一个想法来实现这个效果,当我按下鼠标(MOUSE_DOWN)时,检测到鼠标的 Y 位置,当我释放鼠标时(MOUSE_UP),另一个 Y-检测到的位置。 当旧 Y > 新 Y 时,转到 prevFrame(),当新 Y > 旧 Y 时,转到 nextFrame()。 谁能帮我用actionscript写出来?我试了但是失败了,因为我真的看不懂脚本...
有几种方法可以实现您的需求,我建议您也查看一下 Touch/Gesture events, also, check this library Gestouch (very powerful and well documented and with several examples)
为了帮助您了解如何开始理解 ActionScript 和这个有问题的逻辑,我写了这个简单的例子,仅用于教育目的(不使用 touch/gesture 事件或适当的代码约定来命名等等),您可以根据您能够从我在此答案中提到的示例和相关链接中提取的内容应用您自己的逻辑和需求。
*
// control for Y position when mouse down
var downY:Number = 0;
// control for Y position when mouse up
var upY:Number = 0;
// control for X position when mouse down
var downX:Number = 0;
// control for X position when mouse up
var upX:Number = 0;
// vertical or horizontal control (if true, we use the Y position, if false we use the X)
var verticalControl:Boolean = true;
// adding an event listeter to detect the mouse down state (you can apply the same for your own Object (sprite with image, or whatever you have in mind)
stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler, false, 0, true);
function downHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, upHandler, false, 0, true);
// collecting current Y and X position (mouse down state)
downY = event.localY;
downX = event.localX;
}
function upHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler, false, 0, true);
// collecting current Y and X position (mouse up state)
upY = event.localY;
upX = event.localX;
// checking if should use Y or X position
(verticalControl) ? frameActionScope(upY, downY) : frameActionScope(upX, downX);
}
function frameActionScope(newValue:Number, oldValue:Number):void
{
// using your logic (When the old Y > new Y , go to prevFrame(), and when the new Y > old Y, go to nextFrame())
(newValue >= oldValue) ? nextFrameScope() : prevFrameScope();
}
function nextFrameScope():void
{
trace('next frame');
}
function prevFrameScope():void
{
trace('previous frame');
}
希望能对您有所帮助。
在 gPeart 发布的解决方案中,除了坐标之外,您还可以添加一些代码来捕获鼠标 press/release 的时间戳。这样,您就可以计算滑动的长度、方向和速度。对于非常低的速度或非常小的距离,您可以忽略滑动;让它感觉它只响应真正的滑动。