【问题标题】:Phaser, fluid strafing (diagonal input)移相器,流体扫射(对角线输入)
【发布时间】:2016-05-25 12:36:00
【问题描述】:

我一直在玩一个精灵,试图让它以我期望的方式扫射 - 末日风格。

所以问题是我可以设置我的精灵的角度,我使用velocityFromRotation 相对于其当前角度向上、向下、向左和向右移动它。 到目前为止,很可爱。

问题是我不能沿对角线移动,也就是说,相对于精灵的角度,我需要能够同时向上和向左移动。 我对每个动作都有一个函数,向上、向下、向左和向右 - 因为所有人都使用velocityFromRotation,所以最近按下的键将覆盖第一个键的移动。

据推测,我也可以简单地为每个对角线创建函数,尽管我不完全确定我将传递给 velocityFromRotation 方法来实现这一点 - 但即使这样有效,它也可能会停止我使用模拟输入类型,因为对角线逻辑将被硬编码。我没有使用模拟输入,但我正在尝试将我的预制件构建为尽可能“插入式”可重复使用。

这是我当前的代码:

Ship.prototype.moveForward = function() {
    // Move the player up
    this.game.physics.arcade.velocityFromAngle(this.angle - 90, this.speed, this.body.velocity);
}

Ship.prototype.moveBackward = function() {
    this.game.physics.arcade.velocityFromAngle(this.angle + 90, this.speed, this.body.velocity);
}

Ship.prototype.bankLeft = function() {
    // Move the player left
    this.game.physics.arcade.velocityFromAngle(this.angle + 180, this.speed, this.body.velocity);
}

Ship.prototype.bankRight = function() {
    // Move the player left
    this.game.physics.arcade.velocityFromAngle(this.angle, this.speed, this.body.velocity);
}

【问题讨论】:

    标签: javascript game-physics phaser-framework


    【解决方案1】:

    没关系,我找到了。

    对我来说,诀窍是记录精灵当前正在做什么,并在函数中检查以手动调整角度。

    所以,我在更新函数中存储了一些变量 - 它们在各种运动函数中被覆盖:

    this.movement.movingForward     = false;
    this.movement.movingBackward    = false;
    this.movement.moving            = false;
    

    那么我的运动函数是这样的:

    // Player movement
    Ship.prototype.moveForward = function() {
        // Move the player up
        this.game.physics.arcade.velocityFromAngle(this.angle - 90, this.speed, this.body.velocity);
        this.movement.moving = true;
        this.movement.movingForward = true;
    }
    
    Ship.prototype.moveBackward = function() {
        // Move the player down
        this.game.physics.arcade.velocityFromAngle(this.angle + 90, this.speed, this.body.velocity);
        this.movement.moving = true;
        this.movement.movingBackward = true;
    }
    
    Ship.prototype.bankLeft = function() {
        // Move the player left
        this.game.physics.arcade.velocityFromAngle(this.angle + 180 - ((this.movement.movingForward ? -45 : 0) + (this.movement.movingBackward ? 45 : 0)), this.speed, this.body.velocity);
        this.movement.moving = true;
        this.movement.bankingLeft = true;
    }
    
    Ship.prototype.bankRight = function() {
        // Move the player left
        this.game.physics.arcade.velocityFromAngle(this.angle + ((this.movement.movingForward ? -45 : 0) + (this.movement.movingBackward ? 45 : 0)), this.speed, this.body.velocity);
        this.movement.moving = true;
        this.movement.bankingRight = true;
    }
    

    如果你想知道我为什么将角度调整 90 度,那是因为精灵面朝上,而 Phaser 似乎将 0 度指向右侧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 2020-02-24
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多