当我添加 this.physics 行时 Phaser ARCADE 物理不工作
Phaser ARCADE physics not working when i add this.physics line
我是 Phaser 的新手,我尝试用它制作游戏。
为了添加物理学,我遵循了 phaser.io 物理学教程。
这是我的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body {
margin: 0;
padding: 0
}
canvas {
width: 100%
}
</style>
<body>
<!-- <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.60.0-beta.3/phaser-arcade-physics.js"></script>
<script>
let config={
type: Phaser.AUTO,
width: 1000,
height: 500,
backgroundColor: '#2bff00',
physics: {
default: 'matter',
arcade: {
gravity: {y: 0},
debug: true
}
},
scene: {
preload: preload,
create: create,
update: update
}
}
let game = new Phaser.Game(config);
let enemy1dir = 1, enemy2dir = 2, enemy3dir = 3, enemy4dir = 4, enemy5dir = 5;
function preload() {
this.load.image('player', 'boydoctor.png');
this.load.image('enemy', 'coronavirus.png');
}
let player, enemy1, enemy2, enemy3, enemy4, enemy5;
function create() {
this.physics.world.setBounds(0,0, 1000, 500);
this.player = this.add.sprite(50, 250, 'player');
this.player.setScale(.2, .2);
this.enemy1 = this.add.sprite(150, 50, 'enemy');
this.enemy1.setScale(.05, .05);
this.enemy2 = this.add.sprite(300, 50, 'enemy');
this.enemy2.setScale(.05, .05);
this.enemy3 = this.add.sprite(450, 50, 'enemy');
this.enemy3.setScale(.05, .05);
this.enemy4 = this.add.sprite(600, 50, 'enemy');
this.enemy4.setScale(.05, .05);
this.enemy5 = this.add.sprite(750, 50, 'enemy');
this.enemy5.setScale(.05, .05);
}
function update() {
let height = this.sys.game.config.height;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP).isDown)
this.player.y -= 2;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN).isDown)
this.player.y += 2;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT).isDown)
this.player.x -= 2;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT).isDown)
this.player.x += 2;
if (this.enemy1.y > height - 50)
enemy1dir = -1;
else if (this.enemy1.y < 50)
enemy1dir = 1;
if (this.enemy2.y > height - 50)
enemy2dir = -2;
else if (this.enemy2.y < 50)
enemy2dir = 2;
if (this.enemy3.y > height - 50)
enemy3dir = -3;
else if (this.enemy3.y < 50)
enemy3dir = 3;
if (this.enemy4.y > height - 50)
enemy4dir = -4;
else if (this.enemy4.y < 50)
enemy4dir = 4;
if (this.enemy5.y > height - 50)
enemy5dir = -5;
else if (this.enemy5.y < 50)
enemy5dir = 5;
this.enemy1.y += enemy1dir;
this.enemy2.y += enemy2dir;
this.enemy3.y += enemy3dir;
this.enemy4.y += enemy4dir;
this.enemy5.y += enemy5dir;
}
</script>
</body>
</html>
在此,当我添加 this.physics.world.setBounds(0,0, 1000, 500);
行时出现错误,但是当我删除它时,它可以正常工作。如果此技术错误,那么 我应该使用哪种技术将 ARCADE Physics 添加到游戏中?
我不确定这是否重要,但我注意到在您的配置中您将 'matter' 作为默认设置,但您想使用 'arcade'。也许将默认值更改为 'arcade' 可能会成功。
我是 Phaser 的新手,我尝试用它制作游戏。
为了添加物理学,我遵循了 phaser.io 物理学教程。
这是我的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body {
margin: 0;
padding: 0
}
canvas {
width: 100%
}
</style>
<body>
<!-- <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.60.0-beta.3/phaser-arcade-physics.js"></script>
<script>
let config={
type: Phaser.AUTO,
width: 1000,
height: 500,
backgroundColor: '#2bff00',
physics: {
default: 'matter',
arcade: {
gravity: {y: 0},
debug: true
}
},
scene: {
preload: preload,
create: create,
update: update
}
}
let game = new Phaser.Game(config);
let enemy1dir = 1, enemy2dir = 2, enemy3dir = 3, enemy4dir = 4, enemy5dir = 5;
function preload() {
this.load.image('player', 'boydoctor.png');
this.load.image('enemy', 'coronavirus.png');
}
let player, enemy1, enemy2, enemy3, enemy4, enemy5;
function create() {
this.physics.world.setBounds(0,0, 1000, 500);
this.player = this.add.sprite(50, 250, 'player');
this.player.setScale(.2, .2);
this.enemy1 = this.add.sprite(150, 50, 'enemy');
this.enemy1.setScale(.05, .05);
this.enemy2 = this.add.sprite(300, 50, 'enemy');
this.enemy2.setScale(.05, .05);
this.enemy3 = this.add.sprite(450, 50, 'enemy');
this.enemy3.setScale(.05, .05);
this.enemy4 = this.add.sprite(600, 50, 'enemy');
this.enemy4.setScale(.05, .05);
this.enemy5 = this.add.sprite(750, 50, 'enemy');
this.enemy5.setScale(.05, .05);
}
function update() {
let height = this.sys.game.config.height;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP).isDown)
this.player.y -= 2;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN).isDown)
this.player.y += 2;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT).isDown)
this.player.x -= 2;
if (this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT).isDown)
this.player.x += 2;
if (this.enemy1.y > height - 50)
enemy1dir = -1;
else if (this.enemy1.y < 50)
enemy1dir = 1;
if (this.enemy2.y > height - 50)
enemy2dir = -2;
else if (this.enemy2.y < 50)
enemy2dir = 2;
if (this.enemy3.y > height - 50)
enemy3dir = -3;
else if (this.enemy3.y < 50)
enemy3dir = 3;
if (this.enemy4.y > height - 50)
enemy4dir = -4;
else if (this.enemy4.y < 50)
enemy4dir = 4;
if (this.enemy5.y > height - 50)
enemy5dir = -5;
else if (this.enemy5.y < 50)
enemy5dir = 5;
this.enemy1.y += enemy1dir;
this.enemy2.y += enemy2dir;
this.enemy3.y += enemy3dir;
this.enemy4.y += enemy4dir;
this.enemy5.y += enemy5dir;
}
</script>
</body>
</html>
在此,当我添加 this.physics.world.setBounds(0,0, 1000, 500);
行时出现错误,但是当我删除它时,它可以正常工作。如果此技术错误,那么 我应该使用哪种技术将 ARCADE Physics 添加到游戏中?
我不确定这是否重要,但我注意到在您的配置中您将 'matter' 作为默认设置,但您想使用 'arcade'。也许将默认值更改为 'arcade' 可能会成功。