Let's prevent the sprite from going off screen. Here, we are going to render the ship in another part of the screen. Follow these steps to do so:
- First, let's set the screen size to dedicated constants so that we can use it in our code:
const screenWidth = 800;
const screenHeight = 600;
var config = {
type: Phaser.AUTO,
width: screenWidth,
height: screenHeight,
backgroundColor: '#03187D',
scene: {
preload: preload,
create: create,
update: update
}
};
- Now, on each update, you can check whether the new coordinates of the ship are off screen and change the value to point to another place, or maybe even prevent the ship from moving if you want the ship to stay where it is:
function update() {
// RIGHT button
if ...