|
| 1 | +window.onload = function() { |
| 2 | + |
| 3 | + // Here is a custom group |
| 4 | + // It will automatically create 30 sprites of the given image when created. |
| 5 | + |
| 6 | + MonsterGroup = function (game, image, action) { |
| 7 | + |
| 8 | + Phaser.Group.call(this, game); |
| 9 | + |
| 10 | + for (var i = 0; i < 30; i++) |
| 11 | + { |
| 12 | + var sprite = this.create(game.world.randomX, game.world.randomY, image); |
| 13 | + |
| 14 | + if (action == 'bounce') |
| 15 | + { |
| 16 | + game.add.tween(sprite).to({ y: sprite.y - 100 }, 2000, Phaser.Easing.Elastic.Out, true, 0, 1000, true); |
| 17 | + } |
| 18 | + else if (action == 'slide') |
| 19 | + { |
| 20 | + game.add.tween(sprite).to({ x: sprite.x + 200 }, 4000, Phaser.Easing.Elastic.Out, true, 0, 1000, true); |
| 21 | + } |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + }; |
| 26 | + |
| 27 | + MonsterGroup.prototype = Object.create(Phaser.Group.prototype); |
| 28 | + MonsterGroup.prototype.constructor = MonsterGroup; |
| 29 | + |
| 30 | + var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create }); |
| 31 | + |
| 32 | + var customGroup1; |
| 33 | + var customGroup2; |
| 34 | + |
| 35 | + function preload() { |
| 36 | + |
| 37 | + game.load.image('ufo', 'assets/sprites/ufo.png'); |
| 38 | + game.load.image('baddie', 'assets/sprites/space-baddie.png'); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + function create() { |
| 43 | + |
| 44 | + customGroup1 = new MonsterGroup(game, 'ufo', 'bounce'); |
| 45 | + customGroup2 = new MonsterGroup(game, 'baddie', 'slide'); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | +}(); |
0 commit comments