无论我将其设置为什么值,Phaser 3 Arcade Gravity 都无法正常工作

Phaser 3 Arcade Gravity Isn't working properly no matter what value i set it to

我正在用 Phaser 开发一个新项目,由于某种原因,游戏中的重力完全混乱,当我尝试跳跃时,我跳得像一厘米。如果我更改值,则不会发生任何变化,它总是会出现故障。我怎样才能做到正常起跳和落下?

我以前有过一些项目,重力效果很好,对于这个项目,我使用的是最新稳定版的移相器 3。老实说,我看不出错误是什么,我已经做了一段时间了而

有很多代码与错误无关,所以我删除了它,以便其他人更容易查看。

game.js

const socket = io();
var config = {
    type: Phaser.AUTO,
    width: 1000,
    height: 550,
    parent: 'master',
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 300 },
            debug: true
        }
    },
    scene: {
        preload: resources,
        create: mechanics,
        update: controls
    }
};
const game = new Phaser.Game(config);

function resources() {
    this.load.image("arena", "../resources/images/arena1.png");
    this.load.image("floor", "../resources/images/floor.png");
    this.load.atlas("warrior", "../resources/images/characters/warrior.png","../resources/images/characters/warrior.json");


}
var warrior;

function mechanics() {

    grasslands = this.add.image(500, 225, "arena").setScale(0.7);


    warrior = this.physics.add.sprite(100, 490, "warrior").setScale(2).setSize(15, 15);
    
    floor = this.physics.add.staticGroup();
    floor.create(500, 545, "floor").setVisible(false);

    this.physics.add.collider(warrior, floor);
    warrior.body.collideWorldBounds = true;
    warrior.body.onWorldBounds = true;
}   




function controls() {
    
    key = this.input.keyboard.addKeys("W,A,S,D");


    if(key.A.isDown) {
        warrior.setVelocityX(-100);
        warrior.flipX = true;


    }else if (key.D.isDown) {
        warrior.setVelocityX(100);
        warrior.flipX = false;
    


    }else if (key.W.isDown && warrior.body.touching.down) {
        warrior.setVelocityY(-330);
    }else{
        warrior.setVelocity(0);
        
    }
}

由于这一行 warrior.setVelocity(0); 出现问题。这条线,按预期停止工作的重力 (并阻碍跳跃),因为在场景更新时,速度设置为 0。删除 that 行并在 controls 函数的开头添加 warrior.setVelocityX(0) 一切都应该正常工作 (如果你 want/have保留 if-else 块)或者在这个答案的最后查看我的工作演示。

function controls() {
    key = this.input.keyboard.addKeys("W,A,S,D");

    warrior.setVelocityX(0);

    if(key.A.isDown) {
        warrior.setVelocityX(-100);
        warrior.flipX = true;
    }else if (key.D.isDown) {
        warrior.setVelocityX(100);
        warrior.flipX = false;
    }else if (key.W.isDown && warrior.body.touching.down) {
        warrior.setVelocityY(-330);
    }
}

我只会停止 left/right 移动,当玩家停止按下键 (与 setVelocityX(0),让重力负责停止 jump/upwards 运动。

这里有一个小演示:

var config = {
    type: Phaser.AUTO,
    width: 400,
    height: 160,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 100 },
        }
    },
    scene: {
        create,
        update
    }
}; 

var cursors;
var player;
var playerStateText;

var isJumping = true;

function create () {
    cursors = this.input.keyboard.createCursorKeys();
    
    this.add.text(10, 10, 'Use arrow Keys to move!')
    
    let ground = this.add.rectangle(-40, 120, 480, 50, 0xBAF0FF).setOrigin(0);
    player = this.add.rectangle(20, 20, 30, 30, 0xcccccc).setOrigin(0);
    
    ground = this.physics.add.existing(ground);
    ground.body.setImmovable(true);
    ground.body.allowGravity = false;
    
    player = this.physics.add.existing(player);
    
    this.physics.add.collider(player, ground, _ => isJumping = false);      
}

function update (){
    
    if (cursors.left.isDown){
        player.body.setVelocityX(-100);
    } else if (cursors.right.isDown) {
        player.body.setVelocityX(100);
    }
    else {
        player.body.setVelocityX(0);
    }
    
    if (!isJumping && cursors.up.isDown){
        player.body.setVelocityY(-100);
        isJumping = true;
    } 
}

new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>