【问题标题】:Set the maximum and current speed of a car in Phaser在 Phaser 中设置汽车的最大和当前速度
【发布时间】:2015-02-24 12:44:31
【问题描述】:

我正在使用使用 JS 的 Phaser 框架制作一个自上而下的赛车游戏。我在让汽车减速时遇到了一些麻烦,目前它只是在没有按下任何按钮时停止。我希望它减速停止。到目前为止,这是我的代码: 创建:函数(){

    //run in canvas mode
    this.game.renderer.clearBeforeRender - false;
    this.game.renderer.roundPixels = true;

    //Add arcade physics
    this.game.physics.startSystem(Phaser.Physics.ARCADE);

    //Add background sprites
    this.game.add.sprite(0, 0, 'background');
    this.game.add.sprite(0, 0, 'track');

    //Add car sprite
    car = this.game.add.sprite(800, 135, 'car-concept');

    //Car physics settings
    this.game.physics.enable(car, Phaser.Physics.ARCADE);

    car.body.drag.set(100);
    car.body.maxVelocity.set(200);
    car.body.maxAngular = 500;
    car.body.angularDrag = 500;
    car.body.collideWorldBounds = true;

    //Game input
    cursors = this.game.input.keyboard.createCursorKeys();
    this.game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR]);

    //pivot point
    car.pivot.x = car.width * .3;
    car.pivot.y = car.height * .5;
    car.anchor.setTo(0.3, 0.5);

    //scale the car
    car.scale.setTo(0.3, 0.3);
},

update: function () {

    car.body.velocity.x = 0;
    car.body.velocity.y = 0;
    car.body.angularVelocity = 0;

    if(cursors.left.isDown)
    {
      car.body.angularVelocity = -200;
    }
    else if(cursors.right.isDown)
    {
      car.body.angularVelocity = 200;
    }
    if(cursors.up.isDown)
    {
      this.game.physics.arcade.velocityFromAngle(car.angle, -200, car.body.velocity)
      if(this.currentSpeed < this.maxSpeed)
      {
        this.currentSpeed += 10;
      }
    }
    else
    {
      if(this.currentSpeed > 0)
      {
        this.currentSpeed -= 10;
      }
    }
    if(cursors.down.isDown)
    {
      this.game.physics.arcade.velocityFromAngle(car.angle, 200, car.body.velocity)
      if(this.currentSpeed > 0)
      {
        this.currentSpeed -= 30;
      }
    }

},

speed: function()
{
  this.maxSpeed = 100;
  this.currentSpeed = 0;
},

我在这里查看了一些关于同一问题的问题,这就是我现在所处的位置。我已经设置了 maxSpeed 和 currentSpeed 但由于某种原因它不允许我实际使用它。浏览器中的控制台没有给我任何错误,所以如果有人可以指导我,那就太好了!

谢谢。

【问题讨论】:

    标签: javascript game-physics phaser-framework racing


    【解决方案1】:

    它停止死机的原因是因为您以速度而不是加速度移动它 - 并且在更新循环中将速度设置为零(这实际上是说“停止,现在!”)。

    删除这些行并在您对velocityFromAngle 的调用中将其输入body.acceleration 而不是body.velocity

    这是一个您可以使用的更新循环,但您需要混合您的 currentSpeed 设置等:

    function update() {
    
        if (cursors.up.isDown)
        {
            game.physics.arcade.accelerationFromRotation(sprite.rotation, 200, sprite.body.acceleration);
        }
        else
        {
            sprite.body.acceleration.set(0);
        }
    
        if (cursors.left.isDown)
        {
            sprite.body.angularVelocity = -300;
        }
        else if (cursors.right.isDown)
        {
            sprite.body.angularVelocity = 300;
        }
        else
        {
            sprite.body.angularVelocity = 0;
        }
    
    }
    

    【讨论】:

    • 我使用了该解决方案,但无济于事。接受向前运动需要很高,大约 9000 而不是 200,这似乎没有什么不同。我很确定这是由于 currentSpeed 和 maxSpeed 没有被正确引用。
    • 恐怕这与引用 currentSpeed 无关 - 您正在使用的实际核心循环已损坏。在您开始引入任何类型的速度限制之前,您需要先让它发挥作用。正如我所说,您需要在代码中使用速度删除所有内容,并删除限制(maxVelocity 等)并在添加速度限制之前将其恢复到上面的基本结构。
    【解决方案2】:

    您从未使用过currentSpeed。您在velocityFromAngle 中将速度设置为-200200

    【讨论】:

    • 所以你的意思是用 currentSpeed 替换 200 和 -200?
    猜你喜欢
    • 2013-02-28
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多