AS3 - 以惯性或动量开始拖动?

问题描述 投票:0回答:1

还在学习Adobe Animate AS3,所以请多关照。 我想创建一个“滑动删除”类型的操作。请问有什么方法可以增加惯性或动量吗?当我释放对象时,它就停止了。 不知道,为了改变,谷歌没有提供任何帮助。 任何指针都会很棒。 TIA

import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

var savedY:Number = 0;
var delta:Number = 0;
var scrollArea:Number = 0;

stage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging2, true);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging2, true);
 
function startDragging2(e:MouseEvent) {
    mcMsg2c.startDrag(false, new Rectangle (-1800,400,1400,00));
}
 
function stopDragging2(e:MouseEvent) {
    mcMsg2c.stopDrag();

        stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging2, true);
        stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging2, true);
        stop();
}
actionscript-3
1个回答
0
投票

欢迎,追求知识的新灵魂。

当然,可以为您拖动的对象添加任何类型的缓动,您只需要内置的 startDrag(...) 方法以外的东西,它太简单且不可自定义。

所以,首先,让我们设置环境,将您指定的对象吸引到某个点。像重力一样。

import flash.geom.Point;
import flash.events.Event;
import flash.utils.getTimer;

// I want it to be short.
var M:DisplayObject = mcMsg2c;

// The attractor is placed onto the object, so it doesn't move at start.
var C:Point = new Point(M.x, M.y);

// To keep the current velocity of M.
var V:Point = new Point(0, 0);

// The most precise calculations we can get if we
// take the passing time into the account.
var T:int = getTimer();

// Acceleration and Friction multipliers, respectively.
// Altering those you can balance, tune or dis-balance the system.

// How stron the pull is.
var A:Number = 1;

// How much of the momentum is constantly lost. Must normally be between 0 and 1.
// 0 = no friction, will get out of balance eventually.
// 1 = full friction, all momentum is lost in 1 second.
// 2 = well, it won't hurt you to try and see!
var F:Number = 0.2;

// Let's calculate this simple physics model every frame.
addEventListener(Event.ENTER_FRAME, onFrame);

function onFrame(e:Event):void
{
    // Take the passed time into account.
    var aTime:int = getTimer();
    
    // The time delta from the last call — now in seconds.
    var aDiff:Number = (aTime - T) / 1000;
    
    // Keep the new time.
    T = aTime;
    
    // Calculate acceleration.
    var anAxel:Point = new Point;
    
    anAxel.x = (C.x - M.x) * aDiff * A;
    anAxel.y = (C.y - M.y) * aDiff * A;
    
    // Apply acceleration.
    V.x += anAxel.x;
    V.y += anAxel.y;
    
    // Apply friction.
    V.x *= 1 - F * aDiff;
    V.y *= 1 - F * aDiff;
    
    // Apply the calculated motion.
    M.x += V.x;
    M.y += V.y;
}

现在剩下的就是拖动这个虚拟点C,剩下的就让物理做吧。

import flash.events.MouseEvent;

stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

function onDown(e:MouseEvent):void
{
    // Track the mouse.
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
    stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
    
    // Initially fix the attractor.
    onMove(null);
}

function onMove(e:MouseEvent):void
{
    // Sync the attractor with the mouse.
    C.x = mouseX;
    C.y = mouseY;
    
    // Here you are free to apply any additional conditions and fix the coordinates.
    // Just remember that stage coordinates and local coordinated might be different.
}

function onUp(e:MouseEvent):void
{
    // Fix the attractor for the last time.
    onMove(null);
    
    // Stop tracking the mouse.
    stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
}

这个我没测试过,不过大致思路应该是对的,相信后面你就会明白了。祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.