我正在努力创造一种“蠕虫”风格的回合制火炮游戏as3。我能够在角色的x / y位置添加我的球,并且在点击时让球朝着鼠标位置的方向射击但我现在很难将重力施加到球上。如果有人可以帮助我完成我需要做的事情,让球在击球后落入弧线会很棒。
我添加球并在选定方向点火的代码是: 游戏主要:
function redFollow(e:MouseEvent):void {
var a1 = mouseY - redPower.y;
var b1 = mouseX - redPower.x;
var radians1 = Math.atan2(a1,b1);
var degrees1 = radians1 / (Math.PI/180);
redPower.rotation = degrees1;
}
public function redFIRE(Event:MouseEvent)
{
removeChild(redPower);
turnchangeover();
addChild(RPBall);
trace("FIRE!");
RPBall.x = red.x;
RPBall.y = red.y;
RPBall.rotation = redPower.rotation + 90;
}
然后球的类代码是:
包{
import flash.display.MovieClip;
import flash.events.Event;
public class redBall extends MovieClip {
public function redBall() {
this.addEventListener(Event.ENTER_FRAME, moveShot);
}
function moveShot(event:Event){
var thisMissile:redBall = redBall(event.target);
var _missileSpeed:int = 7;
var gravity:int = 0.8;
//get the x,y components of the angle
var missileAngleRadians = ((-thisMissile.rotation - 180) * Math.PI /180);
//trace("missle angle: " + missileAngleRadians);
var yMoveIncrement = Math.cos(missileAngleRadians) * _missileSpeed;
var xMoveIncrement = Math.sin(missileAngleRadians) * _missileSpeed;
thisMissile.y = thisMissile.y +yMoveIncrement;
thisMissile.x = thisMissile.x + xMoveIncrement;
trace(thisMissile.y);
}
}
}
答案 0 :(得分:0)
你需要分别用X和Y来存储速度。看看,你的球被发射后,你不关心angle
,但只关心速度,速度是受重力影响的。你已经用yMoveIncrement,xMoveIncrement
中的X和Y分量来分割速度,现在你只需要让它们成为球的属性(它将是它的速度),并为速度的Y分量添加一个小的每帧增量
public class redBall extends MovieClip {
public var yVelocity:Number=0;
public var xVelocity:Number=0; // these will be assigned when the ball is shot
public static var gravity:Number=0.5; // Number, not "int"
// also "static" so that it'll affect all balls similarly
public function redBall() {
this.addEventListener(Event.ENTER_FRAME, moveShot);
}
function moveShot(event:Event){
// var thisMissile:redBall = redBall(event.target);
// why is ^ this? You can use "this" qualifier in this function
// we already have the x,y components of the angle
this.y = this.y + this.yVelocity;
this.x = this.x + this.xVelocity;
this.yVelocity = this.yVelocity + gravity;
// also, you care for rotation - let's add a trick
var angle:Number=Math.atan2(this.yVelocity,this.xVelocity);
// direction angle, in radians
this.rotation=angle*180/Math.PI; // make into degrees and rotate
trace(thisMissile.y);
}
}
如果您要将shoot
类添加到redBall
类,接受角度和初始速度,将xVelocity
和yVelocity
指定为{{1}},这将是一个很好的做法。你是在听众那里做的。
答案 1 :(得分:0)
为了重新创建射弹运动,你需要引入3个变量Gravity(常数),速度(在x和y上,类似于你的移动增量)和衰变,也称为摩擦。 / p>
摩擦应该是低于1的值。因此它会随着时间的推移而降低速度。一旦速度小于系统中的重力,物品就应该开始回落到地面。该项目也将在x轴上减速。
velocity.y *= friction;
thisMissile.y += velocity.y + gravity.y;
以下是可能感兴趣的generic tutorial on projectile physics和AS3 tutorial on Angry Bird like ground to ground projectile。