|
| 1 | +/// <reference path="../../Phaser/Game.ts" /> |
| 2 | +(function () { |
| 3 | + var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render); |
| 4 | + |
| 5 | + var zombieCamera: Phaser.Camera; |
| 6 | + |
| 7 | + var zombie: Phaser.Sprite; |
| 8 | + var walkSpeed: Number = 2, |
| 9 | + direction: Number = 1; |
| 10 | + |
| 11 | + function init() { |
| 12 | + game.world.setSize(1280, 600, true); |
| 13 | + game.load.image('ground', 'assets/tests/ground-2x.png'); |
| 14 | + game.load.image('river', 'assets/tests/river-2x.png'); |
| 15 | + game.load.image('sky', 'assets/tests/sky-2x.png'); |
| 16 | + |
| 17 | + game.load.spritesheet('zombie', 'assets/sprites/metalslug_monster39x40.png', 39, 40); |
| 18 | + |
| 19 | + game.load.start(); |
| 20 | + } |
| 21 | + function create() { |
| 22 | + // Add background images. |
| 23 | + game.add.sprite(0, 0, 'sky'); |
| 24 | + game.add.sprite(0, 360, 'ground'); |
| 25 | + game.add.sprite(0, 400, 'river'); |
| 26 | + |
| 27 | + // Create zombie spirte |
| 28 | + zombie = game.add.sprite(480, 336, 'zombie'); |
| 29 | + zombie.animations.add('walk', null, 30, true); |
| 30 | + zombie.animations.play('walk'); |
| 31 | + |
| 32 | + // Create a small camera which looks at the zombie. |
| 33 | + // Use the same settings as the default camera. |
| 34 | + zombieCamera = game.add.camera(0, 0, 800, 600); |
| 35 | + // Use x and y properties to set the target area. |
| 36 | + zombieCamera.x = 420; |
| 37 | + zombieCamera.y = 240; |
| 38 | + // Resize the camera so that it will only look at 200x200 area. |
| 39 | + zombieCamera.setSize(200, 200); |
| 40 | + // Scale the camera to 2.0, now its target will be 100x100. |
| 41 | + zombieCamera.transform.scale.setTo(2.0, 2.0); |
| 42 | + // Use setPosition() method to set where the camera rendered |
| 43 | + // on the screen. |
| 44 | + zombieCamera.setPosition(0, 0); |
| 45 | + } |
| 46 | + function update() { |
| 47 | + if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { |
| 48 | + zombieCamera.x -= 2; |
| 49 | + } |
| 50 | + else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { |
| 51 | + zombieCamera.x += 2; |
| 52 | + } |
| 53 | + if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) { |
| 54 | + zombieCamera.y -= 2; |
| 55 | + } |
| 56 | + else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { |
| 57 | + zombieCamera.y += 2; |
| 58 | + } |
| 59 | + // zombie wandering update |
| 60 | + zombie.x += walkSpeed * direction; |
| 61 | + if (zombie.x > 540 || zombie.x < 440) { |
| 62 | + // Change walk direction. |
| 63 | + direction *= -1; |
| 64 | + // Flip zombie's animation. |
| 65 | + zombie.texture.flippedX = !zombie.texture.flippedX; |
| 66 | + } |
| 67 | + } |
| 68 | + function render() { |
| 69 | + game.camera.renderDebugInfo(32, 32); |
| 70 | + zombieCamera.renderDebugInfo(32, 128); |
| 71 | + } |
| 72 | +})(); |
0 commit comments