shinyclaw Posted October 14, 2014 Share Posted October 14, 2014 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 More sharing options...
Sebi Posted October 14, 2014 Share Posted October 14, 2014 Because you are assigning the inputDown handler only to the last fieldSprite and the fieldSprite parameter is not defined in the rotate method. Link to comment Share on other sites More sharing options...
shinyclaw Posted October 15, 2014 Author Share Posted October 15, 2014 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 More sharing options...
j0hnskot Posted October 16, 2014 Share Posted October 16, 2014 I guess the "new field() " you are calling is a sprite constructor, so the only problem i see is that you call this.fieldSprites[i].angle on a group which is not an array. Try "this.fieldSprites.children.angle". Link to comment Share on other sites More sharing options...
Recommended Posts