Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Identify individual Group vs Group Overlap


Code Looper
 Share

Recommended Posts

 
    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

    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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...