mirror of
https://github.com/wassname/phaser.git
synced 2026-08-07 11:26:10 +08:00
Groups can now be added to other Groups as children via group.add() and group.addAt().
Groups now have an 'alive' property, which can be useful when iterating through child groups with functions like forEachAlive.
This commit is contained in:
@@ -1,49 +1,45 @@
|
||||
window.onload = function() {
|
||||
// Here is a custom group
|
||||
// It will automatically create 30 sprites of the given image when created.
|
||||
|
||||
// Here is a custom group
|
||||
// It will automatically create 30 sprites of the given image when created.
|
||||
MonsterGroup = function (game, image, action) {
|
||||
|
||||
MonsterGroup = function (game, image, action) {
|
||||
Phaser.Group.call(this, game);
|
||||
|
||||
Phaser.Group.call(this, game);
|
||||
for (var i = 0; i < 30; i++)
|
||||
{
|
||||
var sprite = this.create(game.world.randomX, game.world.randomY, image);
|
||||
|
||||
for (var i = 0; i < 30; i++)
|
||||
if (action == 'bounce')
|
||||
{
|
||||
var sprite = this.create(game.world.randomX, game.world.randomY, image);
|
||||
|
||||
if (action == 'bounce')
|
||||
{
|
||||
game.add.tween(sprite).to({ y: sprite.y - 100 }, 2000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
|
||||
}
|
||||
else if (action == 'slide')
|
||||
{
|
||||
game.add.tween(sprite).to({ x: sprite.x + 200 }, 4000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
|
||||
}
|
||||
|
||||
game.add.tween(sprite).to({ y: sprite.y - 100 }, 2000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
|
||||
}
|
||||
else if (action == 'slide')
|
||||
{
|
||||
game.add.tween(sprite).to({ x: sprite.x + 200 }, 4000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
MonsterGroup.prototype = Object.create(Phaser.Group.prototype);
|
||||
MonsterGroup.prototype.constructor = MonsterGroup;
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
|
||||
|
||||
var customGroup1;
|
||||
var customGroup2;
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('ufo', 'assets/sprites/ufo.png');
|
||||
game.load.image('baddie', 'assets/sprites/space-baddie.png');
|
||||
|
||||
}
|
||||
|
||||
function create() {
|
||||
};
|
||||
|
||||
customGroup1 = new MonsterGroup(game, 'ufo', 'bounce');
|
||||
customGroup2 = new MonsterGroup(game, 'baddie', 'slide');
|
||||
MonsterGroup.prototype = Object.create(Phaser.Group.prototype);
|
||||
MonsterGroup.prototype.constructor = MonsterGroup;
|
||||
|
||||
}
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
|
||||
|
||||
}();
|
||||
var customGroup1;
|
||||
var customGroup2;
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('ufo', 'assets/sprites/ufo.png');
|
||||
game.load.image('baddie', 'assets/sprites/space-baddie.png');
|
||||
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
||||
customGroup1 = new MonsterGroup(game, 'ufo', 'bounce');
|
||||
customGroup2 = new MonsterGroup(game, 'baddie', 'slide');
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('ball', 'assets/sprites/pangball.png');
|
||||
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
|
||||
|
||||
}
|
||||
|
||||
var ballsGroup;
|
||||
var shipsGroup;
|
||||
|
||||
function create() {
|
||||
|
||||
ballsGroup = game.add.group();
|
||||
shipsGroup = game.add.group();
|
||||
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
// Create some randomly placed sprites in both Groups
|
||||
ballsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'ball');
|
||||
shipsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'arrow');
|
||||
}
|
||||
|
||||
// Now make the ships group a child of the balls group - so anything that happens to the balls group
|
||||
// will happen to the ships group too
|
||||
ballsGroup.add(shipsGroup);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
ballsGroup.x += 0.1;
|
||||
|
||||
// Because shipsGroup is a child of ballsGroup it has already been moved 0.1px by the line above
|
||||
// So the following line of code will make it appear to move twice as fast as ballsGroup
|
||||
shipsGroup.x += 0.1;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user