Jump to content

Is this the best approach? (Sprites,Animation)


silverplanetside
 Share

Recommended Posts

    function preload() {game.load.spritesheet('player', 'player.png', 100, 100);game.load.spritesheet('torso', 'torsos.png', 100, 100);    game.load.spritesheet('pants', 'pants.png', 100, 100);    game.load.spritesheet('head', 'heads.png', 100, 100);}function create() {player.animations.add('player_walk_left', [0,1,2,3,4,5,6]);player.animations.add('player_walk_right', [7,8,9,10,11,12]);player.animations.add('torso_walk_left', [0,1,2,3,4,5,6]);player.animations.add('torso_walk_right', [7,8,9,10,11,12]);player.animations.add('pants_walk_left', [0,1,2,3,4,5,6]);player.animations.add('pants_walk_right', [7,8,9,10,11,12]);player.animations.add('head_walk_left', [0,1,2,3,4,5,6]);player.animations.add('head_walk_right', [7,8,9,10,11,12]);    player.addChild(torso);    player.addChild(pants);    player.addChild(head);}function update() {    if (cursors.left.isDown)	    {	        player.body.velocity.x = -300;	        player.animations.play('walk_left', 10, true);	        pants.animations.play('walk_left', 10, true);torso.animations.play('torso_walk_left', 10, true);head.animations.play('head_walk_left', 10, true);	    }	    else if (cursors.right.isDown)	    {	        player.body.velocity.x = 300;	        player.animations.play('walk_right', 10, true);	        pants.animations.play('walk_right', 10, true);torso.animations.play('torso_walk_right', 10, true);head.animations.play('head_walk_right', 10, true);	        	    }[...]}

Seems too repetitive for me. Player gets different sprites to play with depending on what he chooses on starting screen.

Link to comment
Share on other sites

Whenever I run into situations like this, I sometimes create helper functions to ease the pain.  For example, player, head, torso, and pants could be abstracted as a list of arbitrary components.  Element 0 would be the player sprite, with the physics body.  If everything is indexed in arrays, then simple loops could take care of the tedium, and those loops could be put into helper functions.

 

The decision to make the helper functions (or roll helper functions into a custom plugin) is an economic one -- spare time before a project deadline means I can invest in nicer code.  Tight deadlines mean "get it done quickly" regardless of the tedium.

 

Tom

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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