Jump to content

Collision Physics problem


Rafaelx
 Share

Recommended Posts

Hi all so I have a small problem, basically I'm trying to kill a moving sprite, it does not matters if it comes from left to right or right to left, but all I want to kill is the one colliding and not the whole group, so to test go to the far right and shoot the block, you will see the new block disappear but not the one that collided, is this possible, I'm providing my game.js below but I created a demo that you can download and test to get a better feel of my problem please help, thanks.

 

Link to Demo

 


BasicGame.Game = function (game) {

    //  When a State is added to Phaser it automatically has the following properties set on it, even if they already exist:

    this.game;      //  a reference to the currently running game (Phaser.Game)
    this.add;       //  used to add sprites, text, groups, etc (Phaser.GameObjectFactory)
    this.camera;    //  a reference to the game camera (Phaser.Camera)
    this.cache;     //  the game cache (Phaser.Cache)
    this.input;     //  the global input manager. You can access this.input.keyboard, this.input.mouse, as well from it. (Phaser.Input)
    this.load;      //  for preloading assets (Phaser.Loader)
    this.math;      //  lots of useful common math operations (Phaser.Math)
    this.sound;     //  the sound manager - add a sound, play one, set-up markers, etc (Phaser.SoundManager)
    this.stage;     //  the game stage (Phaser.Stage)
    this.time;      //  the clock (Phaser.Time)
    this.tweens;    //  the tween manager (Phaser.TweenManager)
    this.state;     //  the state manager (Phaser.StateManager)
    this.world;     //  the game world (Phaser.World)
    this.particles; //  the particle manager (Phaser.Particles)
    this.physics;   //  the physics manager (Phaser.Physics)
    this.rnd;       //  the repeatable random number generator (Phaser.RandomDataGenerator)

    //  You can use any of these from any function within this State.
    //  But do consider them as being 'reserved words', i.e. don't create a property for your own game called "world" or you'll over-write the world reference.
    
     this.bulletTimer = 0;

};

BasicGame.Game.prototype = {

    create: function () {

			                //Enable physics
        
// Set the physics system
			this.game.physics.startSystem(Phaser.Physics.ARCADE);
        
        
        //End of physics 
	
	
        //  Honestly, just about anything could go here. It's YOUR game after all. Eat your heart out!
		
		 this.createBullets();
         this.createTanque();
		 this.makeOneBloque();
        
        this.timerBloques = this.time.events.loop(1500, this.makeBloques, this);
		
    },

    update: function () {

        
        if(this.game.input.activePointer.isDown){
            this.fireBullet();
        }
   
       this.game.physics.arcade.overlap(this.bullets, this.bloque, this.collisionBulletBloque, null, this);
        
	},
    
    createBullets: function() {
        
    this.bullets = this.game.add.group();
    this.bullets.enableBody = true;
    this.bullets.physicsBodyType = Phaser.Physics.ARCADE;
    this.bullets.createMultiple(100, 'bulletSprite');
    this.bullets.setAll('anchor.x', 0.5);
    this.bullets.setAll('anchor.y', 1);
    this.bullets.setAll('outOfBoundsKill', true);
    this.bullets.setAll('checkWorldBounds', true); 
        
       
    },
    
    fireBullet: function(){ 

   if (this.bulletTimer < this.game.time.time) {
            this.bulletTimer = this.game.time.time + 1400;
            this.bullet = this.bullets.getFirstExists(false);
        if (this.bullet) {
            this.bullet.reset(this.tanque.x, this.tanque.y - 20);
            this.bullet.body.velocity.y = -800;
            }
        }

    },
	
	    makeOneBloque: function(){
        
        this.bloquecs = ["bloqueSprite","bloquelSprite"];
         this.bloque = this.game.add.group();
       for (var i = 0; i < 4; i++){
         this.bloque.createMultiple(5, this.bloquecs[Math.floor(Math.random()*this.bloquecs.length)], 0, false);
      }
                     this.bloque.enableBody = true;
         this.game.physics.arcade.enable(this.bloque); 
        this.bloque.setAll('anchor.x', 0.5);
        this.bloque.setAll('anchor.y', 0.5);
        this.bloque.setAll('outOfBoundsKill', true);
        this.bloque.setAll('checkWorldBounds', true); 
        
        
    },
    
        makeBloques: function(){ 
  
         this.bloques = this.bloque.getFirstExists(false);
        if (this.bloques) {

            this.bloques.reset(0, 300);
                 this.bloques.body.kinematic = true;
                this.bloques.body.velocity.x = 500;
            }
        

    },

	    createTanque: function() {
    
        this.tanqueBounds = new Phaser.Rectangle(0, 600, 1024, 150);
        
        this.tanque = this.add.sprite(500, 700, 'tanqueSprite');
        this.tanque.inputEnabled = true;
        this.tanque.input.enableDrag(true);
        this.tanque.anchor.setTo(0.5,0.5);
        this.tanque.input.boundsRect = this.tanqueBounds;
    
        
    },
    
	    collisionBulletBloque: function(bullet, bloques) {
        
       this.bloques.kill();
        this.bullet.kill();
        
    },


    quitGame: function (pointer) {

        //  Here you should destroy anything you no longer need.
        //  Stop music, delete sprites, purge caches, free resources, all that good stuff.

        //  Then let's go back to the main menu.
        this.state.start('MainMenu');

    }

};

 

Basic.7z

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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