Advertisement
Guest User

Sprite Flip

a guest
Jun 14th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Phaser - Making your first game, part 7</title>
  6. <script type="text/javascript" src="js/phaser.min.js"></script>
  7. <style type="text/css">
  8. body {
  9. margin: 0;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14.  
  15. <script type="text/javascript">
  16.  
  17. var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render:render });
  18.  
  19. function preload() {
  20.  
  21. game.load.image('sky', 'assets/bg1.jpg');
  22. game.load.image('ground', 'assets/platform.png');
  23. game.load.image('star', 'assets/star.png');
  24. game.load.spritesheet('dude', 'assets/player.png', 96, 130);
  25. game.load.spritesheet('hamster', 'assets/hitler.png', 64, 64);
  26.  
  27. }
  28.  
  29. var player;
  30. var platforms;
  31. var cursors;
  32.  
  33. function create() {
  34.  
  35. // We're going to be using physics, so enable the Arcade Physics system
  36. game.physics.startSystem(Phaser.Physics.ARCADE);
  37.  
  38. // A simple background for our game
  39. game.add.sprite(0, 0, 'sky');
  40.  
  41. // The platforms group contains the ground and the 2 ledges we can jump on
  42. platforms = game.add.group();
  43.  
  44. // We will enable physics for any object that is created in this group
  45. platforms.enableBody = true;
  46.  
  47. // Here we create the ground.
  48. var ground = platforms.create(0, game.world.height - 64, 'ground');
  49.  
  50. // Scale it to fit the width of the game (the original sprite is 400x32 in size)
  51. ground.scale.setTo(2, 2);
  52.  
  53. // This stops it from falling away when you jump on it
  54. ground.body.immovable = true;
  55.  
  56. // Now let's create two ledges
  57. var ledge = platforms.create(400, 400, 'ground');
  58. ledge.body.immovable = true;
  59.  
  60. ledge = platforms.create(-150, 250, 'ground');
  61. ledge.body.immovable = true;
  62.  
  63. // The player and its settings
  64. player = game.add.sprite(32, game.world.height - 250, 'dude');
  65.  
  66. // We need to enable physics on the player
  67. game.physics.arcade.enable(player);
  68.  
  69. // Player physics properties. Give the little guy a slight bounce.
  70. player.body.bounce.y = 0.2;
  71. player.body.gravity.y = 300;
  72. player.body.setSize(60,100, 15, 18);
  73. player.anchor.setTo(0.5, 0.5);
  74. player.body.collideWorldBounds = true;
  75.  
  76.  
  77. // Our two animations, walking left and right.
  78. player.animations.add('left', [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], 10, true);
  79. player.animations.add('right', [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], 10, true);
  80. player.animations.add('attack', [ 13, 14, 15, 16, 17, 18, 19, 20, 21], 10, true);
  81.  
  82. // Our controls.
  83. cursors = game.input.keyboard.addKeys({ 'space': Phaser.Keyboard.SPACEBAR, 'up': Phaser.Keyboard.UP, 'down': Phaser.Keyboard.DOWN, 'left': Phaser.Keyboard.LEFT, 'right': Phaser.Keyboard.RIGHT }); //game.input.keyboard.createCursorKeys();
  84. //actionKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  85.  
  86. // The hamster and its settings
  87. hamster = game.add.sprite(600, game.world.height - 650, 'hamster');
  88. hamster.frame = 24;
  89.  
  90. // We need to enable physics on the player
  91. game.physics.arcade.enable(hamster);
  92.  
  93. // Player physics properties. Give the little guy a slight bounce.
  94. hamster.body.bounce.y = 0.2;
  95. hamster.body.gravity.y = 300;
  96. hamster.body.collideWorldBounds = true;
  97. hamster.body.drag.setTo(10);
  98.  
  99. // Our two animations, walking left and right.
  100. hamster.animations.add('walk', [ 24, 25, 26, 27 ], 10, true);
  101. hamster.animations.add('death', [ 32, 33, 34, 35, 36, 37, 38, 39], 10, false, true);
  102.  
  103. }
  104.  
  105. function update() {
  106.  
  107. // Collide the player and the stars with the platforms
  108. game.physics.arcade.collide(player, platforms);
  109. game.physics.arcade.collide(hamster, platforms);
  110. game.physics.arcade.overlap(player, hamster, killHamster);
  111.  
  112. // Reset the players velocity (movement)
  113. player.body.velocity.x = 0;
  114.  
  115. if (cursors.space.downDuration(1000)) {
  116. player.animations.play('attack');
  117. }
  118. else if (cursors.left.isDown)
  119. {
  120. // Move to the left
  121. player.body.velocity.x = -150;
  122. //player.anchor.setTo(0.5, 0.5);
  123. player.scale.x = -1;
  124. player.animations.play('left');
  125. }
  126. else if (cursors.right.isDown)
  127. {
  128. // Move to the right
  129. player.body.velocity.x = 150;
  130. //player.anchor.setTo(0.5, 0.5);
  131. player.scale.x = 1;
  132. player.animations.play('right');
  133. }
  134. else
  135. {
  136. // Stand still
  137. player.animations.stop();
  138. player.frame = 11;
  139. }
  140.  
  141. // Allow the player to jump if they are touching the ground.
  142. if (cursors.up.isDown && player.body.touching.down)
  143. {
  144. player.body.velocity.y = -350;
  145. }
  146.  
  147. }
  148.  
  149. function render(){
  150.  
  151. this.game.debug.bodyInfo(player, 32, 32);
  152. this.game.debug.body(player);
  153. }
  154.  
  155. function killHamster(player, hamster){
  156. if(cursors.space.downDuration(1000)){
  157. hamster.animations.play('death',10, false);
  158. hamster.body.enable = false;
  159. }
  160. }
  161.  
  162. </script>
  163.  
  164. </body>
  165. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement