AS3 – startDrag带有惯性或动量吗?

huangapple go评论99阅读模式
英文:

AS3 - startDrag with inertia or momentum?

问题

还在学习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, 0));
}
 
function stopDragging2(e:MouseEvent) {
	mcMsg2c.stopDrag();

	stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging2, true);
	stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging2, true);
	stop();
}
英文:

Still learning Adobe Animate AS3, so please be kind.
I would like to create a "swipe to delete" type action. Is there any way of adding some inertia or momentum please? When I relaease the object it just stops.
No idea, and for a change Google isn't offering any help.
Any pointers would be awasome.
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();
}

答案1

得分: 1

以下是您要翻译的内容:

"Welcome, the new soul in a pursuit of knowledge."
"这是一次追求知识的全新经历。"

"It is, of course, possible to add any type of easing to an object you drag, you just need something else than built-in startDrag(...) method, it is too simple and not customizable."
"当然,你可以为你拖动的对象添加任何类型的缓动效果,你只需要使用内置的 startDrag(...) 方法之外的其他方法,因为它太简单且不可自定义。"

"So, first of all, let's set up the environment, where your designated object is attracted to a certain point. Like gravity, something like that."
"首先,让我们设置一个环境,让你指定的对象被吸引到特定的点,就像重力一样,类似于那样。"

"I want it to be short."
"我希望它很简短。"

"The attractor is placed onto the object, so it doesn't move at the start."
"吸引点被放置在对象上,所以它不会在开始时移动。"

"To keep the current velocity of M."
"为了保持 M 的当前速度。"

"The most precise calculations we can get if we take the passing time into account."
"如果我们考虑经过的时间,我们可以得到最精确的计算。"

"How strong the pull is."
"拉力有多强。"

"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!"
"动量的多少不断丢失。通常应该在 0 到 1 之间。0 = 无摩擦,最终会失去平衡。1 = 全摩擦,所有动量在1秒内丧失。2 = 嗯,尝试一下也无妨!"

"Let's calculate this simple physics model every frame."
"让我们在每一帧中计算这个简单的物理模型。"

"Take the passed time into account."
"考虑经过的时间。"

"The time delta from the last call — now in seconds."
"从上次调用的时间差,现在以秒为单位。"

"Keep the new time."
"保持新时间。"

"Calculate acceleration."
"计算加速度。"

"Apply acceleration."
"应用加速度。"

"Apply friction."
"应用摩擦力。"

"Apply the calculated motion."
"应用计算得出的运动。"

"What's now left is to drag this virtual point C and let the physics do the rest."
"现在剩下的就是拖动这个虚拟点 C,让物理效果完成剩下的工作。"

"Speeding coefficient."
"加速系数。"

"The direction vector MC."
"MC 的方向向量。"

"Follow the attractor point."
"跟随吸引点。"

"This one will give no oscillations, the object just follows the attractor with easing."
"这个版本不会产生振荡,对象只是以缓动效果跟随吸引点。"

英文:

Welcome, the new soul in a pursuit of knowledge.

It is, of course, possible to add any type of easing to an object you drag, you just need something else than built-in startDrag(...) method, it is too simple and not customizable.

So, first of all, let's setup the environment, where your designated object is attracted to a certain point. Like gravity, something like that.

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 coefficients, respectively.
// Altering those you can balance, tune or dis-balance the system.

// How strong 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;
}

What's now left is to drag this virtual point C and let the physics do the rest.

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.removeEventListener(MouseEvent.MOUSE_UP, onUp);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
}

I didn't test that, but the general idea should be correct, I believe you'll figure it from here on. Good luck!

UPD: I don't really understand what do you mean by "continue when you release", but here's a simplified version of the easing, without physics:

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

var M:DisplayObject = mcMsg2c;
var C:Point = new Point(M.x, M.y);

var T:int = getTimer();

// Speeding coefficient.
var S:Number = 1;

addEventListener(Event.ENTER_FRAME, onFrame);

function onFrame(e:Event):void
{
    var aTime:int = getTimer();
    var aDiff:Number = (aTime - T) / 1000;
    
    T = aTime;
    
    // The direction vector MC.
    var aDir:Point = new Point(C.x - M.x, C.y - M.y);
    
    // Follow the attractor point.
    M.x += aDir.x * aDiff * S;
    M.y += aDir.y * aDiff * S;
}

This one will give no oscillations, the object just follwos the attractor with easing.

huangapple
  • 本文由 发表于 2023年3月21日 02:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定