Jump to content

Groups overlap


OGCrab
 Share

Recommended Posts

So I am trying to create a game like battle cats if any one knows what that is. But i have to test if the enemy sprite is overlapping with the player sprites that I am spawning. I am using a group to spawn them so that i can do the this.player.create(x, y, 'image').

But if 1 sprite from the groups collide with the enemy i have to stop them by setting velocity to 0 so that it will stop moving and do some damage. The problem is that I dont know how to only stop the sprite that is actually touching the enemy sprite from a group.

So each time 1 sprite from the group touches the enemy sprite, all the sprites stop moving.

Any ideas on how to fix this? or if there is an easier way of doing this I would like to know :)

Thank you in advance for any help you can give me :)

Link to comment
Share on other sites

@squilibob sure. I Will do it later when i get home

Edit: i found out how to stop Them one by one. I just made Them collide instead of stopping their movement which i dont Know how i did not realize In the first place.

I still need to get em execute a function only for the one who collides with the enemy

Link to comment
Share on other sites

@squilibob

Here is the code

this.playerGroup.forEach(function(player){
             game.physics.arcade.overlap(player, this.enemyPlayerGroup, this.onCollision, null, this);
             game.physics.arcade.overlap(Adventure.enemyBase, player, this.onBaseCollision, null, this);
             game.physics.arcade.collide(player, this.player2);
         }, this); 

But i tryed to do the thing where they collide and it works perfect if there is only 1 sprite from the playerGroup but if i spawn 1 more the sprite that is colliding with the enemy starts moving the enemy. 

And if you did not already know im trying to get it so that if a sprite from the player group collides or overlaps with an enemy. I can make it so only that sprite will stop and do damage to the enemy, while all other sprites keep moving towards the middle and if they hit the enemy aswell they are also gonna start doing some damage.

I found out if i used this code:

spawnPlayer: function(game){
        
        Adventure.Vicci1 = game.add.sprite(740, 960, 'Vicci');
        
        game.physics.enable(Adventure.Vicci1, Phaser.Physics.ARCADE);
        
        Adventure.Vicci1.body.velocity.x = 200;
        
        game.playerTestGroup.add(Adventure.Vicci1);
        
    }

It would spawn a new one each time. But if more than 1 of the spawned players hit the enemy, they will start to push the enemy even though i have them colliding.

Link to comment
Share on other sites

From what I can tell from your code and what you have described that you want to happen, you want to pass a process callback function that will set velocity to zero when a player engages an enemy. You can do it in the overlap or collide function but you probably don't need the collide function at all.

game.physics.arcade.overlap(player, this.enemyPlayerGroup, this.onCollision, this.onEngage, this);

onEngage: function(player) {
  player.velocity.x = 0;
  player.velocity.y = 0;
}

 

Link to comment
Share on other sites

@squilibob Yes, i want to stop the spawned players each time they hit an enemy. But the Thing is if i am using a group they Will all stop even if they dont hit the enemy. I also tryed not to use a group(which requires much more code as i am creating a new Sprite every time) when i didnt Use a group everytime the first spawned player hits the enemy they both stop, but when the next spawned player comes In and collides with both the first player and the enemy it just starts pushing the enemy with a reduced x velocity and i dont Really Know how that works

Also neither the player or the enemy is moved by the "player" they are moved by the game In a straight line like those TD games Where on each side of the map there is a tower and you scroll the map to see each end

hope some1 can help me with this issue :)

 

Link to comment
Share on other sites

@squilibob

Here you go:

The code is a bit messy as i have tryed alot of different things

Adventure.LevelOneTest = function(game, index, player){
    
    
    game.layer;
    game.layer2;
    
    game.speedMult = 0.7;
    
    game.friction = 0.99;    
    
    this.mapSpeed = 1;
    
    Adventure.baseHealthText = null;
    Adventure.enemyBaseHealthText = null;
    Adventure.baseHealth = 30;
    Adventure.enemyBaseHealth = 30;
    
    this.playerTestGroup = null;
    
    Adventure.Vicci1 = null;
    
};

