mirror of
https://github.com/wassname/phaser.git
synced 2026-06-27 16:10:15 +08:00
Corrected few typos and added a keyboard exampl
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
friendAndFoe = game.add.group();
|
||||
enemies = game.add.group();
|
||||
|
||||
// You can directly create sprite and add it to a group
|
||||
// You can directly create a sprite and add it to a group
|
||||
// using just one line.
|
||||
friendAndFoe.create(200, 240, 'ufo');
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
<?php
|
||||
$title = "Calling a function on all children";
|
||||
$title = "Add a new sprite and log what happened";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
<?php
|
||||
$title = "Grouping";
|
||||
$title = "Rotating an entire group";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
<?php
|
||||
$title = "Grouping";
|
||||
$title = "Grouping and dragging";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
$title = "Using the keyboard to move a sprite";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,update:update,render:render });
|
||||
|
||||
var ufo,
|
||||
leftBtn,
|
||||
rightBtn;
|
||||
var speed=4;
|
||||
|
||||
function preload() {
|
||||
game.world.setSize(1280, 600);
|
||||
game.load.image('ground', 'assets/tests/ground-2x.png');
|
||||
game.load.image('river', 'assets/tests/river-2x.png');
|
||||
game.load.image('sky', 'assets/tests/sky-2x.png');
|
||||
game.load.image('cloud0', 'assets/tests/cloud-big-2x.png');
|
||||
game.load.image('cloud1', 'assets/tests/cloud-narrow-2x.png');
|
||||
game.load.image('cloud2', 'assets/tests/cloud-small-2x.png');
|
||||
|
||||
game.load.spritesheet('button', 'assets/buttons/arrow-button.png', 112, 95);
|
||||
|
||||
game.load.image('ufo', 'assets/sprites/ufo.png');
|
||||
|
||||
|
||||
}
|
||||
function create() {
|
||||
// background images
|
||||
game.add.sprite(0, 0, 'sky')
|
||||
.scrollFactor.setTo(0, 0);
|
||||
game.add.sprite(0, 360, 'ground')
|
||||
.scrollFactor.setTo(0.5, 0.5);
|
||||
game.add.sprite(0, 400, 'river')
|
||||
.scrollFactor.setTo(1.3, 1.3);
|
||||
game.add.sprite(200, 120, 'cloud0')
|
||||
.scrollFactor.setTo(0.3, 0.3);
|
||||
game.add.sprite(-60, 120, 'cloud1')
|
||||
.scrollFactor.setTo(0.5, 0.3);
|
||||
game.add.sprite(900, 170, 'cloud2')
|
||||
.scrollFactor.setTo(0.7, 0.3);
|
||||
|
||||
// Create a ufo spirte as player.
|
||||
ufo = game.add.sprite(320, 240, 'ufo');
|
||||
ufo.anchor.setTo(0.5, 0.5);
|
||||
|
||||
// Make the default camera follow the ufo.
|
||||
game.camera.follow(ufo);
|
||||
|
||||
// Add 2 sprite to display hold direction.
|
||||
leftBtn = game.add.sprite(160 - 112, 200, 'button', 0);
|
||||
leftBtn.scrollFactor.setTo(0, 0);
|
||||
leftBtn.alpha = 0;
|
||||
rightBtn = game.add.sprite(640 - 112, 200, 'button', 1);
|
||||
rightBtn.alpha = 0;
|
||||
rightBtn.scrollFactor.setTo(0, 0);
|
||||
}
|
||||
function update() {
|
||||
// Check key states every frame.
|
||||
// Move ONLY one of the left and right key is hold.
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT) &&
|
||||
!game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
|
||||
ufo.x -= speed;
|
||||
ufo.rotation = -15;
|
||||
leftBtn.alpha = 0.6;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) &&
|
||||
!game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
|
||||
ufo.x += speed;
|
||||
ufo.rotation = 15;
|
||||
rightBtn.alpha = 0.6;
|
||||
}
|
||||
else {
|
||||
ufo.rotation = 0;
|
||||
leftBtn.alpha = rightBtn.alpha = 0;
|
||||
}
|
||||
}
|
||||
function render() {
|
||||
|
||||
game.debug.renderText('Hold left/right to move the ufo.');
|
||||
}
|
||||
|
||||
|
||||
})();
|
||||
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user