Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <title>Phaser - Making your first game, part 7</title>
- <script type="text/javascript" src="js/phaser.min.js"></script>
- <style type="text/css">
- body {
- margin: 0;
- }
- </style>
- </head>
- <body>
- <script type="text/javascript">
- var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render:render });
- function preload() {
- game.load.image('sky', 'assets/bg1.jpg');
- game.load.image('ground', 'assets/platform.png');
- game.load.image('star', 'assets/star.png');
- game.load.spritesheet('dude', 'assets/player.png', 96, 130);
- game.load.spritesheet('hamster', 'assets/hitler.png', 64, 64);
- }
- var player;
- var platforms;
- var cursors;
- function create() {
- // We're going to be using physics, so enable the Arcade Physics system
- game.physics.startSystem(Phaser.Physics.ARCADE);
- // A simple background for our game
- game.add.sprite(0, 0, 'sky');
- // The platforms group contains the ground and the 2 ledges we can jump on
- platforms = game.add.group();
- // We will enable physics for any object that is created in this group
- platforms.enableBody = true;
- // Here we create the ground.
- var ground = platforms.create(0, game.world.height - 64, 'ground');
- // Scale it to fit the width of the game (the original sprite is 400x32 in size)
- ground.scale.setTo(2, 2);
- // This stops it from falling away when you jump on it
- ground.body.immovable = true;
- // Now let's create two ledges
- var ledge = platforms.create(400, 400, 'ground');
- ledge.body.immovable = true;
- ledge = platforms.create(-150, 250, 'ground');
- ledge.body.immovable = true;
- // The player and its settings
- player = game.add.sprite(32, game.world.height - 250, 'dude');
- // We need to enable physics on the player
- game.physics.arcade.enable(player);
- // Player physics properties. Give the little guy a slight bounce.
- player.body.bounce.y = 0.2;
- player.body.gravity.y = 300;
- player.body.setSize(60,100, 15, 18);
- player.anchor.setTo(0.5, 0.5);
- player.body.collideWorldBounds = true;
- // Our two animations, walking left and right.
- player.animations.add('left', [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], 10, true);
- player.animations.add('right', [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], 10, true);
- player.animations.add('attack', [ 13, 14, 15, 16, 17, 18, 19, 20, 21], 10, true);
- // Our controls.
- 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();
- //actionKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
- // The hamster and its settings
- hamster = game.add.sprite(600, game.world.height - 650, 'hamster');
- hamster.frame = 24;
- // We need to enable physics on the player
- game.physics.arcade.enable(hamster);
- // Player physics properties. Give the little guy a slight bounce.
- hamster.body.bounce.y = 0.2;
- hamster.body.gravity.y = 300;
- hamster.body.collideWorldBounds = true;
- hamster.body.drag.setTo(10);
- // Our two animations, walking left and right.
- hamster.animations.add('walk', [ 24, 25, 26, 27 ], 10, true);
- hamster.animations.add('death', [ 32, 33, 34, 35, 36, 37, 38, 39], 10, false, true);
- }
- function update() {
- // Collide the player and the stars with the platforms
- game.physics.arcade.collide(player, platforms);
- game.physics.arcade.collide(hamster, platforms);
- game.physics.arcade.overlap(player, hamster, killHamster);
- // Reset the players velocity (movement)
- player.body.velocity.x = 0;
- if (cursors.space.downDuration(1000)) {
- player.animations.play('attack');
- }
- else if (cursors.left.isDown)
- {
- // Move to the left
- player.body.velocity.x = -150;
- //player.anchor.setTo(0.5, 0.5);
- player.scale.x = -1;
- player.animations.play('left');
- }
- else if (cursors.right.isDown)
- {
- // Move to the right
- player.body.velocity.x = 150;
- //player.anchor.setTo(0.5, 0.5);
- player.scale.x = 1;
- player.animations.play('right');
- }
- else
- {
- // Stand still
- player.animations.stop();
- player.frame = 11;
- }
- // Allow the player to jump if they are touching the ground.
- if (cursors.up.isDown && player.body.touching.down)
- {
- player.body.velocity.y = -350;
- }
- }
- function render(){
- this.game.debug.bodyInfo(player, 32, 32);
- this.game.debug.body(player);
- }
- function killHamster(player, hamster){
- if(cursors.space.downDuration(1000)){
- hamster.animations.play('death',10, false);
- hamster.body.enable = false;
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement