Jump to content

Undefined property in the callback


shinyclaw
 Share

Recommended Posts

Hi all, I have declared several game states in my project:

game.state.add('game1', game1State);game.state.add('game2', game2State);game.state.add('game3', game3State);game.state.add('game4', game4State);
Then, in the one of the states I have following declarations:

var game3State = {        create: function() {        this.fields = new Array();                this.fieldSprites = game.add.group();        this.randomizeGrid();     },
Then, in the randomizeGrid() I add some fields and sprites and assign onInputDown callback:

    randomizeGrid: function() {        function field(x, y, id, angle) {            this.x = x;            this.y = y;            this.id = id;            this.angle = angle;        }        this.fields.push(new field(someX1, someY1, someId1, someAngle1));        this.fields.push(new field(someX2, someY2, someId2, someAngle2));           this.fields.push(new field(someX3, someY3, someId3, someAngle3));           for (var i = 0; i < this.fields.length; i++) {            var fieldSprite;                        fieldSprite = this.fieldSprites.create(this.fields[i].x, this.fields[i].y, 'some_name');             fieldSprite.inputEnabled = true;            fieldSprite.events.onInputDown.add(this.rotate, this);          }    },
But in the callback function my fields and fieldSprites are undefined

    rotate: function(fieldSprite) {            fieldSprite.angle += 90;                  var completed = true;                for (var i = 0; i < this.fields.length; i++) {            if (this.fields[i].angle != this.fieldSprites[i].angle) { // Cannot read property 'angle' of undefined                 completed = false;            }        }           if (completed) {                    game.debug.text('COMPLETED!', 10, 10);        }      },}; 
I know, that must be silly, but I cannot find solution for this - please help!
Link to comment
Share on other sites

Thanks Sebastian - I'm sorry, but I put the closing bracket too early, when shortening my code ;) Below is the corrected version of randomizeGrid function (also updated in previous post).

 

The problem remains the same...

    randomizeGrid: function() {        function field(x, y, id, angle) {            this.x = x;            this.y = y;            this.id = id;            this.angle = angle;        }        this.fields.push(new field(someX1, someY1, someId1, someAngle1));        this.fields.push(new field(someX2, someY2, someId2, someAngle2));          this.fields.push(new field(someX3, someY3, someId3, someAngle3));          for (var i = 0; i < this.fields.length; i++) {            var fieldSprite;                        fieldSprite = this.fieldSprites.create(this.fields[i].x, this.fields[i].y, 'some_name');            fieldSprite.inputEnabled = true;            fieldSprite.events.onInputDown.add(this.rotate, this);         }    },
Link to comment
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...
 Share

  • Recently Browsing   0 members

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