Jump to content

Uncaught TypeError: Cannot read property 'x' of undefined


JCB_
 Share

Recommended Posts

The previous time I worked on this project it would load no problem within the browser, now I get the error Uncaught TypeError: Cannot read property 'x' of undefined and it says it's coming from phaser.min.js:24 which isn't making sense to me.

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

function preload() {
    this.game.load.tilemap('tilemap', 'assets/tilemaps/csv/tiles.csv', null, Phaser.Tilemap.CSV);
    this.game.load.spritesheet('tiles', 'assets/tilemaps/tiles/snow_tiles_32.png', 32,32);
    this.game.load.spritesheet('player', 'assets/penguin.png', 32,48);
}

var cursors;
//var snowballs;
//var sTime = 0;
//var fireButton;

function create() {
    this.game.physics.startSystem(Phaser.Physics.ARCADE);
    this.game.plugins.add(Phaser.Plugin.ArcadeSlopes);
    cursors = game.input.keyboard.createCursorKeys();

    this.map = this.game.add.tilemap('tilemap');
    this.map.addTilesetImage('snow_tiles_32', 'tiles');

    this.game.stage.backgroundColor = '#80e3ff';

    this.ground = this.map.createLayer(0);
    this.ground.resizeWorld();

    this.game.slopes.convertTilemapLayer(this.ground,{
        1:  'FULL',
        2:  'FULL',
        3:  'FULL',
        4:  'FULL',
        5:  'FULL',
        6:  'FULL',
        7:  'FULL',
        8:  'FULL',
        9:  'HALF_TOP',
        10: 'FULL',
        12: 'FULL',
        13: 'HALF_BOTTOM_LEFT',
        14: 'HALF_BOTTOM_RIGHT',
        15: 'HALF_BOTTOM_LEFT',

    });

    this.map.setCollisionBetween(1, 34, true, 0);

    //player
    this.player = this.game.add.sprite(100, 50, 'player');
    this.game.physics.arcade.enable(this.player);
    this.player.body.slopes = {sat: {response: 0}}; // workaround for a phaser bug
    this.game.slopes.enable(this.player);
    this.player.body.slopes.preferY = true; // stops the player sliding down slopes

    this.player.body.bounce.y = 0.2;
    this.player.body.gravity.y = 700;
    this.player.body.collideWorldBounds = true;

    this.player.animations.add('left', [0,1,2,3], 10, true);
    this.player.animations.add('right', [5,6,7,8], 10, true);

    //this.snowballs = game.add.group();
    //this.snowballs.enableBody = true;
    //this.snowballs.physicsBodyType = Phaser.Physics.ARCADE;
    //this.snowballs.createMultiple(1, 'snowball');
    //this.snowballs.setAll('anchor.x', 0.5);
    //this.snowballs.setAll('anchor.y', 1);
    //this.snowballs.setAll('outOfBoundsKill', true);
    //this.snowballs.setAll('checkWorldBounds', true);

    //this.fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

    this.game.slopes.enable(this.player);
    this.game.camera.follow(this.player);
}

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

    this.player.body.velocity.x = 0;

    if(cursors.left.isDown){
        this.player.body.velocity.x = -150;
        this.player.animations.play('left');
    }
    else if (cursors.right.isDown){
        this.player.body.velocity.x = 150;
        this.player.animations.play('right');
    }
    else{
        this.player.animations.stop();
        this.player.frame = 4;
    }

    if(cursors.up.isDown && (this.player.body.onFloor() || this.player.body.touching.down)){
        this.player.body.velocity.y = -350;
    }

    //if(fireButton.isDown){
    //this.fireSnowball();
    //}
//}

//function fireSnowball(){
    //if(game.time.now > sTime){
    //this.snowball = snowballs.getFirstExists(false);
    //if(snowball){
    // this.snowball.reset(player.x+30, player.y+30);
    //this.snowball.body.velocity.x = 400;
    //this.sTime = game.time.now = 10;
    // }
    //}
}

 

Test Game Phaser2.rar

Link to comment
Share on other sites

I don't see anything obviously wrong at first glance, but here are two tips:

- Try to inspect all the objects of which you try to access the property 'x'; apparently one of these objects is undefined.
- For development purposes, it's usually better to use the non-minified version of a library, so that the error messages point to meaningful lines of code (as you say, with a minified file, line 24 doesn't mean much).

Hopefully this will help you track down the issue.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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