Hi everyone,
Phaser 3 newbie here. I have created the following code. I aim to ctreate a collision with the bounds and stop. Up down key movement. It works. how to use setbounds methods here?
thanks in advance
shriram
let gameScene = new Phaser.Scene(‘Game’);
let config = {
physics: {
default: ‘arcade’,
arcade: {
width: 300,
height: 300,
gravity: {
x: 0,
y: 0,
},
checkCollision: {
up: true,
down: true,
left: true,
right: true
},
}
},
type: Phaser.AUTO,
width: 500, // game width
height: 400, // game height
scene: gameScene
};
let game = new Phaser.Game(config);
gameScene.init = function() {
this.playerSpeed = 1.5;
};
gameScene.preload = function() {
this.load.image(‘background’, ‘sky.png’);
this.load.image(‘player’, ‘boy.png’);
};
gameScene.create = function() {
let bg = this.add.image(0, 0, ‘background’);
this.player = this.add.sprite(70, this.sys.game.config.height/3, ‘player’);
this.player.setScale(0.5);
bg.setOrigin(0,0);
cursors = this.input.keyboard.createCursorKeys();
gameScene.physics.world.setBounds(0, 0, 200, 200);
this.player.setCollideWorldBounds(true);
};
gameScene.update = function() {
console.log(Math.round(this.player.y), this.player.x);
if (cursors.left.isDown)
{
if (this.player.x !=17)
{
this.player.x -=1;
}
} else if (cursors.right.isDown)
{
if (this.player.x !=445)
{
this.player.x +=1;
}
} else if (cursors.up.isDown)
{
if (Math.round(this.player.y) !=37)
{
this.player.y-= 1;
}
} else if (cursors.down.isDown)
{
if (Math.round(this.player.y) !=400)
{
this.player.y+= 1;
}
}
};