Reputation: 840
I'm new to Phaser and I tried to make a game in it.
For adding the physics i followed the phaser.io tutorial for physics.
This is my code 👇
<!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/[email protected]/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>
In this when I add this.physics.world.setBounds(0,0, 1000, 500);
line there is an error but when I remove it, then it works properly. If this technique is wrong then Which technique should I use to add ARCADE Physics to the game?
Upvotes: 0
Views: 708
Reputation: 46
I'm not sure if this is a big deal or not but I noticed in your config that you have 'matter' as your default but you want to use 'arcade'. Maybe changing default to 'arcade' might do the trick.
Upvotes: 2