【发布时间】: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