【问题标题】:Phaser 3 Arcade Gravity Isn't working properly no matter what value i set it toPhaser 3 Arcade Gravity 无论我设置什么值都无法正常工作
【发布时间】:2022-06-13 14:13:32
【问题描述】:

我正在使用 Phaser 开发一个新项目,但由于某种原因,游戏中的重力完全被弄乱了,当我尝试跳跃时,我跳得像一厘米。如果我更改值,则没有任何变化,它总是会出现故障。怎样才能让我正常跳跃和摔倒?

我之前有一些项目,并且重力工作正常,对于这个项目,我使用的是 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);
        
    }
}

【问题讨论】:

    标签: javascript game-physics game-development phaser-framework


    【解决方案1】:

    由于这条线warrior.setVelocity(0);而出现问题。这条线按预期停止工作的重力(并阻碍跳跃),因为在场景更新时,速度设置为0。删除 that 行并在controls 函数的开头添加warrior.setVelocityX(0),一切都应该正常(如果你想/必须保留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);
        }
    }
    

    我只会在玩家停止按键时停止左/右移动(使用setVelocityX(0),并让重力停止跳跃/向上移动。

    这是一个基本的演示:

    var config = {
        type: Phaser.AUTO,
        width: 400,
        height: 160,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: { y: 200 },
            }
        },
        scene: {
            create,
            update
        }
    }; 
    
    var cursors;
    var player;
    var playerStateText;
    const SPEED = 250;
    
    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(-SPEED);
        } else if (cursors.right.isDown) {
            player.body.setVelocityX(SPEED);
        }
        else {
            player.body.setVelocityX(0);
        }
        
        if (!isJumping && cursors.up.isDown){
            player.body.setVelocityY(-SPEED * .75);
            isJumping = true;
        } 
    }
    
    new Phaser.Game(config);
    <script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2020-10-31
      • 1970-01-01
      相关资源
      最近更新 更多