你需要一点物理知识。
初始速度必须使用您自己添加的一些标准来计算。一个例子是通过按下鼠标时鼠标与大炮之间的距离来计算初始速度。如果距离较大,则弹丸的速度较大;如果距离较小,则弹丸的速度较小。
您添加一个类型为 ENTER_FRAME 的事件侦听器。
我猜这是二维动画,所以你必须在任何时间点找到当前的 x 和 y。
这是一些代码:
var TimeperFrame:Number = 1/fps //fps is not a constant, here you should add a number, a value that you previously added in fla. document properties. I usualy use 60 fps
var Time:Number = 0;
addEventListener(ENTER_FRAME, movingCannonBall);
function movingCannonBall(e:Event):void
{
Time += TimeperFrame;
}
现在这是射弹轨迹的方程。
x = xo + vxo·t
y = yo + vyo·t - 0.5·g·t^2
yo = 你的炮弹的初始高度
vyo = 初始 y 速度; vyo = vo·sin θ
t = 过去的时间,我们通过上层代码控制
g = 地球表面加速度 (9.81 m/s^2)
xo = 起点的初始距离
vxo = 初始 x 速度; vxo = vo·cos θ
现在,在上面的代码中,我们添加这些方程,它应该如下所示:
var TimeperFrame:Number = 1/fps
var Time:Number = 0;
var initx: Number = cannonball.x;
var inity: Number = cannonball.y;
var initVelocity: Number = (you define initial Velocity by your criteria)
var G: Number = 9.81;
addEventListener(ENTER_FRAME, movingCannonBall);
function movingCannonBall(e:Event):void
{
Time += TimeperFrame;
cannonball.x = initx + Math.cos(angle) * initVelocity * Time;
cannonball.y = inity + Math.sin(angle) * initVelocity * Time - G * Time * Time * 0.5
}
这应该有效。我已经多次使用这个代码,它很高效,而且很简单。