Jump to content

Element destroy on Collision


Somaaniii
 Share

Recommended Posts

var debug;
var source1, source2;
var target = new Phaser.Math.Vector2();
var distanceText;
var coords;
var s = 0;

new Phaser.Game(config);

function preload ()
{
this.load.image(‘Player’, ‘Player.png’);
this.load.image(‘enemy’, ‘enemy.png’);
this.load.image(‘enemy1’, ‘enemy.png’);
this.load.image(‘enemy2’, ‘enemy.png’);

}

function create ()
{

source1 = this.physics.add.image(100, 300, ‘Player’);
source2 = this.physics.add.image(400, 500, ‘enemy’);
source3 = this.physics.add.image(450, 500, ‘enemy1’);
source4 = this.physics.add.image(200, 550, ‘enemy2’);

distanceText = this.add.text(100, 10, ‘’);

}

function update ()
{

 debug = this.add.graphics();

this.input.on('pointerdown', function (pointer) {

    target.x = pointer.x;
    target.y = pointer.y;
    
    // Move at 200 px/s:
    this.physics.moveToObject(source1, target, 200);

  
}, this);




var distance = Phaser.Math.Distance.Between(source1.x, source1.y, target.x, target.y);

if (source1.body.speed > 0)
{
    distanceText.setText('Distance: ' + distance);

    //  4 is our distance tolerance, i.e. how close the source can get to the target
    //  before it is considered as being there. The faster it moves, the more tolerance is required.
    if (distance < 4)
    {
        source1.body.reset(target.x, target.y);
    }
}

this.physics.world.collide(source1, source2, function () {
  source1.setVelocity(0); source2.destroy(); console.log('hit?');
  s++;
});
	this.physics.world.collide(source1, source3, function () {
  source1.setVelocity(0); source3.destroy(); console.log('hit?');
  s++;
});
		this.physics.world.collide(source1, source4, function () {
  source1.setVelocity(0); source4.destroy(); console.log('hit?');
  s++;
});

score = this.add.text(500,30,s);
coords = this.add.text(500, 10, 'Click to set target');
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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