collide with other trees

This commit is contained in:
Tyrel Souza 2016-04-08 00:19:41 -04:00
parent e96a101927
commit 695268518a

View File

@ -3,6 +3,11 @@ var player;
// http://talestolduntold.blogspot.com/2015/06/tilemaps-with-invisible-collision-layer.html // http://talestolduntold.blogspot.com/2015/06/tilemaps-with-invisible-collision-layer.html
var TREE_IDS = [
181, 182, 208, 209, 183, // Forest
184, 185, 211, 212, 186, // Fall
];
game.state.add('play', { game.state.add('play', {
preload: function(){ preload: function(){
game.load.spritesheet('character', 'assets/arrow_tileset.png', 32, 32); game.load.spritesheet('character', 'assets/arrow_tileset.png', 32, 32);
@ -17,17 +22,21 @@ game.state.add('play', {
this.layers = {}; this.layers = {};
}, },
create: function(){ create: function(){
// enable p2physics
game.physics.startSystem(Phaser.Physics.P2JS); game.physics.startSystem(Phaser.Physics.P2JS);
// use cursors for movement
this.cursors = this.game.input.keyboard.createCursorKeys(); this.cursors = this.game.input.keyboard.createCursorKeys();
//make a layer
this.createLayers('forest1'); this.createLayers('forest1');
this.createPlayer(); this.createPlayer();
game.physics.p2.enable(this.player) game.physics.p2.enable(this.player)
}, },
update: function(){ update: function(){
this.playerMovement(); this.playerMovement(); // move player
// check for collider, how does this work, i'm not using arcade?
game.physics.arcade.collide(player, this.layers['trees']); game.physics.arcade.collide(player, this.layers['trees']);
game.debug.bodyInfo(player, 32, this.game.world.height - 100); game.debug.bodyInfo(player, 32, this.game.world.height - 100);
@ -36,29 +45,26 @@ game.state.add('play', {
/* CUSTOM METHODS VVVVV */ /* CUSTOM METHODS VVVVV */
createLayers: function(map_name){ createLayers: function(map_name){
// load the tilemap
this.map = game.add.tilemap(map_name); this.map = game.add.tilemap(map_name);
// apply the tileset
this.map.addTilesetImage('kenney_complete', 'tiles'); this.map.addTilesetImage('kenney_complete', 'tiles');
// create all layers
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 // collisions between trees
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"); game.physics.p2.convertTilemap(this.map, "Trees");
tree_ids = [181,182,208,209, 183];
for (var i in tree_ids){ for (var i in TREE_IDS){
this.map.setCollision(tree_ids[i], true, "Trees"); this.map.setCollision(TREE_IDS[i], true, "Trees");
} }
}, },
createPlayer: function(){ createPlayer: function(){
// create the character, enble physics on it, and add animations
player = game.add.sprite(80, 144, 'character'); player = game.add.sprite(80, 144, 'character');
game.physics.enable(player); game.physics.enable(player);
@ -72,9 +78,11 @@ game.state.add('play', {
}, },
playerMovement: function(){ playerMovement: function(){
// zero out velocity
player.body.velocity.y = 0; player.body.velocity.y = 0;
player.body.velocity.x = 0; player.body.velocity.x = 0;
// standard move and animate code.
if(this.cursors.up.isDown) { if(this.cursors.up.isDown) {
player.body.velocity.y -= player.speed; player.body.velocity.y -= player.speed;
player.animations.play('up'); player.animations.play('up');