46 tests with assets and javascript code.

This commit is contained in:
Sean
2013-07-18 15:13:58 +08:00
parent d620fc7796
commit 9f30f656bc
83 changed files with 2168 additions and 94 deletions
+40
View File
@@ -0,0 +1,40 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
var friendAndFoe,
enemies;
function init() {
game.load.image('ufo', 'assets/sprites/ufo.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
game.load.start();
}
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() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('ufo added to game.world.group and "friendAndFoe" group', 16, 24);
Phaser.DebugUtils.context.fillText('others ONLY added to "enemies" group', 16, 40);
}
})();
+37
View File
@@ -0,0 +1,37 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
function init() {
game.load.image('ufo', 'assets/sprites/ufo.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
game.load.start();
}
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. (One thing you should know is, the body type
// of this sprite is set to BODY_DINAMIC by default, while it's
// BODY_DISABLED by default using other creating methods.)
friendAndFoe.addNewSprite(200, 240, 'ufo', null, Phaser.Types.BODY_DISABLED);
// 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.addNewSprite(360 + Math.random() * 200, 120 + Math.random() * 200,
'baddie', null,
Phaser.Types.BODY_DISABLED);
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Tap screen or click to create new baddies.', 16, 24);
}
})();
+30
View File
@@ -0,0 +1,30 @@
(function() {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
var container;
function init() {
game.load.spritesheet('button', 'assets/buttons/number-buttons.png', 160, 160);
game.load.start();
}
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() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Tap or click buttons to bring it to the top.', 32, 32);
}
function bringMeToTop(btn) {
container.bringToTop(btn);
}
})();
+37
View File
@@ -0,0 +1,37 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.image('reviveBtn', 'assets/buttons/revive-button.png');
game.load.start();
}
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, false, 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, false, 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() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Tap or click an item to kill it, and press the revive button to revive them all.', 160, 500);
}
})();
+38
View File
@@ -0,0 +1,38 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
// Left and right group.
var left, right;
// The first selected item.
var selected = null;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.start();
}
function create() {
left = game.add.group();
right = new Phaser.Group(game);
// Add some items to left side, and set a onDragStop listener
// to limit its location when dropped.
var item;
for (var i = 0; i < 3; i++) {
// Directly create sprites from the left group.
item = left.addNewSprite(250, 98 * (i + 1), 'item', i, Phaser.Types.BODY_DISABLED);
// Add another to the right group.
item = right.addNewSprite(348, 98 * (i + 1), 'item', i + 3, Phaser.Types.BODY_DISABLED);
}
exCamera = game.add.camera(0, 0, 800, 600);
exCamera.setPosition(120, 0);
}
function render() {
right.directRender(exCamera);
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Left Group', 300, 80);
Phaser.DebugUtils.context.fillText('Right Group', 400, 80);
Phaser.DebugUtils.context.fillText('Left group is normally rendered, while the right one is ONLY rendered directly to another camera.', 120, 480);
}
})();
+35
View File
@@ -0,0 +1,35 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var baseAlphaIncSpeed = 0.006;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.start();
}
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() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Alpha of items is always changing.', 280, 480);
}
})();
+44
View File
@@ -0,0 +1,44 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var timer, cycle;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.image('reviveBtn', 'assets/buttons/revive-button.png');
game.load.start();
}
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 besides 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() {
// Update timer.
timer += game.time.delta;
if (timer > cycle) {
timer -= cycle;
// Get the first alive item and kill it.
var item = game.world.group.getFirstAlive();
if (item) {
item.kill();
}
}
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('One item will be killed each second.', 280, 420);
// Get living and dead number of a group.
Phaser.DebugUtils.context.fillText('Living: ' + game.world.group.countLiving() + ', Dead: ' + game.world.group.countDead(), 330, 440);
}
})();
+49
View File
@@ -0,0 +1,49 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var timer, cycle;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.image('reviveBtn', 'assets/buttons/revive-button.png');
game.load.start();
}
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 besides 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() {
// Update timer.
timer += game.time.delta;
if (timer > cycle) {
timer -= cycle;
// Get an alive item from the group randomly, so it may not
// be the first to be killed.
// Also you can specific a range, only items between that range
// will be found and return.
// Set a range of (0, 5), so the first item will not be kill at all.
var item = game.world.group.getRandom(1, 5);
if (item) {
item.kill();
}
}
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('One item will be killed each second.', 280, 420);
Phaser.DebugUtils.context.fillText('Yet the first one will NEVER be killed since we use a range from 1 to 5 for selection.', 140, 432);
// Get living and dead number of a group.
Phaser.DebugUtils.context.fillText('Living: ' + game.world.group.countLiving() + ', Dead: ' + game.world.group.countDead(), 330, 460);
}
})();
+58
View File
@@ -0,0 +1,58 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var killTimer, reviveTimer, cycle;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.image('reviveBtn', 'assets/buttons/revive-button.png');
game.load.start();
}
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 besides 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.
killTimer = 0;
// Another timer for reviving.
reviveTimer = 0;
cycle = 1000;
}
function update() {
// Update timers.
killTimer += game.time.delta;
reviveTimer += game.time.delta;
// Kill first alive item every "cycle" duration.
if (killTimer > cycle) {
killTimer -= cycle;
// Get an alive item from the group and kill it.
var item = game.world.group.getFirstAlive();
if (item) {
item.kill();
}
}
// Revive first dead item every 1.5 "cycle" duration.
if (reviveTimer > cycle * 1.5) {
reviveTimer -= cycle * 1.5;
// Get a dead item from the group and revive it.
var item = game.world.group.getFirstDead();
if (item) {
item.revive();
}
}
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('One item will be killed each second and revived later.', 240, 420);
// Get living and dead number of a group.
Phaser.DebugUtils.context.fillText('Living: ' + game.world.group.countLiving() + ', Dead: ' + game.world.group.countDead(), 330, 440);
}
})();
+74
View File
@@ -0,0 +1,74 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
function init() {
game.world.setSize(1280, 800, true);
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('ufo', 'assets/sprites/ufo.png', 24, 21);
game.load.start();
}
function create() {
// Create the sky layer, behind everything and donot move.
var skyLayer = game.add.group();
skyLayer.z = 0;
// Create the cloud layer, only beyond the sky.
var cloudLayer = game.add.group();
cloudLayer.z = 1;
// Create the ground, behind the river and beyond clouds.
var groundLayer = game.add.group();
groundLayer.z = 2;
// Create the sprite layer. This should behind the river,
// and beyond the ground, cloud and sky layer.
var spriteLayer = game.add.group();
spriteLayer.z = 3;
// Create the river layer, beyond everything.
var riverLayer = game.add.group();
riverLayer.z = 4;
// Add sky background to skyLayer.
var sky = new Phaser.Sprite(game, 0, 0, 'sky');
sky.transform.scrollFactor.setTo(0, 0);
skyLayer.add(sky);
// Add clouds to cloudLayer.
var cloud0 = new Phaser.Sprite(game, 200, 120, 'cloud0');
cloud0.transform.scrollFactor.setTo(0.3, 0.1);
var cloud1 = new Phaser.Sprite(game, -60, 120, 'cloud1');
cloud1.transform.scrollFactor.setTo(0.5, 0.1);
var cloud2 = new Phaser.Sprite(game, 900, 170, 'cloud2');
cloud2.transform.scrollFactor.setTo(0.7, 0.1);
cloudLayer.add(cloud0);
cloudLayer.add(cloud1);
cloudLayer.add(cloud2);
// Add ground sprite to groundLayer.
var ground = new Phaser.Sprite(game, 0, 360, 'ground');
ground.transform.scrollFactor.setTo(0.5, 0.1);
groundLayer.add(ground);
// Add river to riverLayer.
var river = new Phaser.Sprite(game, 0, 400, 'river');
river.transform.scrollFactor.setTo(1.3, 0.16);
riverLayer.add(river);
// Add animating sprites to spriteLayer.
var ufo = new Phaser.Sprite(game, 360, 240, 'ufo');
ufo.animations.add('fly', null, 0, false);
ufo.animations.play('fly');
ufo.transform.origin.setTo(0.5, 0.5);
spriteLayer.add(ufo);
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('sky layer: z = 0', 16, 20);
Phaser.DebugUtils.context.fillText('cloud layer: z = 1', 16, 36);
Phaser.DebugUtils.context.fillText('ground layer: z = 2', 16, 52);
Phaser.DebugUtils.context.fillText('sprite layer: z = 3', 16, 68);
Phaser.DebugUtils.context.fillText('river layer: z = 4', 16, 84);
}
})();
+21
View File
@@ -0,0 +1,21 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
var robot;
var eye, body, leftArm, rightArm, leftLeg, rightLeg;
function init() {
game.load.spritesheet('buttons', 'assets/buttons/number-buttons.png', 90, 90);
game.load.start();
}
function create() {
// Add 6 groups and childs.
var item = game.add.group();
item.texture.loadImage('buttons');
}
function render() {
Phaser.DebugUtils.context.fillStyle = 'rgb(0, 160, 213)';
Phaser.DebugUtils.context.fillText('Group can have a texture too, so you can use it just like a simple sprite.', 180, 380);
}
})();
+65
View File
@@ -0,0 +1,65 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var robot;
var eye, body, leftArm, rightArm, leftLeg, rightLeg;
function init() {
game.load.image('eye', 'assets/sprites/robot/eye.png');
game.load.image('body', 'assets/sprites/robot/body.png');
game.load.image('arm-l', 'assets/sprites/robot/arm-l.png');
game.load.image('arm-r', 'assets/sprites/robot/arm-r.png');
game.load.image('leg-l', 'assets/sprites/robot/leg-l.png');
game.load.image('leg-r', 'assets/sprites/robot/leg-r.png');
game.load.start();
}
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 besides the left one.
item = game.add.sprite(388, 98 * (i + 1), 'item', i + 3);
}
// Use groups of sprites to create a big robot.
// Robot itself, you can subclass group class in a real game.
robot = game.add.group();
// Robot components.
leftArm = robot.addNewSprite(90, 175, 'arm-l', 0, Phaser.Types.BODY_DISABLED);
rightArm = robot.addNewSprite(549, 175, 'arm-r', 0, Phaser.Types.BODY_DISABLED);
leftLeg = robot.addNewSprite(270, 325, 'leg-l', 0, Phaser.Types.BODY_DISABLED);
rightLeg = robot.addNewSprite(410, 325, 'leg-r', 0, Phaser.Types.BODY_DISABLED);
body = robot.addNewSprite(219, 32, 'body', 0, Phaser.Types.BODY_DISABLED);
eye = robot.addNewSprite(335, 173,'eye', 0, Phaser.Types.BODY_DISABLED);
leftArm.input.start(0, false, true);
leftArm.input.enableDrag();
rightArm.input.start(0, false, true);
rightArm.input.enableDrag();
leftLeg.input.start(0, false, true);
leftLeg.input.enableDrag();
rightLeg.input.start(0, false, true);
rightLeg.input.enableDrag();
body.input.start(0, false, true);
body.input.enableDrag();
eye.input.start(0, false, true);
eye.input.enableDrag();
}
function update() {
}
function render() {
Phaser.DebugUtils.renderSpriteInfo(leftArm, 32, 32);
Phaser.DebugUtils.renderSpriteInfo(rightArm, 32, 152);
Phaser.DebugUtils.renderSpriteInfo(leftLeg, 32, 272);
Phaser.DebugUtils.renderSpriteInfo(rightLeg, 32, 392);
Phaser.DebugUtils.renderSpriteInfo(rightLeg, 450, 32);
Phaser.DebugUtils.renderSpriteInfo(rightLeg, 450, 152);
Phaser.DebugUtils.context.fillStyle = 'rgb(0, 160, 213)';
Phaser.DebugUtils.context.fillText('The robot is a group and every component is a sprite.', 240, 580);
// Phaser.DebugUtils.context.fillText('Drag each part to re-position them.', 288, 592);
Phaser.DebugUtils.context.fillText('Drag each part to re-position them. ', 288, 592);
}
})();
+48
View File
@@ -0,0 +1,48 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var robot;
var eye, body, leftArm, rightArm, leftLeg, rightLeg;
var draggable;
function init() {
game.load.image('eye', 'assets/sprites/robot/eye.png');
game.load.image('body', 'assets/sprites/robot/body.png');
game.load.image('arm-l', 'assets/sprites/robot/arm-l.png');
game.load.image('arm-r', 'assets/sprites/robot/arm-r.png');
game.load.image('leg-l', 'assets/sprites/robot/leg-l.png');
game.load.image('leg-r', 'assets/sprites/robot/leg-r.png');
game.load.start();
}
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 besides the left one.
item = game.add.sprite(388, 98 * (i + 1), 'item', i + 3);
}
// Use groups of sprites to create a big robot.
// Robot itself, you can subclass group class in a real game.
robot = game.add.group();
// Robot components.
leftArm = robot.addNewSprite(90, 175, 'arm-l', 0, Phaser.Types.BODY_DISABLED);
rightArm = robot.addNewSprite(549, 175, 'arm-r', 0, Phaser.Types.BODY_DISABLED);
leftLeg = robot.addNewSprite(270, 325, 'leg-l', 0, Phaser.Types.BODY_DISABLED);
rightLeg = robot.addNewSprite(410, 325, 'leg-r', 0, Phaser.Types.BODY_DISABLED);
body = robot.addNewSprite(219, 32, 'body', 0, Phaser.Types.BODY_DISABLED);
eye = robot.addNewSprite(335, 173,'eye', 0, Phaser.Types.BODY_DISABLED);
}
function update() {
// Change parent's rotation to change all the childs.
// robot.transform.rotation += 2;
game.world.group.transform.rotation += 2;
}
function render() {
Phaser.DebugUtils.context.fillStyle = 'rgb(0, 160, 213)';
Phaser.DebugUtils.context.fillText('The robot is a group and every component is a sprite.', 240, 580);
}
})();
+51
View File
@@ -0,0 +1,51 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var robot;
var eye, body, leftArm, rightArm, leftLeg, rightLeg;
var draggable;
function init() {
game.load.image('eye', 'assets/sprites/robot/eye.png');
game.load.image('body', 'assets/sprites/robot/body.png');
game.load.image('arm-l', 'assets/sprites/robot/arm-l.png');
game.load.image('arm-r', 'assets/sprites/robot/arm-r.png');
game.load.image('leg-l', 'assets/sprites/robot/leg-l.png');
game.load.image('leg-r', 'assets/sprites/robot/leg-r.png');
game.load.start();
}
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 besides the left one.
item = game.add.sprite(388, 98 * (i + 1), 'item', i + 3);
}
// Use groups of sprites to create a big robot.
// Robot itself, you can subclass group class in a real game.
robot = game.add.group();
// Robot components.
leftArm = robot.addNewSprite(90, 175, 'arm-l', 0, Phaser.Types.BODY_DISABLED);
rightArm = robot.addNewSprite(549, 175, 'arm-r', 0, Phaser.Types.BODY_DISABLED);
leftLeg = robot.addNewSprite(270, 325, 'leg-l', 0, Phaser.Types.BODY_DISABLED);
rightLeg = robot.addNewSprite(410, 325, 'leg-r', 0, Phaser.Types.BODY_DISABLED);
body = robot.addNewSprite(219, 32, 'body', 0, Phaser.Types.BODY_DISABLED);
eye = robot.addNewSprite(335, 173,'eye', 0, Phaser.Types.BODY_DISABLED);
// Tween the robot's size, so all the components also scaled.
// game.add.tween(robot.transform.scale)
game.add.tween(game.world.group.transform.scale)
.to({x: 1.2, y: 1.2}, 1000, Phaser.Easing.Back.InOut, true, 0, false)
.yoyo(true);
}
function update() {
}
function render() {
Phaser.DebugUtils.context.fillStyle = 'rgb(0, 160, 213)';
Phaser.DebugUtils.context.fillText('The robot is a group and every component is a sprite.', 240, 580);
}
})();
+46
View File
@@ -0,0 +1,46 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
function init() {
game.load.image('ufo', 'assets/sprites/ufo.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
game.load.spritesheet('button', 'assets/buttons/baddie-buttons.png', 224, 70);
game.load.start();
}
function create() {
// Create some local groups for later use.
friendAndFoe = game.add.group();
enemies = game.add.group();
// Create a ufo.
friendAndFoe.addNewSprite(200, 240, 'ufo', null, Phaser.Types.BODY_DISABLED);
// Create some enemies.
for (var i = 0; i < 8; i++) {
createBaddie();
}
// Create buttons to create and kill baddies.
game.add.button(16, 50, 'button', createBaddie, 0, 0, 0);
game.add.button(16, 130, 'button', killBaddie, 1, 1, 1);
}
function killBaddie() {
var baddie = enemies.getFirstAlive();
if (baddie) baddie.kill();
}
function createBaddie() {
// Group's recycle() method will always return a valid object unless
// you did not pass an objectClass parameter.
// It will create new object instance of the given class if no "dead"
// object can be found inside the group.
var enemy = enemies.recycle(Phaser.Sprite);
enemy.texture.loadImage('baddie', false);
enemy.texture.opaque = false;
enemy.x = 360 + Math.random() * 200;
enemy.y = 120 + Math.random() * 200;
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Add new baddies using recyle() instead of allocating new object every time.', 16, 24);
}
})();
+49
View File
@@ -0,0 +1,49 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
function init() {
game.load.image('ufo', 'assets/sprites/ufo.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
game.load.spritesheet('button', 'assets/buttons/baddie-buttons.png', 224, 70);
game.load.start();
}
function create() {
// Create some local groups for later use.
friendAndFoe = game.add.group();
enemies = game.add.group();
// Create a ufo.
friendAndFoe.addNewSprite(200, 240, 'ufo', null, Phaser.Types.BODY_DISABLED);
// Create some enemies.
for (var i = 0; i < 8; i++) {
// Since the getFirstAvailable() which we'll use for recycling
// cannot allocate new objects, create them manually here.
enemies.addNewSprite(360 + Math.random() * 200, 120 + Math.random() * 200,
'baddie', null, Phaser.Types.BODY_DISABLED);
}
// Create buttons to create and kill baddies.
game.add.button(16, 50, 'button', createBaddie, 0, 0, 0);
game.add.button(16, 130, 'button', killBaddie, 1, 1, 1);
}
function killBaddie() {
var baddie = enemies.getFirstAlive();
if (baddie) baddie.kill();
}
function createBaddie() {
// Recycle using getFirstAvailable() as an alternative to recycle().
// Notice that this method will not create new objects if there's no one
// available, and it won't change size of this group.
var enemy = enemies.getFirstAvailable();
if (enemy) {
enemy.revive();
}
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Recycle baddies from a group using getFirstAvailable() instead of recycle().', 16, 24);
Phaser.DebugUtils.context.fillText('Notice that you cannot add more than 8 baddies since we only create 8 instance.', 16, 36);
Phaser.DebugUtils.context.fillText('Living baddies: ' + enemies.countLiving(), 340, 420);
}
})();
+65
View File
@@ -0,0 +1,65 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
// Group contains items.
var items;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.image('rect', 'assets/tests/200x100corners.png');
game.load.image('rect2', 'assets/tests/200x100corners2.png');
game.load.start();
}
function create() {
// Create item container group.
items = game.add.group();
// Add some items and add them to the container group,
// then you can drag and drop them to remove.
var item;
for (var i = 0; i < 6; i++) {
// Directly create sprites from the group.
item = items.addNewSprite(90, 90 * i, 'item', i, Phaser.Types.BODY_DISABLED);
// Enable input detection, then it's possible be dragged.
item.input.start(0, false, true);
// Make this item draggable.
item.input.enableDrag();
// Then we make it snap to 90x90 grids.
item.input.enableSnap(90, 90, false, true);
// Add a handler to remove it using different options when dropped.
item.events.onDragStop.add(dropHandler);
}
// Create 2 rectangles, drop it at these rectangle to
// remove it from origin group normally or
// cut it from the group's array entirely.
var rect = game.add.sprite(400, 0, 'rect');
rect.transform.scale.setTo(2.0, 3.0);
var rect2 = game.add.sprite(400, 300, 'rect2');
rect2.transform.scale.setTo(2.0, 3.0);
}
function update() {
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Size of group: ' + items.length, 100, 560);
Phaser.DebugUtils.context.fillText('Drop here to cut items from groups entirely.', 450, 24);
Phaser.DebugUtils.context.fillText('Drop here to remove it normally.', 450, 324);
}
function dropHandler(item, pointer) {
if (item.x < 90) {
item.x = 90;
}
else if (item.x > 400) { // So it is dropped in one rectangle.
if (item.y < 300) {
// Remove it from group normally, so the group's size does not change.
items.remove(item, true);
}
else {
// Remove it from group and cut from it, so the group's size decreases.
items.remove(item);
}
}
}
})();
+66
View File
@@ -0,0 +1,66 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
// Left and right group.
var left, right;
// The first selected item.
var selected = null;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.start();
}
function create() {
left = game.add.group();
right = game.add.group();
// Add some items to left side, and set a onDragStop listener
// to limit its location when dropped.
var item;
for (var i = 0; i < 3; i++) {
// Directly create sprites from the left group.
item = left.addNewSprite(290, 98 * (i + 1), 'item', i, Phaser.Types.BODY_DISABLED);
// Enable input.
item.input.start(0, false, true);
item.events.onInputUp.add(select);
// Add another to the right group.
item = right.addNewSprite(388, 98 * (i + 1), 'item', i + 3, Phaser.Types.BODY_DISABLED);
// Enable input.
item.input.start(0, false, true);
item.events.onInputUp.add(select);
}
}
function select(item, pointer) {
// If there's no one selected, mark it as selected.
if (!selected) {
selected = item;
selected.alpha = 0.5;
}
else {
// Items from different group selected, replace with each other;
// Something like a swap action, maybe better done with
// group.swap() method.
if (selected.group !== item.group) {
// Move the later selected to the first selected item's position.
item.x = selected.x;
item.y = selected.y;
// Replace first selected with the second one.
selected.group.replace(selected, item);
}
else {
selected.alpha = 1;
}
// After checking, now clear the helper var.
selected = null;
}
}
function update() {
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Left Group', 300, 80);
Phaser.DebugUtils.context.fillText('Right Group', 400, 80);
Phaser.DebugUtils.context.fillText('Click an item and one from another group to replace it.', 240, 480);
}
})();
+31
View File
@@ -0,0 +1,31 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
var baseIncSpeed = 0.006;
function init() {
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
game.load.start();
}
function create() {
// Add some items.
for (var i = 0; i < 3; i++) {
// Give the items a different alpha increase speed.
game.add.sprite(290, 98 * (i + 1), 'item', i)
.alphaIncSpeed = baseIncSpeed * (i + 1);
game.add.sprite(388, 98 * (i + 1), 'item', i + 3)
.alphaIncSpeed = baseIncSpeed * (i + 4);
}
game.input.onTap.add(resetAlpha);
}
function resetAlpha() {
// Set "alpha" value of all the childs.
game.world.group.setAll('alpha', Math.random());
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Tap or click to set random alpha of all the items.', 240, 480);
}
})();
+46
View File
@@ -0,0 +1,46 @@
(function() {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
var xTop, yTop, zTop;
function init() {
game.load.image('cell', 'assets/sprites/diamond.png');
game.load.start();
}
function create() {
// Create 3 groups which will have different sort "index".
xTop = game.add.group();
yTop = game.add.group();
zTop = game.add.group();
var i;
for (i = 0; i < 64; i++) {
xTop.addNewSprite(160 + 48 * Math.cos(i * Math.PI / 8), 540 - i * 8,
'cell', 0,
Phaser.Types.BODY_DISABLED);
}
for (i = 0; i < 64; i++) {
yTop.addNewSprite(340 + 48 * Math.cos(i * Math.PI / 8), 540 - i * 8,
'cell', 0,
Phaser.Types.BODY_DISABLED);
}
for (i = 0; i < 64; i++) {
zTop.addNewSprite(520 + 48 * Math.cos(i * Math.PI / 8), 540 - i * 8,
'cell', 0,
Phaser.Types.BODY_DISABLED);
}
}
function update() {
// Sort 3 groups using different methods, all of them are
// ascending by default.
xTop.sort('x');
yTop.sort('y');
zTop.sort('z');
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Left group sorted by x.', 16, 18);
Phaser.DebugUtils.context.fillText('Middle group sorted by y.', 16, 36);
Phaser.DebugUtils.context.fillText('Right group sorted by z.', 16, 54);
}
})();
+57
View File
@@ -0,0 +1,57 @@
(function() {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
// Wabbits inside this group is sorted by its "dead" property.
// Dead wabbits behinds the others.
var wabbits;
function init() {
game.load.image('wabbit', 'assets/sprites/wabbit.png');
game.load.start();
}
function create() {
// Create container group.
wabbits = game.add.group();
// Create wabbit and add to the container.
var wabbe;
for (var i = 0; i < 64; i++) {
wabbe = wabbits.addNewSprite(Math.random() * 480 + 64, Math.random() * 480 + 32,
'wabbit', 0,
Phaser.Types.BODY_DISABLED);
wabbe.transform.scale.setTo(2, 2);
wabbe.transform.origin.setTo(0.5, 0.5);
// Give wabbie a flag of living or not.
wabbe.dead = false;
wabbe.input.start(0, false, true);
wabbe.events.onInputUp.add(killMe, this);
}
}
function update() {
// sort wabbies by "exists", so killed ones will
wabbits.sort('dead', Phaser.Group.DESCENDING);
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Tap or click wabbits to kill them.', 32, 32);
}
function killMe(wabbe) {
// Disable input.
wabbe.input.stop();
// Do not call the kill method, set its "dead" property instead.
wabbe.dead = true;
// Kill effects.
game.add.tween(wabbe)
.to({x: wabbe.x - 48}, 2000, Phaser.Easing.Linear.None, true, 0, false);
game.add.tween(wabbe)
.to({y: 640}, 2000 - wabbe.y, Phaser.Easing.Back.In, true, 0, false);
game.add.tween(wabbe)
.to({rotation: 240}, 1000, Phaser.Easing.Back.In, true, 0, false);
game.add.tween(wabbe.transform.scale)
.to({x: 2, y: 2}, 1000, Phaser.Easing.Bounce.In, true, 0, false);
}
})();
+60
View File
@@ -0,0 +1,60 @@
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
// Groups for storing friends and enemies, may use for collision later.
var friendAndFoe,
enemies;
// Groups for teaming up stuff.
var normalBaddies,
purpleBaddies;
function init() {
game.load.image('ufo', 'assets/sprites/ufo.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
game.load.image('purple-baddie', 'assets/sprites/space-baddie-purple.png');
game.load.start();
}
function create() {
// Create some local groups for later use.
friendAndFoe = game.add.group();
enemies = game.add.group();
normalBaddies = game.add.group();
purpleBaddies = game.add.group();
// Add both teams to enemies group.
enemies.add(normalBaddies);
enemies.add(purpleBaddies);
// Create a ufo as a friend sprite.
friendAndFoe.addNewSprite(200, 240, 'ufo', null, Phaser.Types.BODY_DISABLED);
// Create some enemies.
for (var i = 0; i < 16; i++) {
createBaddie();
}
// Tap to create new baddie sprites.
game.input.onTap.add(createBaddie, this);
}
function createBaddie() {
var baddie;
if (Math.random() > 0.5) {
baddie = purpleBaddies.addNewSprite(360 + Math.random() * 200, 120 + Math.random() * 200,
'purple-baddie', null, Phaser.Types.BODY_DISABLED);
}
else {
baddie = normalBaddies.addNewSprite(360 + Math.random() * 200, 120 + Math.random() * 200,
'baddie', null, Phaser.Types.BODY_DISABLED);
}
}
function render() {
Phaser.DebugUtils.context.fillStyle = '#fff';
Phaser.DebugUtils.context.fillText('Tap screen or click to create new baddies.', 16, 24);
Phaser.DebugUtils.context.fillText('enemies: ' + enemies.length + ' (actually ' + enemies.length + ' groups)', 16, 48);
Phaser.DebugUtils.context.fillText('normal baddies: ' + normalBaddies.length, 16, 60);
Phaser.DebugUtils.context.fillText('purple baddies: ' + purpleBaddies.length, 16, 72);
Phaser.DebugUtils.context.fillText('friends: ' + friendAndFoe.length, 16, 96);
}
})();