Hi Devs,
I have a game in Phaser and i want the player to keep moving continuously till it reaches the end of the map. I have to click everytime to make it move. For now, i have set the cursor keys to move it but i want the click approach. Here is the code
const config = {
type: Phaser.AUTO,
width: 800,
height: 900,
physics: {
default: “arcade”,
arcade: {
gravity: { y: 1000 },
debug: false,
},
},
scene: {
preload: preload,
create: create,
update: update,
},
};
const assets = {
scene: {
width: 400,
gameOver: “game-over”,
restart: “restart-button”,
messageInitial: “message-initial”,
},
};
var game = new Phaser.Game(config);
let gameOver;
let gameStarted;
let restartButton;
let gameOverBanner;
let messageInitial;
let framesMoveUp;
let backgroundImage;
var map;
var player;
var cursors;
var text;
var score = 0;
function preload() {
// Backgrounds
this.load.image(“background”, “assets/sky.png”);
// At last image must be loaded with its JSON
this.load.spritesheet(“player”, “assets/white-bird-sprite.png”, {
frameWidth: 220,
frameHeight: 160,
});
// Start game
this.load.image(assets.scene.messageInitial, “assets/square.png”, {
height: 200,
width: 200,
});
// End game
this.load.image(assets.scene.gameOver, “assets/gameover.png”);
this.load.image(assets.scene.restart, “assets/restart-button.png”);
// tiles in spritesheet
this.load.spritesheet(“tiles”, “assets/tiles.png”, {
frameWidth: 100,
frameHeight: 100,
});
// Load the export Tiled JSON
this.load.tilemapTiledJSON(“map”, “assets/map.json”);
}
function movePlayer() {}
function create() {
cursors = this.input.keyboard.createCursorKeys();
// Square Bird Logo
messageInitial = this.add.image(
assets.scene.width,
230,
assets.scene.messageInitial
);
messageInitial.setDepth(30);
messageInitial.visible = true;
const backgroundImage = this.add
.image(0, 0, “background”)
.setOrigin(0, 0)
.setInteractive();
map = this.make.tilemap({ key: “map” });
var tileset = map.addTilesetImage(“tiles”, “tiles”);
var platforms = map.createDynamicLayer(“Platform”, tileset, 0, 0);
var collision = map.createDynamicLayer(“Collision”, tileset, 0, 0);
platforms.setCollisionByExclusion(-1);
collision.setCollisionByExclusion(-1);
// set the boundaries of our game world
this.physics.world.bounds.width = platforms.width;
this.physics.world.bounds.height = platforms.height;
// Adding Player
this.player = this.physics.add.sprite(0, 0, “player”);
this.player.setBounce(0.1);
this.player.setCollideWorldBounds(true);
this.physics.add.collider(this.player, platforms);
this.physics.add.collider(this.player, collision);
this.player.body.allowGravity = true;
// set bounds so the camera won’t go outside the game world
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
// make the camera follow the player
this.cameras.main.startFollow(this.player);
this.cameras.main.setBackgroundColor("#00ffff");
// White Bird Animations
this.anims.create({
key: “animate”,
frames: this.anims.generateFrameNumbers(“player”, {
start: 0,
end: 2,
}),
frameRate: 10,
repeat: -1,
});
this.player.play(“animate”);
backgroundImage.on(“pointerdown”, () => {
if (gameOver) return;
if (!gameStarted) startGame(game.scene.scenes[1]);
this.player.setVelocityX(1000);
});
framesMoveUp = 0;
score = 0;
gameOver = false;
}
function startGame() {
messageInitial.visible = false;
gameStarted = true;
}
function update() {
this.button = this.player.setInteractive();
if (cursors.left.isDown) {
this.player.body.setVelocityX(-200);
} else if (cursors.right.isDown) {
this.player.body.setVelocityX(200);
} else {
this.player.body.setVelocityX(0);
}
// jump
if (cursors.up.isDown && this.player.body.onFloor()) {
this.player.body.setVelocityY(-700);
}
this.button.on(
“pointerdown”,
function () {
this.player.body.setVelocityX(1000);
},
this
);
}