|
| 1 | +<?php |
| 2 | + $title = "Using the keyboard to move a sprite"; |
| 3 | + require('../head.php'); |
| 4 | +?> |
| 5 | + |
| 6 | +<script type="text/javascript"> |
| 7 | + |
| 8 | +(function () { |
| 9 | + |
| 10 | + var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,update:update,render:render }); |
| 11 | + |
| 12 | + var ufo, |
| 13 | + leftBtn, |
| 14 | + rightBtn; |
| 15 | + var speed=4; |
| 16 | + |
| 17 | + function preload() { |
| 18 | + game.world.setSize(1280, 600); |
| 19 | + game.load.image('ground', 'assets/tests/ground-2x.png'); |
| 20 | + game.load.image('river', 'assets/tests/river-2x.png'); |
| 21 | + game.load.image('sky', 'assets/tests/sky-2x.png'); |
| 22 | + game.load.image('cloud0', 'assets/tests/cloud-big-2x.png'); |
| 23 | + game.load.image('cloud1', 'assets/tests/cloud-narrow-2x.png'); |
| 24 | + game.load.image('cloud2', 'assets/tests/cloud-small-2x.png'); |
| 25 | + |
| 26 | + game.load.spritesheet('button', 'assets/buttons/arrow-button.png', 112, 95); |
| 27 | + |
| 28 | + game.load.image('ufo', 'assets/sprites/ufo.png'); |
| 29 | + |
| 30 | + |
| 31 | + } |
| 32 | + function create() { |
| 33 | + // background images |
| 34 | + game.add.sprite(0, 0, 'sky') |
| 35 | + .scrollFactor.setTo(0, 0); |
| 36 | + game.add.sprite(0, 360, 'ground') |
| 37 | + .scrollFactor.setTo(0.5, 0.5); |
| 38 | + game.add.sprite(0, 400, 'river') |
| 39 | + .scrollFactor.setTo(1.3, 1.3); |
| 40 | + game.add.sprite(200, 120, 'cloud0') |
| 41 | + .scrollFactor.setTo(0.3, 0.3); |
| 42 | + game.add.sprite(-60, 120, 'cloud1') |
| 43 | + .scrollFactor.setTo(0.5, 0.3); |
| 44 | + game.add.sprite(900, 170, 'cloud2') |
| 45 | + .scrollFactor.setTo(0.7, 0.3); |
| 46 | + |
| 47 | + // Create a ufo spirte as player. |
| 48 | + ufo = game.add.sprite(320, 240, 'ufo'); |
| 49 | + ufo.anchor.setTo(0.5, 0.5); |
| 50 | + |
| 51 | + // Make the default camera follow the ufo. |
| 52 | + game.camera.follow(ufo); |
| 53 | + |
| 54 | + // Add 2 sprite to display hold direction. |
| 55 | + leftBtn = game.add.sprite(160 - 112, 200, 'button', 0); |
| 56 | + leftBtn.scrollFactor.setTo(0, 0); |
| 57 | + leftBtn.alpha = 0; |
| 58 | + rightBtn = game.add.sprite(640 - 112, 200, 'button', 1); |
| 59 | + rightBtn.alpha = 0; |
| 60 | + rightBtn.scrollFactor.setTo(0, 0); |
| 61 | + } |
| 62 | + function update() { |
| 63 | + // Check key states every frame. |
| 64 | + // Move ONLY one of the left and right key is hold. |
| 65 | + if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT) && |
| 66 | + !game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { |
| 67 | + ufo.x -= speed; |
| 68 | + ufo.rotation = -15; |
| 69 | + leftBtn.alpha = 0.6; |
| 70 | + } |
| 71 | + else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) && |
| 72 | + !game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { |
| 73 | + ufo.x += speed; |
| 74 | + ufo.rotation = 15; |
| 75 | + rightBtn.alpha = 0.6; |
| 76 | + } |
| 77 | + else { |
| 78 | + ufo.rotation = 0; |
| 79 | + leftBtn.alpha = rightBtn.alpha = 0; |
| 80 | + } |
| 81 | + } |
| 82 | + function render() { |
| 83 | + |
| 84 | + game.debug.renderText('Hold left/right to move the ufo.'); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +})(); |
| 89 | + |
| 90 | +</script> |
| 91 | + |
| 92 | +<?php |
| 93 | + require('../foot.php'); |
| 94 | +?> |
| 95 | + |
0 commit comments