BadBadGnom

Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined

Recommended Posts

I'm having that error too, I have no idea what is going on. This happends when I restart the game. I have a restart function in which, after the user clicks on a "restart" button, I reset all the variables (inside an object) and start the game with this.game.state.start("Game");

 

The full error is: 

Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined c.Physics.Arcade.enableBody @ phaser.js:62117 c.Physics.Arcade.enable @ phaser.js:62097 TaTeTi.Game.dragGame @ Game.js:311 TaTeTi.Game.controlPlace @ Game.js:294 TaTeTi.Game.onDown @ Game.js:231 c.SignalBinding.execute @ phaser.js:19057 c.Signal.dispatch @ phaser.js:18880 c.Events.hasOwnProperty.e.indexOf.c.Events.(anonymous function).c.Events.(anonymous function) @ phaser.js:35189 c.InputHandler._touchedHandler @ phaser.js:30876 c.Pointer.start @ phaser.js:29001 c.Mouse.onMouseDown @ phaser.js:28020 c.Mouse.start._onMouseDown @ phaser.js:27936

The dragGame function:

dragGame: function(){  for(var i = 0; i < Piece.length; i++){    if(Piece[i]._IS_PLAYER){      this.physics.arcade.enable(this.playerPieces.children[i]);      this.playerPieces.children[i].name = Piece[i]._NAME;      this.playerPieces.children[i].place = Piece[i]._CURRENT_POS;      this.playerPieces.children[i].inputEnabled = true;      this.playerPieces.children[i].events.onInputDown.add(this.onDownForDrag,this);    }else if(this.playerPieces.children[i].key == 'square'){      this.playerPieces.children[i].inputEnabled = false;    }  }},

controlPlace function:

controlPlace: function(square){   if(TaTeTi._ALLPIECESINPLACE){    this.dragGame(square);  }else{    var name = square.name;    var posX = square.x;    var posY = square.y;    if(!this._COORDINATES[name]._TAKEN){      this.drawPiece(posX,posY,name);      this._COORDINATES[name]._TAKEN = true;    }else{      console.log('OCUPIED!!');    }  }},

and onDown:

onDown: function(theSquare,pointer){  this.controlPlace(theSquare);  this.playerPieces.add(theSquare);},

Hope you can help me, thanks guys!!  :)

 

Edit: btw! phaser version is 2.3.0

Share this post


Link to post
Share on other sites

The hasOwnProperty check is the first place where the object you're passing in is dereferenced. This means that you're probably passing a null reference.

 

When you make a call to state.start, the second parameter is whether you want to clear the World display list, which defaults to true. Any objects that are children of game.world (which, in most cases, is all of them) are cleared, so if you need display objects like sprites to persist between states, you need to call start.start("Game", false) to make sure pre-existing things in the game.world aren't erased.

 

Of course, switching states can be a very complicated procedure, so there may be other issues for BadBadGnom, but in diegoalmesp's case I'd start with that and see if the issue is resolved.

Share this post


Link to post
Share on other sites

thanks substandardgaussian ! I tried that but I get the same error. Maybe is the way I'm assigning physics to the sprites?, this is the code in the create function:

create: function() {  this.handleResize(); // some custom function  this.physics.startSystem(Phaser.Physics.ARCADE);  this.preloadBG = this.add.sprite(0,0,'generalBG');  this.preloadBG.width = TaTeTi._WIDTH;  this.preloadBG.height = TaTeTi._HEIGHT;  this.board = this.add.sprite(TaTeTi._WIDTH/2+TaTeTi._RATIO*30,TaTeTi._HEIGHT/2,'board');  this.board.anchor.set(0.5,0.5);  this.board.scale.setTo(TaTeTi._RATIO*0.35,TaTeTi._RATIO*0.35);  this.board.name = 'THE BOARD';  this.HitZoneGroup = this.add.group();  this.playerPieces = this.add.group();  this.initPlaces();  for(var i = 0; i < this._COORDINATES.length; i++){    this.square = this.add.sprite(0,0,'square');    this.square.anchor.set(0.5,0.5);    this.square.alpha = 0;    this.square.x = this._COORDINATES[i]._x1;    this.square.y = this._COORDINATES[i]._y1;    this.square.name = this._COORDINATES[i]._ID;    this.square.inputEnabled = true;    this.square.scale.setTo(TaTeTi._RATIO*0.4,TaTeTi._RATIO*0.4);    this.square.events.onInputDown.add(this.onDown,this);    this.HitZoneGroup.add(this.square);  }  this.physics.enable(this.HitZoneGroup, Phaser.Physics.ARCADE);  this.physics.enable(this.playerPieces, Phaser.Physics.ARCADE);},

because the line in question (on phaser.js) is:

enableBody: function (object) {  if (object.hasOwnProperty('body') && object.body === null) <--- this one  {    object.body = new Phaser.Physics.Arcade.Body(object);  }},

Many thanks for your help!!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.