change physics to J2, set collisions on tree layer
This commit is contained in:
parent
cbf40600c2
commit
e96a101927
80
src/game.js
80
src/game.js
@ -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', {
|
game.state.add('play', {
|
||||||
preload: function(){
|
preload: function(){
|
||||||
@ -15,67 +17,81 @@ game.state.add('play', {
|
|||||||
this.layers = {};
|
this.layers = {};
|
||||||
},
|
},
|
||||||
create: function(){
|
create: function(){
|
||||||
game.physics.startSystem(Phaser.Physics.ARCADE);
|
game.physics.startSystem(Phaser.Physics.P2JS);
|
||||||
|
|
||||||
this.cursors = this.game.input.keyboard.createCursorKeys();
|
this.cursors = this.game.input.keyboard.createCursorKeys();
|
||||||
|
|
||||||
|
this.createLayers('forest1');
|
||||||
this.createTileMap('forest1');
|
|
||||||
this.createPlayer();
|
this.createPlayer();
|
||||||
|
game.physics.p2.enable(this.player)
|
||||||
|
|
||||||
this.setCollisions();
|
|
||||||
},
|
},
|
||||||
update: function(){
|
update: function(){
|
||||||
this.playerMovement();
|
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']);
|
/* CUSTOM METHODS VVVVV */
|
||||||
game.physics.collide(this.player, this.layers['trees']);
|
createLayers: function(map_name){
|
||||||
},
|
|
||||||
createTileMap: function(map_name){
|
|
||||||
this.map = game.add.tilemap(map_name);
|
this.map = game.add.tilemap(map_name);
|
||||||
this.map.addTilesetImage('kenney_complete', 'tiles');
|
this.map.addTilesetImage('kenney_complete', 'tiles');
|
||||||
|
|
||||||
|
|
||||||
this.layers['water'] = this.map.createLayer('Water');
|
this.layers['water'] = this.map.createLayer('Water');
|
||||||
this.layers['ground'] = this.map.createLayer('Ground');
|
this.layers['ground'] = this.map.createLayer('Ground');
|
||||||
this.layers['trees'] = this.map.createLayer('Trees');
|
this.layers['trees'] = this.map.createLayer('Trees');
|
||||||
this.layers['misc'] = this.map.createLayer('Misc');
|
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(){
|
createPlayer: function(){
|
||||||
this.player = game.add.sprite(50, 50, 'character');
|
player = game.add.sprite(80, 144, 'character');
|
||||||
game.physics.arcade.enable(this.player);
|
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(){
|
playerMovement: function(){
|
||||||
this.player.body.velocity.y = 0;
|
player.body.velocity.y = 0;
|
||||||
this.player.body.velocity.x = 0;
|
player.body.velocity.x = 0;
|
||||||
|
|
||||||
if(this.cursors.up.isDown) {
|
if(this.cursors.up.isDown) {
|
||||||
this.player.body.velocity.y -= this.player.speed;
|
player.body.velocity.y -= player.speed;
|
||||||
this.player.animations.play('up');
|
player.animations.play('up');
|
||||||
}
|
}
|
||||||
else if(this.cursors.down.isDown) {
|
else if(this.cursors.down.isDown) {
|
||||||
this.player.body.velocity.y += this.player.speed;
|
player.body.velocity.y += player.speed;
|
||||||
this.player.animations.play('down');
|
player.animations.play('down');
|
||||||
}
|
}
|
||||||
if(this.cursors.left.isDown) {
|
if(this.cursors.left.isDown) {
|
||||||
this.player.body.velocity.x -= this.player.speed;
|
player.body.velocity.x -= player.speed;
|
||||||
this.player.animations.play('left');
|
player.animations.play('left');
|
||||||
}
|
}
|
||||||
else if(this.cursors.right.isDown) {
|
else if(this.cursors.right.isDown) {
|
||||||
this.player.body.velocity.x += this.player.speed;
|
player.body.velocity.x += player.speed;
|
||||||
this.player.animations.play('right');
|
player.animations.play('right');
|
||||||
} else {
|
} else {
|
||||||
this.player.animations.stop(null, true);
|
player.animations.stop(null, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user