Advertisement
itsdevkay

PinheadPhil

Mar 19th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     // (width, height, Phaser.<TYPE>, name-for-html, { preload: preload ... });
  3.     var game = new Phaser.Game(800, 600, Phaser.AUTO, 'pinhead phil', { preload: preload, create: create, update: update });
  4.  
  5.     var dude;
  6.     var platforms;
  7.     var cursors;
  8.     var jumpButton;
  9.     var facing = 'left';
  10.  
  11.     function preload () {
  12.         this.load.image('sky', 'sky.png');
  13.         this.load.image('ground', '/assets/platform.png'); // adding ground asset
  14.         this.load.image('star', 'star.png'); // adding star asset
  15.         this.load.image('bomb', 'bomb.png'); // adding bomb asset
  16.  
  17.         /*
  18.         'dude' is loaded as a sprite sheet
  19.         because it contains animation frames for movement
  20.         an image would just load the same as above
  21.         but use spritesheets, they have a n i m a t i o n s already
  22.         */
  23.         this.load.spritesheet('dude',
  24.             '/assets/dude.png',
  25.             { frameWidth: 32, frameHeight: 48 }
  26.         ); 
  27.     }
  28.    
  29.     // adds to display
  30.     function create ()
  31.     {
  32.         // S T A R T . Y O U R . E N G I N E S .
  33.         game.physics.startSystem(Phaser.Physics.ARCADE);
  34.         this.add.image(0, 0, 'sky') // loading sky asset
  35.         game.physics.arcade.gravity.y = 1000; // world gravity
  36.  
  37.         // w o r l d . b o u n d s
  38.         //game.world.setBounds(0,0,1280, 600);
  39.  
  40.         // p l a t f o r m . g r o u p .
  41.         platforms = game.add.physicsGroup();
  42.         game.physics.enable(platforms, Phaser.Physics.ARCADE);
  43.  
  44.         platforms.create(75, 400, 'ground');
  45.         platforms.create(350, 500, 'ground');
  46.  
  47.         platforms.setAll('body.immovable', true);
  48.         platforms.setAll('body.allowGravity', false);
  49.  
  50.         // p l a y e r . c o n f i g .
  51.         dude = game.add.sprite(100, 100, 'dude'); // player
  52.         game.physics.enable(dude, Phaser.Physics.ARCADE); // add physics to dude
  53.         dude.body.gravity.y = 10000;
  54.  
  55.         dude.body.bounce.y = 0.3;
  56.         dude.body.collideWorldBounds = false;
  57.  
  58.         // c a m e r a . f o l l o w .
  59.         game.camera.follow(dude);
  60.  
  61.         // a n i m a t i o n s
  62.         dude.animations.add('left', [0, 1, 2, 3], 10, true);
  63.         dude.animations.add('turn', [4], 20, true);
  64.         dude.animations.add('right', [5, 6, 7, 8], 10, true);
  65.  
  66.         // c a m e r a
  67.         game.camera.follow(dude) // camera follow (<object>);
  68.  
  69.         // K E Y . R E G I S T E R .
  70.         cursors = game.input.keyboard.createCursorKeys(); // creates the arrow keys
  71.         jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACE); // space key for jump
  72.  
  73.     }
  74.  
  75.  
  76.     function update ()
  77.     {
  78.         game.physics.arcade.collide(platforms, dude);
  79.         // v e l o c i t y . v a l u e s .
  80.         dude.body.velocity.x = 0;
  81.         dude.body.velocity.y = 0;
  82.  
  83.         // m o v e m e n t . c o n t r o l s .
  84.         // move left
  85.         if (cursors.left.isDown)
  86.         {
  87.             dude.body.velocity.x = -75;
  88.  
  89.             if (facing != 'left')
  90.             {
  91.                 dude.animations.play('left');
  92.                 facing = 'left';
  93.             }
  94.  
  95.         }
  96.  
  97.         // move right
  98.         else if (cursors.right.isDown)
  99.         {
  100.             dude.body.velocity.x = 75;
  101.  
  102.             if (facing != 'right')
  103.             {
  104.                 dude.animations.play('right');
  105.                 facing = 'right';
  106.             }
  107.         }
  108.         else
  109.         {
  110.             if (facing != 'idle')
  111.             {
  112.                 dude.animations.stop() // stop animations
  113.  
  114.                 if (facing === 'left')
  115.                 {
  116.                     dude.frame = 0;
  117.                 }
  118.                 else if (facing == 'right')
  119.                 {
  120.                     dude.frame = 5;
  121.                 }
  122.             }
  123.         }
  124.  
  125.         // jump
  126.         if (jumpButton.isDown && player.body.onFloor())
  127.         {
  128.             dude.body.velocity.y = 1000000;
  129.         }
  130.  
  131.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement