Code Looper Posted January 6, 2017 Share Posted January 6, 2017 Hi, I am a Phaser Hobbyist. I have been trying to figure out how to identify which members of GroupA are overlapping GroupB. I have used: this.GroupA.children.z Which is useful, but using a for loop, I just can't find that illusive overlap between opposing groups in order to affect the overlapping group members. Please Help Link to comment Share on other sites More sharing options...
Code Looper Posted January 6, 2017 Author Share Posted January 6, 2017 updateOverlaps: function () { 'use strict'; var i = 0; this.game.physics.arcade.overlap(this.player, this.coin, this.takeCoin, null, this); this.game.physics.arcade.overlap(this.player, this.enemies, this.playerDie, null, this); this.game.physics.arcade.overlap(this.enemies, this.friends, this.enemyFriendFinder(this.enemies.children.z, this.friends.children.z), null, this ); }, enemyFriendFinder: function (enemyId, friendId) { 'use strict'; this.enemyId = enemyId; this.friendId = friendId; console.log(this.enemyId, ' Has overlapped ', this.friendId); }, Link to comment Share on other sites More sharing options...
Code Looper Posted January 6, 2017 Author Share Posted January 6, 2017 I tried without the for loop... Link to comment Share on other sites More sharing options...
Code Looper Posted January 6, 2017 Author Share Posted January 6, 2017 updateOverlaps: function () { 'use strict'; this.game.physics.arcade.overlap(this.player, this.coin, this.takeCoin, null, this); this.game.physics.arcade.overlap(this.player, this.enemies, this.playerDie, null, this); var i = 0; for (i; i < 3; i = i +1) { this.game.physics.arcade.overlap(this.enemies, this.friends, this.enemyFriendFinder(this.enemies.children[i].z, this.friends.children[i].z), null, this ); } }, enemyFriendFinder: function (enemyId, friendId) { 'use strict'; this.enemyId = enemyId; this.friendId = friendId; console.log(this.enemyId, ' Has overlapped ', this.friendId); }, Link to comment Share on other sites More sharing options...
Code Looper Posted January 6, 2017 Author Share Posted January 6, 2017 The code above seems to show me doesn't even show correct overlaps Link to comment Share on other sites More sharing options...
Code Looper Posted January 6, 2017 Author Share Posted January 6, 2017 To clarify, I want to use the physics overlap statement to find which group members are overlapping, then use the handler function to do stuff to the particular group members. Link to comment Share on other sites More sharing options...
Code Looper Posted January 6, 2017 Author Share Posted January 6, 2017 Other code works fine doing this: friendFinder: function () { 'use strict'; var i = 0; //////////////////////////////////////////////////////////////////////////////////////////// // FINDING FRIENDS WHO ARE NOT EATING // // This code first finds the x and y position of thr friends group members, then // we find find the x and y position of the player. // // We check to see if any of the friends group // members have stopped moving ( Eating a carrot ). // // THEN, we check to see if the friends group members are within range of the player. // // If a friends group member is in range of the player, then // the x velocity of the particular friend is set to zero, stopping the friend's movement. // // Then, the id of the friend in question is passed to the friendEncounter function. for (i; i < 3; i = i + 1) { var friendsChildPosX = this.friends.children[i].body.position.x, playerPosY = this.player.body.position.y, friendsChildPosY = this.friends.children[i].body.position.y, playerPosX = this.player.body.position.x; if (this.friends.children[i].body.velocity.x === 0) { null; } else if ((friendsChildPosX > playerPosX - 10) && (friendsChildPosX < playerPosX + 10) && (friendsChildPosY === playerPosY)) { this.friends.children[i].body.velocity.x = 0; this.friendEncounter(i); } } }, friendEncounter: function (friendId) { 'use strict'; //////////////////////////////////////////////////////////////////////////////////////////// // WHEN PLAYER ENCOUNTERS A HAPPY FRIEND ('Happy Friends', are eating a Carrot) // // This code, first uses the friendId, to visually change the // state of the stopped friend from moving to eating, then // the number of carrots held by the player is updated. this.friendId = friendId; this.friends.children[this.friendId].loadTexture('friendEating', 0); this.game.global.score = this.game.global.score - 1; this.scoreLabel.text = ('carrot: ' + this.game.global.score); // //////////////////////////////////////////////////////////////////////////////////////////// } Although we are not dealing with two groups here. Just a group and a sprite. Link to comment Share on other sites More sharing options...
Recommended Posts