collide with other trees
This commit is contained in:
parent
e96a101927
commit
695268518a
32
src/game.js
32
src/game.js
|
@ -3,6 +3,11 @@ var player;
|
|||
|
||||
// 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', {
|
||||
preload: function(){
|
||||
game.load.spritesheet('character', 'assets/arrow_tileset.png', 32, 32);
|
||||
|
@ -17,17 +22,21 @@ game.state.add('play', {
|
|||
this.layers = {};
|
||||
},
|
||||
create: function(){
|
||||
// enable p2physics
|
||||
game.physics.startSystem(Phaser.Physics.P2JS);
|
||||
|
||||
// use cursors for movement
|
||||
this.cursors = this.game.input.keyboard.createCursorKeys();
|
||||
|
||||
//make a layer
|
||||
this.createLayers('forest1');
|
||||
this.createPlayer();
|
||||
game.physics.p2.enable(this.player)
|
||||
},
|
||||
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.debug.bodyInfo(player, 32, this.game.world.height - 100);
|
||||
|
@ -36,29 +45,26 @@ game.state.add('play', {
|
|||
|
||||
/* CUSTOM METHODS VVVVV */
|
||||
createLayers: function(map_name){
|
||||
// load the tilemap
|
||||
this.map = game.add.tilemap(map_name);
|
||||
// apply the tileset
|
||||
this.map.addTilesetImage('kenney_complete', 'tiles');
|
||||
|
||||
|
||||
// create all layers
|
||||
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']);
|
||||
// collisions between 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");
|
||||
|
||||
for (var i in TREE_IDS){
|
||||
this.map.setCollision(TREE_IDS[i], true, "Trees");
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
createPlayer: function(){
|
||||
// create the character, enble physics on it, and add animations
|
||||
player = game.add.sprite(80, 144, 'character');
|
||||
game.physics.enable(player);
|
||||
|
||||
|
@ -72,9 +78,11 @@ game.state.add('play', {
|
|||
|
||||
},
|
||||
playerMovement: function(){
|
||||
// zero out velocity
|
||||
player.body.velocity.y = 0;
|
||||
player.body.velocity.x = 0;
|
||||
|
||||
// standard move and animate code.
|
||||
if(this.cursors.up.isDown) {
|
||||
player.body.velocity.y -= player.speed;
|
||||
player.animations.play('up');
|
||||
|
|
Loading…
Reference in New Issue
Block a user