|
| 1 | +var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { |
| 2 | + preload: function() { |
| 3 | + game.load.image ( 'sky' , 'assets/sky.png' ) ; |
| 4 | + game.load.image ( 'ground' , 'assets/platform.png' ) ; |
| 5 | + game.load.image ( 'star' , 'assets/star.png' ) ; |
| 6 | + game.load.spritesheet ( 'dude' , 'assets/dude.png' , 32 , 48 ) ; |
| 7 | + } |
| 8 | + , create: create |
| 9 | + , update: update |
| 10 | +}); |
| 11 | + |
| 12 | +var platforms; |
| 13 | +var player; |
| 14 | +var playerDimensions = {width: 48, height: 32}; |
| 15 | +var keys; |
| 16 | +var configs = { jumpVelocity : 500 , horizontalMovement : 150 } |
| 17 | +var emitters = {}; |
| 18 | +var collisionHandlers = { |
| 19 | + playerVworld: function(player, ground) { |
| 20 | + |
| 21 | + if ( player.body.velocity.y > -2 && player.body.velocity.y < +2 ) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + emitters.star.x = player.x + playerDimensions.width / 2; |
| 26 | + emitters.star.y = player.y + playerDimensions.height; |
| 27 | + emitters.star.removeAll(false, true); |
| 28 | + emitters.star.start(true, 2000, null, 10); |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +function create() { |
| 33 | + keys = game.input.keyboard.createCursorKeys(); |
| 34 | + |
| 35 | + game.add.sprite(0, 0, 'sky'); |
| 36 | + game.add.sprite(0, 0, 'star'); |
| 37 | + player = game.add.sprite(0, 0, 'dude'); |
| 38 | + player.animations.add( 'left', [0, 1, 2, 3], 10, true); |
| 39 | + player.animations.add( 'right', [5, 6, 7, 8], 10, true); |
| 40 | + |
| 41 | + |
| 42 | + game.physics.startSystem(Phaser.Physics.ARCADE); |
| 43 | + game.physics.arcade.enable(player); |
| 44 | + player.body.bounce.y = 0.2; |
| 45 | + player.body.gravity.y = 350; |
| 46 | + player.body.collideWorldBounds = true; |
| 47 | + |
| 48 | + platforms = game.add.group(); |
| 49 | + platforms.enableBody = true; |
| 50 | + |
| 51 | + var ground = platforms.create(0, game.world.height - 64, 'ground'); |
| 52 | + ground.scale.setTo(2,2); |
| 53 | + ground.body.immovable = true; |
| 54 | + |
| 55 | + var ledge = platforms.create(400, 400, 'ground'); |
| 56 | + ledge.body.immovable = true; |
| 57 | + |
| 58 | + emitters.star = game.add.emitter(0, 0, 5); |
| 59 | + emitters.star.makeParticles('star'); |
| 60 | + emitters.gravity = 200; |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +function update() { |
| 65 | + game.physics.arcade.collide(player, platforms, collisionHandlers.playerVworld); |
| 66 | + |
| 67 | + updatePlayer(player, keys); |
| 68 | +} |
| 69 | + |
| 70 | +function updatePlayer(player, keys) { |
| 71 | + updatePlayerVertical(player, keys); |
| 72 | + updatePlayerHorizontal(player, keys); |
| 73 | +} |
| 74 | + |
| 75 | +function updatePlayerVertical(player, keys) { |
| 76 | + if ( player.body.touching.down && keys.up.isDown) { |
| 77 | + player.body.velocity.y = - configs.jumpVelocity; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function updatePlayerHorizontal(player, keys) { |
| 82 | + if (keys.left.isDown) { |
| 83 | + player.body.velocity.x = - configs.horizontalMovement; |
| 84 | + player.animations.play('left'); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (keys.right.isDown) { |
| 89 | + player.body.velocity.x = + configs.horizontalMovement; |
| 90 | + player.animations.play('right'); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // == else == { |
| 95 | + player.body.velocity.x = 0; |
| 96 | + player.animations.stop(); |
| 97 | + player.frame = 4; |
| 98 | + return; |
| 99 | + // } |
| 100 | +} |
| 101 | + |
0 commit comments