Jump to content

[solved] Arcade Collision not working


jeffersonbenson
 Share

Recommended Posts

I'm about 75% sure that the answer is staring at me right in the face, but I'd really appreciate a second or third pair of eyes looking at this code. 

Let me explain:

About a week ago a friend and I had a brilliant idea to start coding a game, where he would do art and I would program. So I picked up Phaser because it seemed to be among the fan favorites and had excellent documentation. Things were going really smooth until I started moving a character around and have him collide into tiles made in Tiled. For the life of me I can't seem to get him to actually collide with the blocks, and no amount of fudging seems to have made things any smoother. So I'd really appreciate it if someone could take a look and see what I'm forgetting. Thanks!

Here's the code: 

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

function preload() {

    game.load.tilemap('map', 'Assets/Tilemaps/newSnow.json', null, Phaser.Tilemap.TILED_JSON);
    
    game.load.image('tileset', 'Assets/Tilemaps/snowTiles.png');
    
    game.load.image('penguin', 'Assets/penguin.jpg');  


    var map;
    var tileset;
    var penguin;
    var Background;
    var Foreground;
    

    
}
function create() {

   map = game.add.tilemap('map');

   
   map.addTilesetImage('Background', 'tileset');
   //map.addTilesetImage('Foreground', 'tileset');
   
   map.setCollision([29], true);

   Background = map.createLayer('Background');
   Foreground = map.createLayer('Foreground');
   
   map.setCollisionBetween(1, 100, true, 'Foreground');

   
   Background.resizeWorld();

   
    //game.add.sprite(0, 0, 'background');
    game.stage.backgroundColor = '#71c5cf';
    
    player = game.add.sprite(0, 0, 'penguin');
    
    player.anchor.setTo(0.5, 0.5);
    

    
    game.physics.enable(player, Phaser.Physics.ARCADE);
   
    player.body.collideWorldBounds = true;
    
    cursors = game.input.keyboard.createCursorKeys();

    

}

function update() {
    game.physics.arcade.collide(player, Foreground);


    var dirMove = 3;

    if (cursors.left.isDown)
    {
        player.body.x -= dirMove;
        }
    else if (cursors.right.isDown)
    {
        player.body.x += dirMove;

    }
    else if (cursors.up.isDown)
    {
        player.body.y -= dirMove;

    }
    else if (cursors.down.isDown)
    {
       //ycoord += 50;
       player.body.y += dirMove;

    }
    else
    {
        player.animations.stop();
    }

}

Link to comment
Share on other sites

So, hey, it looks like you're using an ancient version of Phaser in this project: 2.0.1. I upgraded to 2.4.9 when I was testing locally.

Additionally, I changed the lines where you said "map.addTilesetImage('Background', 'tileset');" to "map.addTilesetImage('Snow1', 'tileset');". You're telling Phaser what image it knows about (i.e. 'tileset') corresponds to what tileset image in the Tiled map (i.e. 'Snow1'). 

Then, instead of passing the string 'Foreground' to map.setCollisionBetween, I passed the actual layer: Foreground, no quotes.

Here's the modified source: http://pastebin.com/dTDqT4e8

Also: you declared the variables you're using inside the "preload" function, but then used them in "create". In JavaScript, variables are scoped by their enclosing functions so the vars in "preload" are not the same as the ones in "create". Each of those variables you're using (Background, Foreground, etc) are all exposed as global variables. That's not necessarily a bad thing, I just didn't want you to assign a value you need in preload and then wonder why you can't see it in create.

Link to comment
Share on other sites

@drhayes Thank you thank you thank you! I've been racking my brain around this problem for a few days now and its been killing me slowly (which I understand is normal for most programmers). I hadn't noticed that the version was out of date for phaser, since it was pulled directly from the github page. I'll have to look into this further.

As far as the variable scope is concerned, thank you for explaining that better. I was wondering why all the tutorials I was reading negleced the "var" but that makes sense. I may re-scope them later, but for now this is simply a proof of concept that this game could actually be built.

Thanks again!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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