Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 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 9</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 });
  18.  
  19. function preload() {
  20.  
  21. game.load.image('tilesky', 'assets/skytile.gif');
  22. game.load.image('sky', 'assets/city_3.png');
  23. game.load.image('ground', 'assets/platform.png');
  24. game.load.image('star', 'assets/(raz).gif');
  25. game.load.spritesheet('dude', 'assets/dansprite2.png', 32, 45);
  26. game.load.spritesheet('coin', 'assets/01coin.png', 15, 120);
  27.  
  28.  
  29.  
  30. }
  31.  
  32. var player;
  33. var platforms;
  34. var cursors;
  35.  
  36. var stars;
  37. var score = 0;
  38. var scoreText;
  39.  
  40. var night;
  41. var buildings;
  42.  
  43. var camspeed = 2;
  44.  
  45.  
  46. function create() {
  47.  
  48. // We're going to be using physics, so enable the Arcade Physics system
  49. game.physics.startSystem(Phaser.Physics.ARCADE);
  50.  
  51. game.world.setBounds(0, 0, 6000, 600);
  52.  
  53. // A simple background for our game
  54.  
  55. night = game.add.tileSprite(0, 0, 6000, 600, 'tilesky' );
  56.  
  57. buildings = game.add.tileSprite(0, 0, 6000, 600, 'sky');
  58.  
  59.  
  60. // The platforms group contains the ground and the 2 ledges we can jump on
  61. platforms = game.add.group();
  62.  
  63. // We will enable physics for any object that is created in this group
  64. platforms.enableBody = true;
  65.  
  66. // Here we create the ground.
  67. var ground = platforms.create(0, game.world.height - 64, 'ground');
  68.  
  69. // Scale it to fit the width of the game (the original sprite is 400x32 in size)
  70. ground.scale.setTo(15, 2);
  71.  
  72. // This stops it from falling away when you jump on it
  73. ground.body.immovable = true;
  74.  
  75. // Now let's create two ledges
  76. var ledge = platforms.create(400, 400, 'ground');
  77. ledge.body.immovable = true;
  78.  
  79. ledge = platforms.create(-150, 250, 'ground');
  80. ledge.body.immovable = true;
  81.  
  82. ledge = platforms.create(800, 250, 'ground');
  83. ledge.body.immovable = true;
  84.  
  85. ledge = platforms.create(1200, 100, 'ground');
  86. ledge.body.immovable = true;
  87.  
  88. ledge = platforms.create(1050, 400, 'ground');
  89. ledge.body.immovable = true;
  90.  
  91. ledge = platforms.create(1550, 325, 'ground');
  92. ledge.body.immovable = true;
  93.  
  94. // The player and its settings
  95. player = game.add.sprite(32, game.world.height - 150, 'dude');
  96.  
  97. // We need to enable physics on the player
  98. game.physics.arcade.enable(player);
  99.  
  100. // Player physics properties. Give the little guy a slight bounce.
  101. player.body.bounce.y = 0.;
  102. player.body.gravity.y = 350;
  103. player.body.collideWorldBounds = true;
  104.  
  105. // Our two animations, walking left and right.
  106. player.animations.add('left', [0, 1, 2, 3], 10, true);
  107. player.animations.add('right', [5, 6, 7, 8], 10, true);
  108.  
  109. // Finally some stars to collect
  110. stars = game.add.group();
  111.  
  112. // We will enable physics for any star that is created in this group
  113. stars.enableBody = true;
  114.  
  115. // Here we'll create 12 of them evenly spaced apart
  116. for (var i = 0; i < 25; i++)
  117. {
  118. // Create a star inside of the 'stars' group
  119. var star = stars.create(Math.random() * 4500, 0, 'star');
  120.  
  121. // Let gravity do its thing
  122. star.body.gravity.y = 300;
  123.  
  124. // This just gives each star a slightly random bounce value
  125. star.body.bounce.y = 0.7 + Math.random() * 0.2;
  126. }
  127.  
  128. // The score
  129. scoreText = game.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#fff' });
  130.  
  131. // Our controls.
  132. cursors = game.input.keyboard.createCursorKeys();
  133.  
  134. scoreText.fixedToCamera = true;
  135.  
  136. }
  137.  
  138. function update() {
  139.  
  140. // Collide the player and the stars with the platforms
  141. game.physics.arcade.collide(player, platforms);
  142. game.physics.arcade.collide(stars, platforms);
  143.  
  144. // Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
  145. game.physics.arcade.overlap(player, stars, collectStar, null, this);
  146.  
  147. // Reset the players velocity (movement)
  148. player.body.velocity.x = 0;
  149.  
  150. if (cursors.left.isDown)
  151. {
  152. // Move to the left
  153. player.body.velocity.x = -150;
  154.  
  155. player.animations.play('left');
  156.  
  157. // night.tilePosition.x += 1;
  158. // buildings.tilePosition.x += 2;
  159.  
  160. /* game.camera.x -= camSpeed;*/
  161. if (!game.camera.atLimit.x){
  162. night.tilePosition.x += 1;
  163. buildings.tilePosition.x += 2;
  164. }
  165. }
  166. else if (cursors.right.isDown)
  167. {
  168. // Move to the right
  169. player.body.velocity.x = 150;
  170.  
  171. player.animations.play('right');
  172.  
  173. night.tilePosition.x -= 1;
  174. buildings.tilePosition.x -= 2;
  175.  
  176. /* game.camera.x += camSpeed;*/
  177. if (!game.camera.atLimit.x){
  178. night.tilePosition.x -= 1;
  179. buildings.tilePosition.x -= 2;
  180. }
  181. }
  182.  
  183. else
  184. {
  185. // Stand still
  186. player.animations.stop();
  187.  
  188. player.frame = 4;
  189. }
  190.  
  191.  
  192. // Allow the player to jump if they are touching the ground.
  193. if (cursors.up.isDown && player.body.touching.down)
  194. {
  195. player.body.velocity.y = -350;
  196. }
  197.  
  198. game.camera.focusOnXY(player.x, player.y);
  199.  
  200. }
  201.  
  202. function collectStar (player, star) {
  203.  
  204. // Removes the star from the screen
  205. star.kill();
  206.  
  207. // Add and update the score
  208. score += 1;
  209. scoreText.text = 'Score: ' + score;
  210.  
  211.  
  212. }
  213.  
  214. </script>
  215.  
  216. </body>
  217. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement