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

Uncaught TypeError: Cannot read property 'time' of undefined


Bobcp
 Share

Recommended Posts

Hi, i create script, and have error, don't understand why it happens, can you help me?
 
 
this.lostLife = function (jim, snowball){    snowball.hasOverlap =true;    life=life-1;    waterScore=waterScore-10;    if(waterScore<0){waterScore=0};    jim.body.enable = false;    this.jimAliveTimer = this.game.time.events.add(Phaser.Timer.SECOND, jimAlive, this);  // Error this line code    console.log('LifeLost');}

 

Link to comment
Share on other sites

Before  this.jimAliveTimer.. you can add console.log(this) to see what this is refering to. It's hard to tell where's the error without looking at the rest of the code but what the interpreter is telling you is that you want to access a property called time inside this.game but this.game is undefined. 

 

If you're new to javascript I suggest you read https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch1.md

Link to comment
Share on other sites

Before  this.jimAliveTimer.. you can add console.log(this) to see what this is refering to. It's hard to tell where's the error without looking at the rest of the code but what the interpreter is telling you is that you want to access a property called time inside this.game but this.game is undefined. 

 

If you're new to javascript I suggest you read https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch1.md

Thank you.

 

Yes, it's becouse use this.

 

Sometime this defind like a object game, but another way, this defined like a function.

 

I put console.log(this) before this.jimAliveTimer

function (jim, snowball){            snowball.hasOverlap =true;            life=life-1;            waterScore=waterScore-10;            if(waterScore<0){waterScore=0};            jim.body.en…
this.game.physics.arcade.collide(jim, snowballGroup, this.lostLife);

snowball is a child of the group called snowballGroup

this.snowballCreate = function() {  	this.snowball = snowballGroup.create(1500*ratioScale,this.game.world.height-100*ratioScale-heightGround*ratioScale, 'snowball');	this.snowball.scale.setTo(ratioScale, ratioScale);	this.snowball.body.gravity.y = 2000;   	this.snowball.body.bounce.y = 0.1;        this.snowball.body.mass = 10;	this.snowball.anchor.setTo(0.5, 0.5)		this.snowball.hasScored=false;		  	this.snowball.hasOverlap=false;		}

Why this defined like a function, not like an object?

Link to comment
Share on other sites

this is bound at runtime to the calling function. In you code this will be set to whoever called lostLife(). The one calling lostLife must have a property called game containing a reference to a Phaser.Game if you want this.game.time.... to work as intented. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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