Jump to content

Cannot read property 'x' of undefined using sprite.animations.play


Archer3CL
 Share

Recommended Posts

Im following this tutorial but im changing some of the sprites.

 

So, when the game is running, sporadically this error appears 

%c %c %c Phaser v2.0.7 | Pixi.js v1.6.1 | WebGL | WebAudio  %c %c  http://phaser.io  %c %c ♥%c♥%c♥  (03:01:25:504)  at public_html/js/phaser.js:22011Uncaught TypeError: Cannot read property 'x' of undefined (03:02:20:821 | error, javascript)    at Phaser.Sprite.setFrame (public_html/js/phaser.js:31278:33)    at Phaser.Animation.play (public_html/js/phaser.js:44089:22)    at Phaser.AnimationManager.play (public_html/js/phaser.js:43593:45)    at Neon.item.spawnEnemy (public_html/js/Game.js:61:26)    at Neon.Game.update (public_html/js/Game.js:43:23)    at Phaser.StateManager.update (public_html/js/phaser.js:16546:35)    at Phaser.Game.update (public_html/js/phaser.js:22122:24)    at Phaser.RequestAnimationFrame.updateRAF (public_html/js/phaser.js:38277:19)    at _onLoop (public_html/js/phaser.js:38263:30)> 

 

This is update

update: function() {        this._spawnRate += this.time.elapsed;        if (this._spawnRate > 1000) {            this._spawnRate = 0;            Neon.item.spawnEnemy(this);         }        this._enemyGroup.forEach(function(enemy) {            enemy.angle += enemy.rotateMe;        });        if (!Neon._lives) {            this.add.sprite((Neon.GAME_WIDTH - 594) / 2, (Neon.GAME_HEIGHT - 271) / 2, 'game-over');            this.game.paused = true;        }    }

This is spawnEnemy

spawnEnemy: function(game) {        var dropPos = Math.floor(Math.random() * Neon.GAME_WIDTH);        var dropOffset = [-27, -36, -38, -48];        var enemyType = Math.floor(Math.random() * 5);        var enemy = game.add.sprite(dropPos, dropOffset[enemyType], 'enemy');        enemy.animations.add('anim', [enemyType], 10, true);        enemy.animations.play('anim', 10, true);        game.physics.enable(enemy, Phaser.Physics.ARCADE);        enemy.checkWorldBounds = true;        enemy.events.onOutOfBounds.add(this.removeEnemy, this);        enemy.anchor.setTo(0.5, 0.5);        enemy.rotateMe = (Math.random() * 4) - 2;        game._enemyGroup.add(enemy);    },

And on my Preloader.js i declare my spritesheet like this

this.load.spritesheet('enemy', 'img/enemy.png', 50, 50);

My spritesheet is 200 x 50 and has 4 different sprites

 

The only thing i tried is to comment the error line on spawnEnemy but if i do that in the game only shows the first sprite.

 

Help is appreciated

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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