Jump to content

Sprite TypeError: Cannot read property 'index' of undefined


rickydamta
 Share

Recommended Posts

Hello,

I have a sprite 

player = game.add.sprite(50, 550, 'player');

When i try to load some animations

player.animations.add('left', [4, 5, 6, 7], 10, true);player.animations.add('right', [8, 9, 10, 11], 10, true);player.animations.add('up', [12, 13, 14, 15], 10, true);player.animations.add('down', [0, 1, 2, 3], 10, true);

I get this error

TypeError: Cannot read property 'index' of undefined

This is my full code

var menu = function(game){	console.log("%cStarting my awesome game", "color:white; background:red");}menu.prototype = {			preload: function() {				game.load.tilemap('level_json', '/maps/level1.json', null, Phaser.T                                ilemap.TILED_JSON);				game.load.image('tiles', '/res/tiles_level2.png');				game.load.spritesheet('player','/res/player.png', 32, 48);                                },		                create: function() {				   game.physics.startSystem(Phaser.Physics.ARCADE);				    // Load the map.                                    map = game.add.tilemap('level_json');                                    map.addTilesetImage('tiles_level1', 'tiles');				    layer = map.createLayer('Tile Layer 1');				    layer.resizeWorld();				    layer.wrap = true;				    player = game.add.sprite(50, 550, 'player');				    player.animations.add('left', [4, 5, 6, 7], 10, true);				    player.animations.add('right', [8, 9, 10, 11], 10, true);				    player.animations.add('up', [12, 13, 14, 15], 10, true);				    player.animations.add('down', [0, 1, 2, 3], 10, true);    		                    game.camera.follow(player);				    game.physics.enable(player, Phaser.Physics.ARCADE);				    player.body.collideWorldBounds = true;                                    cursors = game.input.keyboard.createCursorKeys();                            },		update: function() {                                if(cursors.left.isDown)	 			{					 player.body.velocity.x = -150;					 player.animations.play('left');				}				else if(cursors.right.isDown)				{					 player.body.velocity.x = 150;					 player.animations.play('right');				}				else if(cursors.up.isDown)				{					player.body.velocity.y = -150;					player.animations.play('up');				}				else if(cursors.down.isDown)				{					player.body.velocity.y = 150;					player.animations.play('down');				}				else				{					player.body.velocity.x = 0;					player.body.velocity.y = 0;					player.frame = 0;				}    }}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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