Adventure.LevelOneTest.prototype = {
    
     create: function(index, player){  
          // the big map to scroll
         
        this.scrollingMap = this.game.add.image(0, 0, 'map2');
         
        this.floor = this.scrollingMap.addChild(game.add.sprite(0, 1000, 'floor'));
            
        Adventure.base = this.scrollingMap.addChild(game.add.sprite(640, 870, 'base'));
            
        Adventure.enemyBase = this.scrollingMap.addChild(game.add.sprite(4350, 870, 'base'));
         
        this.spawnPanel = this.game.add.image(0, 778, 'spawnPanel');
         this.spawnPanel.width = game.width;
         
         this.spawnPlayer = this.spawnPanel.addChild(this.game.add.sprite(1600, 0, 'spawnPlayer'));
         
         this.spawnPlayer.inputEnabled = true;
         
         this.spawnPlayer.events.onInputDown.add(this.playerSpawn, this);
         
         this.playerTestGroup = this.scrollingMap.addChild(game.add.group());
         
         this.playerGroup = this.scrollingMap.addChild(game.add.group());
         this.enemyPlayerGroup = this.scrollingMap.addChild(game.add.group());
         
         this.playerGroup.enableBody = true;
         this.enemyPlayerGroup.enableBody = true;
            game.physics.arcade.enable(this.floor);
         
            this.floor.body.immovable = true;
         
            this.scrollingMap.height = game.height;
         
            Adventure.baseHealthText = Adventure.base.addChild(this.game.add.text(50, -50, Adventure.baseHealth));
            Adventure.enemyBaseHealthText = Adventure.enemyBase.addChild(this.game.add.text(50, -50, '30', {font: "18px Arial Black", fill: "#000"}));

            Adventure.foodTotalText = this.spawnPanel.addChild(this.game.add.text(50, -50, '20', {font: "18px Arial Black", fill: "#000"}));
         
              // map will accept inputs
            this.scrollingMap.inputEnabled = true;
              // map can be dragged
            this.scrollingMap.input.enableDrag(false);
              // custom property: we save map position
            this.scrollingMap.savedPosition = new Phaser.Point(this.scrollingMap.x, this.scrollingMap.y);
              // custom property: the map is not being dragged at the moment
            this.scrollingMap.isBeingDragged = false; 
              // custom property: map is not moving (or is moving at no speed)
            this.scrollingMap.movingSpeed = 0; 
              // map can be dragged only if it entirely remains into this rectangle
            this.scrollingMap.input.boundsRect = new Phaser.Rectangle(game.width - this.scrollingMap.width, game.height - this.scrollingMap.height, this.scrollingMap.width * 2 - game.width, this.scrollingMap.height * 2 - game.height);
              // when the player starts dragging...
              this.scrollingMap.events.onDragStart.add(function(){
                   // set isBeingDragged property to true
                this.scrollingMap.isBeingDragged = true;
                   // set movingSpeed property to zero. This will stop moving the map
                   // if the player wants to drag when it's already moving
                this.scrollingMap.movingSpeed = 0;
        }, this);
        // when the player stops dragging...
        this.scrollingMap.events.onDragStop.add(function(){
               // set isBeingDragged property to false
            this.scrollingMap.isBeingDragged = false;
        }, this);   

        game.physics.startSystem(Phaser.Physics.ARCADE);
         
        this.foodTimer = 0;
         
        this.attackTimer = 0; 
        this.baseAttackTimer = 0;
        this.gameOverTimer = 0;
         
        Adventure.player2 = this.game.add.sprite(4100, 940, 'Ivan');
         
        Adventure.player2.health = Adventure.player.health;
         
        game.physics.arcade.enable(Adventure.player2);
        
        game.physics.arcade.enable(Adventure.base);
        game.physics.arcade.enable(Adventure.enemyBase); 

        Adventure.player2.body.velocity.x = -200;

        Adventure.player2.body.gravity.y = 2000;
    
        this.enemyPlayerGroup.add(Adventure.player2);
         
     },
     update:function(){
         
         //this.playerGroup.forEach(function(player){
             //game.physics.arcade.overlap(player, this.enemyPlayerGroup, this.onCollision, null, this);
             //game.physics.arcade.overlap(Adventure.enemyBase, player, this.onBaseCollision, null, this);
             //game.physics.arcade.collide(player, this.player2);
         //}, this); 
         
         //if(game.physics.arcade.overlap(this.player, this.enemyPlayerGroup, this.onCollision, null, this) == true){
            //this.onCollision();
         //}
         //else{
            //this.player.body.velocity.x = 200 
            //this.playerGroup.setAll('body.velocity.x', 200)
         //}
         
         game.physics.arcade.collide(Adventure.player2, this.floor);
         
         this.foodTimer += this.time.elapsed;
         
         if(this.foodTimer > 300){
             Adventure.food++;
             this.foodTimer = 0;
         }
         
         game.physics.arcade.overlap(this.player, Adventure.enemyBase, this.onBaseCollision, null, this);
         
         
         if(Adventure.player2.health <= 0){
             Adventure.player2.kill();
         }
         
         if(Adventure.enemyBaseHealth <= 0){
             Adventure.enemyBase.kill();
             
             this.gameOverTimer += this.time.elapsed;
             
             if(this.gameOverTimer > 1000){
                 game.state.start("GameTitle");
             }
         }
         
          // if the map is being dragged...
          if(this.scrollingMap.isBeingDragged){
               // save current map position
               this.scrollingMap.savedPosition = new Phaser.Point(this.scrollingMap.x, this.scrollingMap.y);
          }
          // if the map is NOT being dragged...
          else{
               // if the moving speed is greater than 1...
               if(this.scrollingMap.movingSpeed > 1){
                    // adjusting map x position according to moving speed and angle using trigonometry
                    this.scrollingMap.x += this.scrollingMap.movingSpeed * Math.cos(this.scrollingMap.movingangle);
                    // adjusting map y position according to moving speed and angle using trigonometry
                    this.scrollingMap.y += this.scrollingMap.movingSpeed * Math.sin(this.scrollingMap.movingangle);
                    // keep map within boundaries
                    if(this.scrollingMap.x < game.width - this.scrollingMap.width){
                         this.scrollingMap.x = game.width - this.scrollingMap.width;
                    }
                    // keep map within boundaries
                    if(this.scrollingMap.x > 0){
                         this.scrollingMap.x = 0;
                    }
                    // keep map within boundaries
                    if(this.scrollingMap.y < game.height - this.scrollingMap.height){
                         this.scrollingMap.y = game.height - this.scrollingMap.height;
                    }
                    // keep map within boundaries
                    if(this.scrollingMap.y > 0){
                         this.scrollingMap.y = 0;
                    }
                    // applying friction to moving speed
                    this.scrollingMap.movingSpeed *= game.friction;
                    // save current map position
                    this.scrollingMap.savedPosition = new Phaser.Point(this.scrollingMap.x, this.scrollingMap.y);
               }
               // if the moving speed is less than 1...
               else{
                    // checking distance between current map position and last saved position
                    // which is the position in the previous frame
                    var distance = this.scrollingMap.savedPosition.distance(this.scrollingMap.position);
                    // same thing with the angle
                    var angle = this.scrollingMap.savedPosition.angle(this.scrollingMap.position);
                    // if the distance is at least 4 pixels (an arbitrary value to see I am swiping)
                    if(distance > 4){
                         // set moving speed value
                         this.scrollingMap.movingSpeed = distance * game.speedMult;
                         // set moving angle value
                         this.scrollingMap.movingangle = angle;
                    }
               }
          }
         
         Adventure.foodTotalText.setText(Adventure.food, {font: "18px Arial Black", fill: "#000"});        
         Adventure.enemyBaseHealthText.setText(Adventure.enemyBaseHealth + ' Health', {font: "18px Arial Black", fill: "#000"});
         
         game.physics.arcade.collide(Adventure.Vicci1, Adventure.player2);
         
         /*if(this.PVicci1 != null){
                this.playerGroup.add(this.PVicci1);
                if(game.physics.arcade.overlap(this.PVicci1, this.player2) == true){
                    this.PVicci1.body.velocity.x = 0;
                    this.player2.body.velocity.x = 0;
                }
             else {
                 this.player2.body.velocity.x = -200;
                 this.PVicci1.body.velocity.x = 200;
             }
                
            }
         if(this.PVicci2 != null){
                this.playerGroup.add(this.PVicci2);
                console.log(this.PVicci2.body.velocity.x);
                if(game.physics.arcade.overlap(this.PVicci2, this.player2) == true){
                    this.PVicci2.body.velocity.x = 0;
                    this.player2.body.velocity.x = 0;
                }
             else {
                 this.player2.body.velocity.x = -200;
                 this.PVicci2.body.velocity.x = 200;
             }
                    
         }
        
         game.physics.arcade.collide(this.playerGroup, this.player2);
         game.physics.arcade.collide(this.playerGroup, this.floor);
         */
     },
    
    
    
    onCollision: function(){
        
        this.attackTimer += this.time.elapsed;
        
        if(this.attackTimer > 1000){
            console.log(this.player2.health);
            this.player2.health -= Adventure.player.damage;
            this.attackTimer = 0;
            
        }
        
    },
    
    onBaseCollision: function(){
        
        this.baseAttackTimer += this.time.elapsed;
        
        if(this.baseAttackTimer > 1000){
            console.log(Adventure.enemyBaseHealth);
            Adventure.enemyBaseHealth -= Adventure.player.damage;
            this.baseAttackTimer = 0;
        }
    },
    
    playerSpawn: function(player){
        
        if(Adventure.food - Adventure.player.cost >= 0){
            
            Adventure.spawnPlayers.spawnPlayer(this);
            
            /*if(this.PVicci1 != null && this.PVicci2 == null){
                this.PVicci2 = this.scrollingMap.addChild(this.game.add.sprite(740, 960, 'Vicci'));
                game.physics.arcade.enable(this.PVicci2);
                this.PVicci2.body.velocity.x = 200;
                this.PVicci2.body.gravity.y = 0;
                
            }  
            
            
            if(this.PVicci1 == null){
                this.PVicci1 = this.scrollingMap.addChild(this.game.add.sprite(740, 960, 'Vicci'));
                game.physics.arcade.enable(this.PVicci1);
                this.PVicci1.body.velocity.x = 200;
                this.PVicci1.body.gravity.y = 0;
            }
            
            if(this.PVicci1 != null){
            } 
            
            if(this.PVicci2 != null){
            }
            */
            Adventure.food -= Adventure.player.cost;
            
        }
        
    },

};

Adventure.spawnPlayers = {
    
    spawnPlayer: function(game){
        
        Adventure.Vicci1 = game.add.sprite(740, 960, 'Vicci');
        
        game.physics.enable(Adventure.Vicci1, Phaser.Physics.ARCADE);
        
        Adventure.Vicci1.body.velocity.x = 200;
        
        game.playerTestGroup.add(Adventure.Vicci1);
        
        
        
        
    }

    
    
    
    
}

 

Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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