Jump to content

This is one thing I wish I could collide into!


phuurup
 Share

Recommended Posts

I'm having troubles with really understanding how to evoke collision detection; how to and where to put it. I've looked at the examples and the API and I think my non existent background in computer science/logic are hindering me right now. Is it possible to get (or to be pointed towards) some really easy to follow examples of how to go about collision detection, from as primitive of a start as possible e.g.  this.load.image('ball', images/ball.png). 

 

thanks for any help or suggestions! 

Link to comment
Share on other sites

This is what I've got. I tried going over the Phaser examples :\ 

 

var game = new Phaser.Game(384, 640, Phaser.AUTO, 'gameDiv');

 
 
function preload() {
    
    game.load.image('ground', 'images/PNG/red_button00.png');
    game.load.image('ball', 'images/PNG/green_circle.png');
    
},
    
    var sprite;
    var sprite2;
    
function create() {
 
game.physics.startSystem(Phaser.Physics.ARCADE);
 
game.stage.backgroundColor = '#124184';
    
sprite = game.add.sprite(300, 500, 'ground');
sprite.name = 'ground';
game.physics.enable(sprite, Phaser.Physics.ARCADE);
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.up = true;
sprite.body.checkCollision.down = true;
sprite.body.immovable = true;
 
sprite2 = game.add.sprite(350, 400, 'ball', 2);
sprite2.name = 'ball';
game.physics.enable(sprite2, Phaser.Physics.ARCADE);
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(1, 1);
    sprite2.body.velocity.y = -200;
},
function update() {
 
game.physics.arcade.collide(sprite, sprite2);
    game.physics.arcade.enable(this.ball);
 
 
},
 
 
Am I even close? lol
Link to comment
Share on other sites

Even when I blindly copy and paste it doesn't seem to work. I'm using brackets and it's live preview option. Could that Just be the issue? :\

 

thanks again everyone. I know it might be something small, but I'm just learning this  :mellow:

 

Code if anyone cares:

 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>practice</title>
  <script type="text/javascript" src="lib/phaser.js"></script>
  <script type="text/javascript" src="main2.js"></script>
</head>
<body>
  <div id="gameDiv"></div>
    </body>

 

</html>

______________________________________________________________________________________________

 

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'gameDiv');

 
 
function preload() {
    
    game.load.image('ground', 'images/PNG/red_button00.png');
    game.load.image('ball', 'images/PNG/green_circle.png');
    
},
    
    var sprite;
    var sprite2;
    
function create() {
 
    game.physics.startSystem(Phaser.Physics.ARCADE);
 
game.stage.backgroundColor = '#124184';
    
    sprite = game.add.sprite(50, 200, 'ground');
    sprite2 = game.add.sprite(700, 220, 'ball');
 
    game.physics.enable( [ sprite, sprite2 ], Phaser.Physics.ARCADE);
 
    sprite.name = 'ground';
    sprite.body.velocity.x = 100;
    
    sprite2.name = 'ball';
    sprite2.body.velocity.x = -100;
 
}
 
function update() {
 
    // object1, object2, collideCallback, processCallback, callbackContext
    game.physics.arcade.collide(sprite, sprite2, collisionHandler, null, this);
 
}
 
function collisionHandler (obj1, obj2) {
 
    //  The two sprites are colliding
    game.stage.backgroundColor = '#992d2d';
 
}
 
function render() {
 
    game.debug.body(sprite);
    game.debug.body(sprite2);
 
}
Link to comment
Share on other sites

After going through other peoples code, i've figured it out! Thank you to the two individuals who commented, and to the other 159 (at this point) individuals who just looked at this and said "I'll let the next guy answer this one #n00b". 

 

:P 

 

 

 

joke directed towards myself**

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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