I have a fully functioning game with only a single player, however, I wanted to expand the game and make it a multiplayer game. In order to do so I just took my code from Player 1 and copied and made some alterations for the Player 2 Javascript File. For starters I only wanted the camera to follow player 1 and not player 2, and wanted player 2 to be moved by the arrow keys whereas player to be moved by the WASD keys. After removing the camera follow code, and making the cursor keys for player 2, I ran the code. The results were very different. Both players move by the WASD keys and the camera only follows player 2, which is the complete opposite to what I attempted. The code for player 1 and player 2 can be found below (in that order):
Player 1:
var Phaser = Phaser || {};
var Platformer = Platformer || {};
Platformer.Player = function (game_state, position, properties) {
"use strict";
var propertiesTemp = new Array(); // Temp variable for array
for (let i = 0; i < properties.length; i++) {
var name = properties[i].name;
propertiesTemp[name] = properties[i].value;
}
properties = propertiesTemp; //Overwrite old values with new
Platformer.Prefab.call(this, game_state, position, properties);
this.walking_speed = +properties.walking_speed;
this.jumping_speed = +properties.jumping_speed;
this.bouncing = +properties.bouncing;
this.game_state.game.physics.arcade.enable(this);
this.body.collideWorldBounds = true;
this.game.camera.follow(this);
this.frame = 3;
this.anchor.setTo(0.5);
this.game.input.keyboard.addKey(Phaser.Keyboard.A);
this.game.input.keyboard.addKey(Phaser.Keyboard.S);
this.game.input.keyboard.addKey(Phaser.Keyboard.D);
this.game.input.keyboard.addKey(Phaser.Keyboard.W);
//this.cursors = this.game_state.game.input.keyboard.createCursorKeys();//*/
};
Platformer.Player.prototype = Object.create(Platformer.Prefab.prototype);
Platformer.Player.prototype.constructor = Platformer.Player;
Platformer.Player.prototype.update = function () {
"use strict";
this.game_state.game.physics.arcade.collide(this,
this.game_state.layers.Solid);//changed the layer name to get it working, because why not
this.game_state.game.physics.arcade.collide(this, this.game_state.groups.enemies,
this.hit_enemy, null, this);
if (this.game.input.keyboard.isDown(Phaser.Keyboard.D) && this.body.velocity.x >= 0) {
// move right
this.body.velocity.x = this.walking_speed;
this.animations.play("walking");
this.scale.setTo(-1, 1);
} else if (this.game.input.keyboard.isDown(Phaser.Keyboard.A) && this.body.velocity.x
<= 0) {
// move left
this.body.velocity.x = -this.walking_speed;
this.animations.play("walking");
this.scale.setTo(1, 1);
} else {
// stop
this.body.velocity.x = 0;
this.animations.stop();
this.frame = 3;
}
// jump only if touching a tile
if (this.game.input.keyboard.isDown(Phaser.Keyboard.W) && this.body.blocked.down) {
this.body.velocity.y = -this.jumping_speed;
}
// dies if touches the end of the screen
if (this.bottom >= this.game_state.game.world.height) {
//this.game_state.restart_level();
this.body.x = this.body.x -900;
this.body.y = 24;
}
};
Platformer.Player.prototype.hit_enemy = function (player, enemy) {
"use strict";
// if the player is above the enemy, the enemy is killed, otherwise the player dies
if (enemy.body.touching.up) {
enemy.kill();
this.body.y -= this.bouncing;
} else {
//this.game_state.restart_level();
this.body.x = this.body.x -900;
this.body.y = 24;
}
};
Player 2:
var Phaser = Phaser || {};
var Platformer = Platformer || {};
Platformer.Player2 = function (game_state, position, properties) {
"use strict";
var propertiesTemp = new Array(); // Temp variable for array
for (let i = 0; i < properties.length; i++) {
var name = properties[i].name;
propertiesTemp[name] = properties[i].value;
}
properties = propertiesTemp; //Overwrite old values with new
Platformer.Prefab.call(this, game_state, position, properties);
this.walking_speed = +properties.walking_speed;
this.jumping_speed = +properties.jumping_speed;
this.bouncing = +properties.bouncing;
this.game_state.game.physics.arcade.enable(this);
this.body.collideWorldBounds = true;
this.cursors = this.game_state.game.input.keyboard.createCursorKeys();//*/
};
Platformer.Player2.prototype = Object.create(Platformer.Prefab.prototype);
Platformer.Player2.prototype.constructor = Platformer.Player2;
Platformer.Player2.prototype.update = function () {
"use strict";
this.game_state.game.physics.arcade.collide(this,
this.game_state.layers.Solid);//changed
the layer name to get it working, because why not
this.game_state.game.physics.arcade.collide(this, this.game_state.groups.enemies,
this.hit_enemy, null, this);
if (this.cursors.right.isDown && this.body.velocity.x >= 0) {
// move right
this.body.velocity.x = this.walking_speed;
this.animations.play("walking");
this.scale.setTo(-1, 1);
} else if (this.cursors.left.isDown && this.body.velocity.x <= 0) {
// move left
this.body.velocity.x = -this.walking_speed;
this.animations.play("walking");
this.scale.setTo(1, 1);
} else {
// stop
this.body.velocity.x = 0;
this.animations.stop();
this.frame = 3;
}
// jump only if touching a tile
if (this.cursors.up.isDown && this.body.blocked.down) {
this.body.velocity.y = -this.jumping_speed;
}
// dies if touches the end of the screen
if (this.bottom >= this.game_state.game.world.height) {
//this.game_state.restart_level();
this.body.x = this.body.x -900;
this.body.y = 24;
}
};
Platformer.Player2.prototype.hit_enemy = function (player, enemy) {
"use strict";
// if the player is above the enemy, the enemy is killed, otherwise the player dies
if (enemy.body.touching.up) {
enemy.kill();
this.body.y -= this.bouncing;
} else {
//this.game_state.restart_level();
this.body.x = this.body.x -900;
this.body.y = 24;
}
};