0

我正在尝试创建一个“蠕虫”风格的回合制火炮游戏是 as3。我可以将我的球添加到角色的 x/y 位置,并在点击时让球朝鼠标位置的方向发射,但我现在很难将重力施加到球上。如果有人可以帮助我完成我需要做的事情以使球在被射击后以弧线落下,那就太好了。

我添加球并让它在选定的方向发射的代码是:游戏主:

函数 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);

  }



}

}

4

2 回答 2

0

为了重新创建弹丸运动,您需要引入 3 个变量重力(常数)、速度(在 x 和 y 上,类似于您的移动增量)和衰减,也称为摩擦。

摩擦力应该小于 1。这样它会随着时间的推移降低速度。一旦速度小于系统中的重力,项目应该开始回落到地面。该项目还将在 x 轴上减速。

velocity.y *= friction;
thisMissile.y += velocity.y + gravity.y;

这是一个可能感兴趣的关于弹丸物理的通用教程和一个关于愤怒的小鸟(如地对地弹丸)的 AS3 教程

于 2013-07-18T11:22:39.097 回答
0

你需要用 X 和 Y 分别存储速度。你看,你的球发射后,你不关心angle,而只关心速度,而速度是受重力影响的。您已经通过 X 和 Y 分量分割速度yMoveIncrement,xMoveIncrement,现在您只需将它们作为球的属性(它将是它的速度),并为速度的 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将接受角度和初始速度的类中,这将是一个很好的做法,xVelocityyVelocity像您在侦听器中所做的那样进行分配。

于 2013-07-18T10:49:38.813 回答