Jump to content

Modify phaser example - create sprite in group


Recommended Posts

In the Phaser examples there is 'Create Sprite in a group'.

Here's the code:


    var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { 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;
var enemies;

function create() {

    // Create some local groups for later use.
    friendAndFoe = game.add.group();
    enemies = game.add.group();

    // You can directly create a 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++)

    // 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.text('Tap screen or click to create new baddies.', 16, 24);



What if you wanted to add the same sprite 'the baddie' over and over again automatically in a fixed location, no button use, using a timer instead? 

Let's say, so it built up a horizontal row of baddies and then a vertical row of baddies, maybe one baddie every two seconds with a corresponding audio file (just a short beep), the same audio file over and over with each baddie being spawned.

There's another example; 'Create if null' - creates a sprite every second.  Something like the two examples combined???  Don't get me wrong, lots of examples there.

Anyone have any ideas how one might do the above?  Because I don't see any examples like what I'm describing, sprites, animations, fixed locations, timers, audio files, etc.

Link to comment
Share on other sites

I'm not sure what you're asking. Did you try calling "createBaddie" on a timer and it didn't work? You pass in the location to the call to create (right now with a couple of Math.random() values, which is why it's not in the same place every time).


OTOH, you could make a higher-order function to do it:

function createBaddieMaker(x, y) {  return function createBadde() {    return enemies.create(x, y, 'baddie');  }}

Something like that.

Link to comment
Share on other sites


What I was thinking of was something like this:(Like from a bad scifi movie)

The hero is standing in the midlle of a field, suddenly out of thin air, a baddie apears, and then another one and another one, etc. etc. all in a horizontal row, then a vertical row, then a horizontal row, then a vertical row - until the hero is completely surrounded.

Now in the example, the player clicks on the ufo - randomly creating a 'baddie' anywhere on the stage or screen.  So 'what if?'  there is no ufo, and the baddies appear regularly (right from the start of the game) in a fixed position (forming an army of sorts), surrounding the hero, who doesn't move.  And there is an audio file associated with the appearance of each baddie.  So my main question was, how does a timer and a sound file hook up to something like that in phaser?  Not randomly.  See what  I mean?  Don't see any phaser examples like that.


Merry Christmas!

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...