Jump to content

P2 Collision problem with frames


Rafaelx
 Share

Recommended Posts

Hi all I have a small problem, I'm trying to determine my score by the type of sprite that you hit with your bullets, this type is defined to the player by a small sprite on top of the screen, kind of like if you hit matching one you get 100 points if not 50 points see my code below. Can someone correct me in what I'm doing wrong?

///////I load my atlas

  this.load.atlas('Monsters', 'asset/game1/Monstruos/monstruos.png', 'asset/game1/Monstruos/monstruos.json');

//////This is my sprite that determines the type

        createFiguraCompare: function(){
        
           
// create a sprite, random frame 0..4
this.figuritaspega = this.game.add.sprite(0, 0, 'Monsters');
this.figuritaspega.frame = this.rnd.integerInRange(0,4);

// create a group
this.figuritaspegaG = this.game.add.group(); // <- function call
this.figuritaspegaG.add(this.figuritaspega);

// scale entire group and reposition group
this.figuritaspegaG.scale.set(0.5 , 0.5 );

// notice that the sprite position is relative to the group position
this.figuritaspegaG.x = 800;
this.figuritaspegaG.y = 140;
        
        
    },

////////this is my moving sprites that you must hit with the bullets

        makeOneFigura: function() {
          
        this.figura = this.game.add.group();
        this.figura.enableBody = true;    
        this.figura.physicsBodyType = Phaser.Physics.P2JS;
    //         for (var i = 0; i < 5; i++){
        this.figura.createMultiple(100, 'Monsters', 0, false);  
    //         }
        this.figura.setAll('anchor.x', 0.7);
        this.figura.setAll('anchor.y', 0.7);
        this.figura.setAll('outOfBoundsKill', true);
        this.figura.setAll('checkWorldBounds', true); 


        
        
        
    },
    
      makeFiguras: function(x, y){ 

   if (this.timerFiguras) {
            this.figuras = this.figura.getFirstExists(false);
       if (this.figuras) {
            this.figuras.reset(500, 0);
             this.figuras.frame = this.game.rnd.integerInRange(0,4);  
             this.figuras.body.setCollisionGroup(this.figuraCG);
            this.figuras.body.collides(this.bulletCG); 
            this.figuras.body.velocity.y = 1400;
        }
   };

    },

////and my collision handler My problem is mainly here


        collisionBulletFigura: function(bullet, figura, score, scoreText, figuritaspegaG) {
        
            if (this.figura.currentFrame === this.figuritaspegaG.currentFrame){
       figura.sprite.kill();
        bullet.sprite.kill();
            this.score += 100;
            this.scoreText.text = this.score;
       }else {
                  figura.sprite.kill();
        bullet.sprite.kill()
        
         this.score += 50;
            this.scoreText.text = this.score;
       }

            
        
    },

 

Link to comment
Share on other sites

I don't use P2 that often, so I am not sure. Although I don't see you call the collision handler (collisionBulletFigura), but you might have cut that out.

I always use this handler I took from on of the example and works like a charm. It may be a use to you as well.

First you add the collision handler to the bullet/player object:

bullet.body.onBeginContact.add(this.hitDetection, this);

Collision handler:

hitDetection: function(body, bodyB, shapeA, shapeB, equation) {
   if (body) {
      //The key is the name of the sprite used by the object you want to detect
      //E.g. if you want +100 score on objects with the "blueTarget" sprite and
      //+50 on all others you can do this:
      if (body.sprite.key == "blueTarget") {
         score += 100;
         //Do other stuff
      }
      else {
         score += 50;
         //Do other stuff
      }
   }
},

I used this example: http://phaser.io/examples/v2/p2-physics/contact-events

EDIT: I did set the code box to Javascript, but for some reasons the colors are still incorrect.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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