P4nch0 Posted August 4, 2016 Share Posted August 4, 2016 Please help me with this collision in phaser. I have defined player: if (zmiennastart != 0){ this.player = this.game.add.sprite(parseInt(playerx), parseInt(playery), 'player', 5); } else {this.player = this.game.add.sprite(parseInt(playerx), parseInt(playery), 'player', 5);} this.game.physics.arcade.enable(this.player ); this.player.anchor.set(.5); this.player.scale.x=70/100; this.player.scale.y=65/100; this.player.body.setSize(4, 25, 0, 15); //this.game.debug.body(this.player); //the camera will follow the player in the world this.game.camera.follow(this.player); this.physics.startSystem(Phaser.Physics.Arcade); I have objects created in TIlededitor i objectsLayer. Objects have added type in program "item" And there is define group "this.items". Objects are adding by type "item". createItems: function() { //create items this.items = this.game.add.group(); this.items.enableBody = true; var item; result = this.findObjectsByType('item', this.map, 'objectsLayer'); result.forEach(function(element){ this.createFromTiledObject(element, this.items); }, this); }, I forgot to add, this is function creating sprite to objects: //find objects in a Tiled layer that containt a property called "type" equal to a certain value findObjectsByType: function(type, map, layer) { var result = new Array(); map.objects[layer].forEach(function(element){ if(element.properties.type === type) { //Phaser uses top left, Tiled bottom left so we have to adjust //also keep in mind that the cup images are a bit smaller than the tile which is 16x16 //so they might not be placed in the exact position as in Tiled element.y -= map.tileHeight; result.push(element); } }); return result; }, //create a sprite from an object createFromTiledObject: function(element, group) { var sprite = group.create(element.x, element.y, element.properties.sprite); //copy all properties to the sprite Object.keys(element.properties).forEach(function(key){ sprite[key] = element.properties[key]; }); }, There is collisione before player and groups. this.game.physics.arcade.overlap(this.player, this.items, this.funkcjaitem, null, this); Player is colliding with objects, but passes through. Can anyone help me to do this collision that player collide with objects but cant pass through them? I will be very gratefull. Link to comment Share on other sites More sharing options...
symof Posted August 4, 2016 Share Posted August 4, 2016 this.game.physics.arcade.collide(this.player, this.items, this.funkcjaitem, null, this); Use collide instead of overlap. P4nch0 1 Link to comment Share on other sites More sharing options...
P4nch0 Posted August 4, 2016 Author Share Posted August 4, 2016 Sory, i dont understand. Where i must add it? i have it in update function. Link to comment Share on other sites More sharing options...
symof Posted August 4, 2016 Share Posted August 4, 2016 6 minutes ago, P4nch0 said: Sory, i dont understand. Where i must add it? i have it in update function. It's good in the update function but you are using arcade.overlap and you need to change that to arcade.collide http://phaser.io/examples/v2/arcade-physics/sprite-vs-sprite P4nch0 1 Link to comment Share on other sites More sharing options...
P4nch0 Posted August 4, 2016 Author Share Posted August 4, 2016 OMG, that was easy, thanks Ok, now player is colliding with objects, but obects are movable. When i try to meke it immovable: createItems: function() { //create items this.items = this.game.add.group(); this.items.enableBody = true; this.items.body.immovable = true; var item; result = this.findObjectsByType('item', this.map, 'objectsLayer'); result.forEach(function(element){ this.createFromTiledObject(element, this.items); }, this); }, I have that wrong: TypeError: this.items.body is undefined TypeError: this.items.body is undefined Do You know why? Link to comment Share on other sites More sharing options...
symof Posted August 4, 2016 Share Posted August 4, 2016 createItems: function() { //create items this.items = this.game.add.group(); this.items.enableBody = true; //this.items.body.immovable = true; this.items.setAll('body.immovable', true); var item; result = this.findObjectsByType('item', this.map, 'objectsLayer'); result.forEach(function(element){ this.createFromTiledObject(element, this.items); }, this); }, Since it's a group you need to use setAll to change the values of all members of the group. P4nch0 1 Link to comment Share on other sites More sharing options...
P4nch0 Posted August 4, 2016 Author Share Posted August 4, 2016 Ok, i have done that what You talk, now i dont heave wrong, but objects are still movable.. Do you have idea why? Link to comment Share on other sites More sharing options...
symof Posted August 4, 2016 Share Posted August 4, 2016 10 minutes ago, P4nch0 said: Ok, i have done that what You talk, now i dont heave wrong, but objects are still movable.. Do you have idea why? http://phaser.io/docs/2.6.1/Phaser.Physics.Arcade.Body.html#moves You also need to use: this.items.setAll('body.moves', false); P4nch0 1 Link to comment Share on other sites More sharing options...
P4nch0 Posted August 4, 2016 Author Share Posted August 4, 2016 Man, thans a lot for You help, but still nothing.. Now it looks that: createItems: function() { //create items this.items = this.game.add.group(); this.items.enableBody = true; //this.items.body.immovable = true; this.items.setAll('body.immovable', true); this.items.setAll('body.moves', false); var item; result = this.findObjectsByType('item', this.map, 'objectsLayer'); result.forEach(function(element){ this.createFromTiledObject(element, this.items); }, this); }, Link to comment Share on other sites More sharing options...
symof Posted August 4, 2016 Share Posted August 4, 2016 Try it like this createItems: function() { //create items this.items = this.game.add.group(); this.items.enableBody = true; var result = this.findObjectsByType('item', this.map, 'objectsLayer'); result.forEach(function(element){ this.createFromTiledObject(element, this.items); }, this); this.items.setAll('body.immovable', true); this.items.setAll('body.moves', false); }, You were first changing the body then creating them. This was the order is correct as you create them from tiles then changed their body attributes. You also seem to not use var item; So it might be safe to delete that also, unless you have more code in that function, and I added var in front of result as that seems to be a local variable. P4nch0 1 Link to comment Share on other sites More sharing options...
P4nch0 Posted August 4, 2016 Author Share Posted August 4, 2016 Yeeeh, You are right! Man thanks a lot, nice to see helpfull person who know a lot of phaser My project is still creating, i have done a lot but i am still learning java script, fine to know about anyone can help. Thanks! symof 1 Link to comment Share on other sites More sharing options...
Recommended Posts