change physics to J2, set collisions on tree layer

This commit is contained in:
Tyrel Souza 2016-04-08 00:08:51 -04:00
parent cbf40600c2
commit e96a101927

View File

@ -1,5 +1,7 @@
var game = new Phaser.Game(1152, 704, Phaser.AUTO, '');
var game = new Phaser.Game(1152, 850, Phaser.AUTO, '');
var player;
// http://talestolduntold.blogspot.com/2015/06/tilemaps-with-invisible-collision-layer.html
game.state.add('play', {
preload: function(){
@ -15,67 +17,81 @@ game.state.add('play', {
this.layers = {};
},
create: function(){
game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.startSystem(Phaser.Physics.P2JS);
this.cursors = this.game.input.keyboard.createCursorKeys();
this.createTileMap('forest1');
this.createLayers('forest1');
this.createPlayer();
this.setCollisions();
game.physics.p2.enable(this.player)
},
update: function(){
this.playerMovement();
game.debug.bodyInfo(this.player, 32, this.game.world.height - 100);
game.physics.arcade.collide(player, this.layers['trees']);
game.debug.bodyInfo(player, 32, this.game.world.height - 100);
},
setCollisions: function(){
game.physics.collide(this.player, this.layers['water']);
game.physics.collide(this.player, this.layers['trees']);
},
createTileMap: function(map_name){
/* CUSTOM METHODS VVVVV */
createLayers: function(map_name){
this.map = game.add.tilemap(map_name);
this.map.addTilesetImage('kenney_complete', 'tiles');
this.layers['water'] = this.map.createLayer('Water');
this.layers['ground'] = this.map.createLayer('Ground');
this.layers['trees'] = this.map.createLayer('Trees');
this.layers['misc'] = this.map.createLayer('Misc');
// collisions
this.layers['trees'].enableBody = true;
this.map.collisionLayer = this.layers['trees'];
game.physics.arcade.enable(this.layers['trees'], Phaser.Physics.ARCADE, true);
game.physics.enable(this.layers['trees']);
game.physics.p2.convertTilemap(this.map, "Trees");
tree_ids = [181,182,208,209, 183];
for (var i in tree_ids){
this.map.setCollision(tree_ids[i], true, "Trees");
}
},
createPlayer: function(){
this.player = game.add.sprite(50, 50, 'character');
game.physics.arcade.enable(this.player);
player = game.add.sprite(80, 144, 'character');
game.physics.enable(player);
player.body.collideWorldBounds = true;
player.animations.add('up', [9,10,11], 10, true);
player.animations.add('down', [0,1,2], 10, true);
player.animations.add('left', [3,4,5], 10, true);
player.animations.add('right', [6,7,8], 10, true);
player.speed = 200;
this.player.body.collideWorldBounds = true;
this.player.animations.add('up', [9,10,11], 10, true);
this.player.animations.add('down', [0,1,2], 10, true);
this.player.animations.add('left', [3,4,5], 10, true);
this.player.animations.add('right', [6,7,8], 10, true);
this.player.speed = 200;
},
playerMovement: function(){
this.player.body.velocity.y = 0;
this.player.body.velocity.x = 0;
player.body.velocity.y = 0;
player.body.velocity.x = 0;
if(this.cursors.up.isDown) {
this.player.body.velocity.y -= this.player.speed;
this.player.animations.play('up');
player.body.velocity.y -= player.speed;
player.animations.play('up');
}
else if(this.cursors.down.isDown) {
this.player.body.velocity.y += this.player.speed;
this.player.animations.play('down');
player.body.velocity.y += player.speed;
player.animations.play('down');
}
if(this.cursors.left.isDown) {
this.player.body.velocity.x -= this.player.speed;
this.player.animations.play('left');
player.body.velocity.x -= player.speed;
player.animations.play('left');
}
else if(this.cursors.right.isDown) {
this.player.body.velocity.x += this.player.speed;
this.player.animations.play('right');
player.body.velocity.x += player.speed;
player.animations.play('right');
} else {
this.player.animations.stop(null, true);
player.animations.stop(null, true);
}
}