silverplanetside Posted January 2, 2016 Share Posted January 2, 2016 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 More sharing options...
tsphillips Posted January 4, 2016 Share Posted January 4, 2016 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 silverplanetside 1 Link to comment Share on other sites More sharing options...
Recommended Posts