Jump to content

Physic Collision problem


Liste
 Share

Recommended Posts

Hi,

can someone help me with collision?

I dont understand why player and pipe/vertPipe collision dont work :( I try my best, but still I dont know what I am doing wrong...

Thx for help.

 

var game = new Phaser.Game(800,600, Phaser.AUTO, '', {preload: preload, create: create, update: update});

var timer = 0;

var score = 0;

var pipes = [];

var vertPipes = [];

var i = 0;

 

function preload()

{

    game.load.image('bird', 'img/bird.png');

    game.load.image('title', 'img/title.png');

    game.load.image('bgtile', 'img/tilebg_final.png');

    game.load.image('pipe1', 'img/pipe1Fini.png');

    game.load.image('vertpipe', 'img/vertPipeFini.png');

    //game.load.audio('flap', ['audio/flap.mp3', 'audio/flap.ogg']);

    //game.load.audio('birdFX1', ['audio/birds1.mp3', 'audio/birds1.ogg']);

    //game.load.audio('squakFX1', ['audio/squak.mp3', 'audio/squak.ogg']);

}

 

function create()

{

    

    

    

    

        // IMAGES

    game.stage.backgroundColor = "#4ec0ca";  

    game.add.sprite(190,165, 'title');

    bgtile = game.add.tileSprite(0, 350, 800, game.cache.getImage('bgtile').height, 'bgtile');

    player = game.add.sprite(350, game.world.height-250, 'bird');

    

        // GRAVITY AND COLIDE WORLD

    game.physics.startSystem(Phaser.Physics.ARCADE);

    

    game.physics.arcade.enable(player);

    player.body.bounce.y = 0.2;

    player.body.gravity.y = 200;

    player.body.collideWorldBounds = true;

    

        // DEFINE KEYBOARD AND CURSORS

    keyboard = game.input.keyboard;

    cursors = game.input.keyboard.createCursorKeys();

    

    scoreText = game.add.text(16,16, 'Score: 0', { font: '24px arial', fill: '#fff' });

}

 

function collision(){

    alert('You loose. Your score is: ' + score);   

}

 

function update()

{

    timer++;

    bgtile.tilePosition.x -= 2;

    

    for(var i=0; i<pipes.length; i++){

        pipes.x -= 2;

        game.physics.arcade.collide(player, pipes, collision, null, this);

    }

    for(var i=0; i<vertPipes.length; i++){

        vertPipes.x -= 2;  

        game.physics.arcade.collide(player, vertPipes, collision, null, this);

    }

    

        // KEYBOARD

    if(keyboard.isDown(Phaser.Keyboard.SPACEBAR)) {

        player.body.velocity.y = -150;   

    } else if (cursors.left.isDown) {

        player.body.velocity.x = -120;   

    } else if (cursors.right.isDown) {

        player.body.velocity.x = 120;   

    } else {

        player.body.velocity.x = 0;   

    }

    

        // ADD SCORE

    if(timer/100 % 1 == 0) {

        score += 1;

        scoreText.setText('Score: ' + score);

    }

        // PIPES

    if(timer/300 % 1 == 0 && timer != 0){

            // formula range -- Math.floor(Math.random() * (UpperRange - LowerRange + 1)) + LowerRange;

        randyX = Math.floor(Math.random() * (1000-700 + 1)) + 700; 

        pipes = game.add.sprite(randyX, Math.floor(Math.random() * (500-310 + 1)) + 310, 'pipe1');

        vertPipes = game.add.sprite(randyX, Math.floor(Math.random() * (-75-(-10) + 1)) -10, 'vertpipe');

        

        

    }

}

 

Link to comment
Share on other sites

It doesn't look like you enabled the physics bodies on any one of the sprites you create in that last section of code. There's a few ways you can do this. You can set "enableBody = true" on both your sprites, or you could write "game.physics.arcade.enable({pipes, vertPipes})".

 

Some additional advice: I've found that collisions in Arcade physics can be pretty spotty with high velocity collisions. After a certain amount of overlap, the collision system will ignore the body and let it pass through, which can easily happen if the colliding bodies are moving fast enough. You can work around this, however, by setting OVERLAP_BIAS to some high number (it's set to 4 by default).

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...