mirror of
https://github.com/wassname/phaser.git
synced 2026-07-14 01:10:16 +08:00
* Fixed issue causing Keyboard.justPressed to always fire (thanks stemkoski)
* Added Keyboard.addKey() which creates a new Phaser.Key object that can be polled for updates, pressed states, etc. See the 2 new examples showing use.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
$title = "Creating and using a Key object";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
||||
|
||||
}
|
||||
|
||||
var sprite;
|
||||
|
||||
var upKey;
|
||||
var downKey;
|
||||
var leftKey;
|
||||
var rightKey;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#736357';
|
||||
|
||||
sprite = game.add.sprite(300, 300, 'phaser');
|
||||
|
||||
// In this example we'll create 4 specific keys (up, down, left, right) and monitor them in our update function
|
||||
|
||||
upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
|
||||
downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
|
||||
leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
|
||||
rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (upKey.isDown)
|
||||
{
|
||||
sprite.y--;
|
||||
}
|
||||
else if (downKey.isDown)
|
||||
{
|
||||
sprite.y++;
|
||||
}
|
||||
|
||||
if (leftKey.isDown)
|
||||
{
|
||||
sprite.x--;
|
||||
}
|
||||
else if (rightKey.isDown)
|
||||
{
|
||||
sprite.x++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
$title = "Keyboard Hotkeys";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
||||
game.load.image('logo', 'assets/sprites/phaser_tiny.png');
|
||||
game.load.image('pineapple', 'assets/sprites/pineapple.png');
|
||||
|
||||
}
|
||||
|
||||
var key1;
|
||||
var key2;
|
||||
var key3;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#736357';
|
||||
|
||||
// Here we create 3 hotkeys, keys 1-3 and bind them all to their own functions
|
||||
|
||||
key1 = game.input.keyboard.addKey(Phaser.Keyboard.ONE);
|
||||
key1.onDown.add(addPhaserDude, this);
|
||||
|
||||
key2 = game.input.keyboard.addKey(Phaser.Keyboard.TWO);
|
||||
key2.onDown.add(addPhaserLogo, this);
|
||||
|
||||
key3 = game.input.keyboard.addKey(Phaser.Keyboard.THREE);
|
||||
key3.onDown.add(addPineapple, this);
|
||||
|
||||
}
|
||||
|
||||
function addPhaserDude () {
|
||||
game.add.sprite(game.world.randomX, game.world.randomY, 'phaser');
|
||||
}
|
||||
|
||||
function addPhaserLogo () {
|
||||
game.add.sprite(game.world.randomX, game.world.randomY, 'logo');
|
||||
}
|
||||
|
||||
function addPineapple () {
|
||||
game.add.sprite(game.world.randomX, game.world.randomY, 'pineapple');
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
$title = "Keyboard justPressed";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
|
||||
|
||||
var phaser;
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
||||
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#736357';
|
||||
|
||||
phaser = game.add.sprite(300, 300, 'phaser');
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (game.input.keyboard.justPressed(Phaser.Keyboard.UP))
|
||||
{
|
||||
phaser.y--;
|
||||
}
|
||||
|
||||
if (game.input.keyboard.justPressed(Phaser.Keyboard.DOWN))
|
||||
{
|
||||
phaser.y++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
Reference in New Issue
Block a user