Sign in to follow this  
rickydamta

Sprite TypeError: Cannot read property 'index' of undefined

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;				}    }}

 

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.