Groups examples and some boolean checks corrected

This commit is contained in:
Webeled 2013-09-27 13:27:15 +01:00
parent 18c695e9dd
commit 903b11b730
12 changed files with 563 additions and 5 deletions

View file

@ -71,9 +71,6 @@
game.camera.follow(ufo);
// follow style switch buttons
btn0 = game.add.button(6, 40, 'button', lockonFollow,this, 0, 0, 0);
btn1 = game.add.button(6, 120, 'button', platformerFollow,this, 1, 1, 1);

View file

@ -0,0 +1,63 @@
<?php
$title = "Adding to group using 'add'";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render:render });
var friendAndFoe,
enemies;
function preload() {
game.load.image('ufo', 'assets/sprites/ufo.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
}
function create() {
// Create some local groups for later use.
friendAndFoe = game.add.group();
enemies = game.add.group();
// Use game.add (GameObjectFactory) to create sprites, those
// newly created ones will be added to game.world.group
// automatically. While you can still use new to allocate and
// only add them to your own groups.
var ufo = game.add.sprite(200, 240, 'ufo');
friendAndFoe.add(ufo);
// Create some enemies using new keyword.
// (Don't forget to pass game as the first parameter.)
var enemy;
for (var i = 0; i < 16; i++) {
enemy = new Phaser.Sprite(game,
360 + Math.random() * 200, 120 + Math.random() * 200,
'baddie');
enemies.add(enemy);
}
}
function render() {
game.debug.renderText('ufo added to game.world.group and "friendAndFoe" group', 20, 24);
game.debug.renderText('others ONLY added to "enemies" group', 20, 40);
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,56 @@
<?php
$title = "Adding to group using create";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render:render });
function preload() {
game.load.image('ufo', 'assets/sprites/ufo.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
}
var friendAndFoe,
enemies;
function create() {
// Create some local groups for later use.
friendAndFoe = game.add.group();
enemies = game.add.group();
// You can directly create sprite and add it to a group
// using just one line.
friendAndFoe.create(200, 240, 'ufo');
// Create some enemies.
for (var i = 0; i < 8; i++) {
createBaddie();
}
// Tap to create new baddie sprites.
game.input.onTap.add(createBaddie, this);
}
function createBaddie() {
enemies.create(360 + Math.random() * 200, 120 + Math.random() * 200,'baddie');
}
function render() {
game.debug.renderText('Tap screen or click to create new baddies.', 16, 24);
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,101 @@
<?php
$title = "Bringing to top and game indexes";
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 });
function preload() {
game.load.image('beast', 'assets/pics/shadow_of_the_beast2_karamoon.png');
game.load.image('snot', 'assets/pics/nslide_snot.png');
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
game.load.image('coke', 'assets/sprites/cokecan.png');
game.load.image('disk', 'assets/sprites/oz_pov_melting_disk.png');
}
var group1,
group2,
coke,
disk;
function create() {
// Create a background image
game.add.sprite(0, 0, 'beast');
// Create a Group that will sit above the background image
group1 = game.add.group();
// Create a Group that will sit above Group 1
group2 = game.add.group();
// Now let's create some random sprites and enable them all for drag and 'bring to top'
for (var i = 0; i < 10; i++)
{
var tempSprite = game.add.sprite(game.stage.randomX, game.stage.randomY, 'atari1');
tempSprite.name = 'atari' + i;
tempSprite.input.start(i, true);
tempSprite.input.enableDrag(false, true);
group1.add(tempSprite);
// Sonics
var tempSprite=game.add.sprite(game.stage.randomX, game.stage.randomY, 'sonic');
tempSprite.name = 'sonic' + i;
tempSprite.input.start(10 + i, true);
tempSprite.input.enableDrag(false, true);
group2.add(tempSprite);
}
// Add 2 control sprites into each group - these cannot be dragged but should be bought to the top each time
coke = group1.create(100, 100, 'coke');
disk = group2.create(400, 300, 'disk');
// Create a foreground image - everything should appear behind this, even when dragged
var snot = game.add.sprite(game.world.centerX, game.world.height, 'snot');
snot.anchor.setTo(0.5, 1);
// You can click and drag any sprite but Sonic sprites should always appear above the Atari sprites
// and both types of sprite should only ever appear above the background and behind the
}
function update() {
if (game.input.keyboard.justReleased(Phaser.Keyboard.ONE))
{
coke.bringToTop();
}
if (game.input.keyboard.justReleased(Phaser.Keyboard.TWO))
{
disk.bringToTop();
}
}
function render() {
game.debug.renderInputInfo(32, 32);
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,46 @@
<?php
$title = "Bring to Top using parent container";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render : render });
var container;
function preload() {
game.load.spritesheet('button', 'assets/buttons/number-buttons.png', 160, 160);
}
function create() {
// Container for sorting the buttons, which we'll use to make buttons
// to the top later.
container = game.add.group();
// Add buttons to container.
container.add(game.add.button(200, 100, 'button', bringMeToTop, this, 0, 0, 0));
container.add(game.add.button(300, 100, 'button', bringMeToTop, this, 1, 1, 1));
container.add(game.add.button(100, 200, 'button', bringMeToTop, this, 2, 2, 2));
container.add(game.add.button(400, 200, 'button', bringMeToTop, this, 3, 3, 3));
container.add(game.add.button(300, 300, 'button', bringMeToTop, this, 4, 4, 4));
container.add(game.add.button(200, 300, 'button', bringMeToTop, this, 5, 5, 5));
}
function render() {
game.debug.renderText('Tap or click buttons to bring it to the top.', 32, 32);
}
function bringMeToTop(btn) {
container.bringToTop(btn);
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,52 @@
<?php
$title = "Calling a function on all members of the group";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render : render });
function preload() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.image('reviveBtn', 'assets/buttons/revive-button.png');
}
function create() {
// Add some items.
var item;
for (var i = 0; i < 3; i++) {
// Give the items a different alpha increase speed.
item = game.add.sprite(290, 98 * (i + 1), 'item', i);
// Enable input.
item.input.start(0, true);
item.events.onInputUp.add(kill);
// An item besides the left one.
item = game.add.sprite(388, 98 * (i + 1), 'item', i + 3);
item.input.start(0, true);
item.events.onInputUp.add(kill);
}
// Add a button to revive all the items.
game.add.button(270, 400, 'reviveBtn', reviveAll, this, 0, 0, 0);
}
function kill(item) {
item.kill();
}
function reviveAll() {
game.world.group.callAll('revive');
}
function render() {
game.debug.renderText('Tap or click an item to kill it, and press the revive button to revive them all.', 160, 500);
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,57 @@
<?php
$title = "Calling a function on all children";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create});
function preload() {
// Using Phasers asset loader we load up a PNG from the assets folder
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
}
var firstGroup;
function create() {
// Here we'll create a new Group
firstGroup = game.add.group();
// And add some sprites to it
for (var i = 0; i < 10; i++)
{
// Create a new sprite at a random screen location
var newSprite = new Phaser.Sprite(game, game.stage.randomX, game.stage.randomY, 'sonic');
// This set-ups a listener for the event, view your console.log output to see the result
newSprite.events.onAddedToGroup.add(logGroupAdd);
// Add the sprite to the Group
firstGroup.add(newSprite);
}
}
function logGroupAdd(sprite, group) {
console.log('Sprite added to Group', group.ID, 'at z-index:', group.getIndex(sprite));
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,64 @@
<?php
$title = "Retaining the display order when a sprite is dead";
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('atari1', 'assets/sprites/atari130xe.png');
game.load.image('atari2', 'assets/sprites/atari800xl.png');
game.load.image('card', 'assets/sprites/mana_card.png');
}
var items;
card;
function create() {
items = game.add.group();
// Items are rendered in the depth order in which they are added to the Group
items.create(64, 100, 'atari1');
card = items.create(240, 80, 'card');
items.create(280, 100, 'atari2');
game.input.onTap.addOnce(removeCard, this);// obviously the event can be fired only once
}
function removeCard() {
// Now let's kill the card sprite
card.kill();
game.input.onTap.addOnce(replaceCard, this);
}
function replaceCard() {
// And bring it back to life again - I assume it will render in the same place as before?
var bob = items.getFirstDead();
bob.revive();
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,50 @@
<?php
$title = "iterating over a group";
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 baseAlphaIncSpeed= 0.006;
function preload() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
}
function create() {
// Add some items.
for (var i = 0; i < 3; i++) {
game.add.sprite(290, 98 * (i + 1), 'item', i)
.alphaIncSpeed = baseAlphaIncSpeed * (i + 1);
game.add.sprite(388, 98 * (i + 1), 'item', i + 3)
.alphaIncSpeed = baseAlphaIncSpeed * (i + 4);
}
}
function update() {
// Animating alpha property of each item using forEach() method.
game.world.group.forEach(function(item) {
// Update alpha first.
item.alpha -= item.alphaIncSpeed;
// Check for switch between increasing and descreasing.
if (item.alpha < 0.001 || item.alpha > 0.999) {
item.alphaIncSpeed *= -1;
}
});
}
function render() {
game.debug.renderText('Alpha of items is always changing.', 280, 480);
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,72 @@
<?php
$title = "Getting the first child";
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});
function preload() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.image('reviveBtn', 'assets/buttons/revive-button.png');
}
var timer,
cycle;
function create() {
// Add some items.
var item;
for (var i = 0; i < 3; i++) {
// Give the items a different alpha increase speed.
item = game.add.sprite(290, 98 * (i + 1), 'item', i);
// An item beside the left one.
item = game.add.sprite(388, 98 * (i + 1), 'item', i + 3);
}
// Set a timer so we can perform an action after a delay.
timer = 0;
cycle = 1000;
}
function update() {
if (game.time.now > timer) {
// Update timer.
timer = game.time.now +cycle;
// Get the first alive item and kill it.
var item = game.world.group.getFirstAlive();
if(item){
item.kill();
}
}
}
function render() {
game.debug.renderText('One item will be killed each second.', 280, 420);
// Get living and dead number of a group.
game.debug.renderText('Living: ' + game.world.group.countLiving() + ', Dead: ' + game.world.group.countDead(), 330, 440);
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -31,7 +31,7 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
renderer = renderer || Phaser.AUTO;
parent = parent || '';
state = state || null;
transparent = transparent || false;
if (typeof transparent == 'undefined') { transparent = false; }
antialias = typeof antialias === 'undefined' ? true : antialias;
/**

View file

@ -75,7 +75,7 @@ Phaser.InputHandler.prototype = {
start: function (priority, useHandCursor) {
priority = priority || 0;
useHandCursor = useHandCursor || false;
if (typeof useHandCursor == 'undefined') { useHandCursor = false; }
// Turning on
if (this.enabled == false)