Jump to content

Collision not detected


denisb
 Share

Recommended Posts

I can't figure out why my spacecraft overlaps with the asteroid and does not collide. I used Phaser.Utils.Debug and found that the display 

objects to not touch each other. Are the bodies in different containers? Thanks for your help.

 

var MyGame={};

MyGame.Level1=function(){
    this.spacecraft;
    this.asteroid;
    this.cursors;
};

MyGame.Level1.prototype={
    
    preload:function(){
        this.load.image('spacecraft', 'assets/spacecraft.png');
        this.load.image('asteroid', 'assets/asteroid.png');
    },
    
    create:function(){
        
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        this.asteroid=this.add.sprite(500,100,'asteroid');
        this.physics.arcade.enable(this.asteroid,Phaser);
        this.asteroid.body.immovable=true;
        
        this.spacecraft=this.add.sprite(0,100,'spacecraft');
        this.game.physics.arcade.enable(this.spacecraft);

    },
    
    update:function(){
        
        this.physics.arcade.collide(this.spacecraft,this.asteroid,this.collisionHandler,this.processHandler,this);
        
        if(this.game.input.keyboard.isDown(Phaser.Keyboard.UP)){
            this.spacecraft.body.y-=4;
        }
        else if(this.game.input.keyboard.isDown(Phaser.Keyboard.DOWN)){
            this.spacecraft.body.y+=4;
        }    
        if(this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT)){
            this.spacecraft.body.x-=4;
        }
        else if(this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
            this.spacecraft.body.x+=4;
        }
        
    },

    processHandler:function(spacecraft,asteroid){
        return true;
    },
    
    collisionHandler:function(spacecraft,asteroid){
        spacecraft.kill();
    }
};

Link to comment
Share on other sites

drhayes is correct.  In order to correctly use the physics system to detect collisions, you need to give things velocity rather than controlling their positions directly with x and y.  However, if you do use x and y, the overlap() call still works because this just checks for rectangle overlap on the bodies, and doesn't care about velocity.  This can be useful in a few instances, depending on your use case.  As a general statement, however, using physics and velocity is your best bet for consistent game logic.

Link to comment
Share on other sites

Oh, that makes sense! Thank you. One more question then. If I have a character carrying something and I want to check for a collision with a table, to see if that something was put ON the table, then how am I supposed to move it?

Right now I use a group to 'attach' the object to the hero and I move the whole group which by itself has no body, so I suppose I should rather add the object as a child of hero sprite? Will it inherit parent's velocity so I could detect it's collision with the table or is it basically pointless to check for collision of a child at all?

We're talking about Arcade physics of course.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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