Jump to content

Phaser - Uncaught TypeError: Cannot set property 'y' of undefined


Noaml1
 Share

Recommended Posts

i'm trying to follow the starstruck example in remake it in typescript and im getting an error i dont understand.

This is what i have so far :

create() {            this.game.physics.startSystem(Phaser.Physics.ARCADE);            this.game.stage.backgroundColor = '#000000';            this.bg = this.game.add.tileSprite(0, 0, 800, 600, 'background');            this.bg.fixedToCamera = true;            this.map = this.game.add.tilemap('level1');            this.map.addTilesetImage('tiles-1');            this.map.setCollisionByExclusion([13, 14, 15, 16, 46, 47, 48, 49, 50, 51]);                       this.layer = this.map.createLayer('Tile Layer 1');            this.layer.resizeWorld();            this.game.physics.arcade.gravity.y = 250;            this.player = this.game.add.sprite(32, 32, 'dude');            this.game.physics.enable(this.player, Phaser.Physics.ARCADE);            this.player.body.bounc.y = 0.2;            this.player.body.collideWorldBounds = true;            this.player.body.setSize(20, 32, 5, 16);            this.player.animations.add('left', [0, 1, 2, 3], 10, true);            this.player.animations.add('turn', [4], 20, true);            this.player.animations.add('right', [5, 6, 7, 8], 10, true);            this.game.camera.follow(this.player);                        this.cursors = this.game.input.keyboard.createCursorKeys();            this.jumpbutton = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);        }        update() {            this.game.physics.arcade.collide(this.player, this.layer);            this.player.body.velocity.x = 0;            if (this.cursors.right.isDown) {                this.player.body.velocity.x = 150;                this.player.animations.play('right');            }        }

everythings been loaded correctly in the preload method

and im getting the error:

Phaser v2.0.5 - Canvas - WebAudio http://phaser.io phaser.js:19908
Uncaught TypeError: Cannot set property 'y' of undefined Main.ts:39
Uncaught TypeError: Cannot read property 'right' of undefined Main.ts:58
 

the first error occurs in this area:
 

  this.game.physics.enable(this.player, Phaser.Physics.ARCADE);            this.player.body.bounc.y = 0.2;

the second here:

this.player.body.velocity.x = 0;            if (this.cursors.right.isDown) {

what's happening?

Link to comment
Share on other sites

The first error is because of a typo; it's body.bounce, not body.bounc. This may be causing the second error to occur due to script halting; though it could also be a scope issue because of 'this'. Try fixing the first error and see what happens.

 

The IE error is a little bit weird; presumably you're loading phaser.js or phaser.min.js before Main.js? If not, Phaser will not be available when your script calls it.

Link to comment
Share on other sites

I think I'd probably need to see it for myself to identify that issue, seems to me like a race condition (Phaser isn't being loaded in time for when it's used). You could try loading phaser.js in the <head> section of your page, then loading Main.js just before </body> maybe.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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