我正在学习AS3,我正在努力建立一个小小的互动,我可以在舞台上抛出一个正方形。它只需要保持在x轴上,向左或向右抛出。
我找到了很多涉及物理学的教程。我不需要那么复杂的东西。有了我的,只要你点击广场然后扔一点,无论投掷动作有多难,广场将永远被抛出舞台,取决于你的鼠标的方向。
我有一种想法在脑海中可以通过动画片段中的补间来完成“投掷”的感觉(因为它只需要向左或向右),这将根据方向的不同而激活。老鼠。
如果这是一个基本问题我很抱歉,我不知道如何开始。请帮忙!
答案 0 :(得分:1)
尝试将此作为要抛出的对象的基类。
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class ThrowableObject extends Sprite
{
// properties
public var yv:Number = 0;
public var xv:Number = 0;
private var _grabbed:Boolean = false;
private var _gy:int = 0;
private var _gx:int = 0;
private var ox:int = 0;
private var oy:int = 0;
/**
* Constructor
*/
public function ThrowableObject()
{
addEventListener(Event.ADDED_TO_STAGE, _init);
}
/**
* Called on dispatch of Event.ADDED_TO_STAGE
* @param e Event.ADDED_TO_STAGE
*/
private function _init(e:Event):void
{
addEventListener(Event.ENTER_FRAME, _handle);
addEventListener(MouseEvent.MOUSE_DOWN, _handleClick);
stage.addEventListener(MouseEvent.MOUSE_UP, _handleRelease);
}
/**
* Called on dispatch of Event.ENTER_FRAME
* @param e Event.ENTER_FRAME
*/
private function _handle(e:Event):void
{
ox = x;
oy = y;
if(_grabbed)
{
x = parent.mouseX - _gx;
y = parent.mouseY - _gy;
}
else
{
x += xv;
y += yv;
}
}
/**
* Called on dispatch of MouseEvent.MOUSE_DOWN
* @param e MouseEvent.MOUSE_DOWN
*/
private function _handleClick(e:MouseEvent):void
{
grabbed = true;
parent.addChild(this);
}
/**
* Called on dispatch of MouseEvent.MOUSE_UP
* @param e MouseEvent.MOUSE_UP
*/
private function _handleRelease(e:MouseEvent):void
{
grabbed = false;
}
/**
* Sets grabbed
* @param val Boolean representing value to set grabbed as
*/
protected function set grabbed(bool:Boolean):void
{
_grabbed = bool;
if(_grabbed)
{
_gx = mouseX;
_gy = mouseY;
}
else
{
xv = x - ox;
yv = y - oy;
}
}
}
}