Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

error with colision between sprites/layers (arcade physics)


kingfisher
 Share

Recommended Posts

Hey, I'm new to phaser and want to create a small rpg(ish) game.

I use the example Csv Map Collide as a base, because I like the idea of tilemaps.

So when I copied the code into my project, I get the console shows "TypeError: c.layer.callbacks is undefined" and the canvas is black. As soon as I remove the line which defines the collision between the player and the layer it works fine, but without any collisions.

Here's my code(I added another layer):

var game = new Phaser.Game(jQuery(window).width(), jQuery(window).height(), Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

    game.load.tilemap('map', 'assets/tilemaps/csv/catastrophi_level2.csv', null, Phaser.Tilemap.CSV);
    game.load.tilemap('map2', 'assets/tilemaps/csv/catastrophi_level3.csv', null, Phaser.Tilemap.CSV);
    game.load.image('tiles', 'assets/tilemaps/tiles/catastrophi_tiles_16.png');
    game.load.spritesheet('player', 'assets/sprites/spaceman.png', 16, 16);

}

var map;
var layer = [];
var player;
var cursors;

function create() {

    //  Because we're loading CSV map data we have to specify the tile size here or we can't render it
    map = game.add.tilemap('map', 16, 16);
    //  Now add in the tileset
    map.addTilesetImage('tiles');
    //  Create our layer
    layer[0] = map.createLayer(0);
//    layer = Phaser.TilemapLayer(game, 'tiles', 0, game.width, game.height);

    map2 = game.add.tilemap('map2', 16, 16);
    //  Now add in the tileset
    map2.addTilesetImage('tiles');
    //  Create our layer
    layer[1] = map2.createLayer(0);
    
    
    //  Resize the world
    layer[0].resizeWorld();
    
    map.setCollisionBetween(54, 83);
    layer[0].debug = true;
    
    //  Player
    player = game.add.sprite(48, 48, 'player', 1);
    player.animations.add('left', [8,9], 10, true);
    player.animations.add('right', [1,2], 10, true);
    player.animations.add('up', [11,12,13], 10, true);
    player.animations.add('down', [4,5,6], 10, true);
    
    //physics
    game.physics.enable(player, Phaser.Physics.ARCADE);
    player.body.setSize(10, 14, 2, 1);

    //camera
    game.camera.follow(player);

    //  Allow cursors to scroll around the map
    cursors = game.input.keyboard.createCursorKeys();

    var help = game.add.text(16, 16, 'Arrows to scroll', { font: '14px Arial', fill: '#ffffff' });
    help.fixedToCamera = true;

}

function update() {    
    game.physics.arcade.collide(player, layer[0]);
    game.physics.arcade.collide(player, layer[1], collisionCallback, processCallback, this);

    player.body.velocity.set(0);

    if (cursors.left.isDown)
    {
        player.body.velocity.x = -100;
        player.play('left');
    }
    else if (cursors.right.isDown)
    {
        player.body.velocity.x = 100;
        player.play('right');
    }
    else if (cursors.up.isDown)
    {
        player.body.velocity.y = -100;
        player.play('up');
    }
    else if (cursors.down.isDown)
    {
        player.body.velocity.y = 100;
        player.play('down');
    }
    else
    {
        player.animations.stop();
    }

}

function collisionCallback (obj1, obj2) {

    console.log("callback");
}

function processCallback(obj1, obj2) {

    console.log("process");
}

function render() {

}

I hope someone can help me with this issue, since I can't continue with my project :)

 

EDIT: found the error, I was using an outdated Phaser.js file :D

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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