Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Game Class and Sprite Children?


jjwallace
 Share

Recommended Posts

When you create a game object do sprites inside its create class automatically become children of that class?

 

Frome Examples:

MonsterBunny = function (game, rotateSpeed) {

    //  We call the Phaser.Sprite passing in the game reference
    //  We're giving it a random X/Y position here, just for the sake of this demo - you could also pass the x/y in the constructor
    Phaser.Sprite.call(this, game, game.world.randomX, game.world.randomY, 'bunny');

    this.anchor.setTo(0.5, 0.5);

    this.rotateSpeed = rotateSpeed;

    var randomScale = 0.1 + Math.random();

    this.scale.setTo(randomScale, randomScale)

    game.add.existing(this);

};

MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);
MonsterBunny.prototype.constructor = MonsterBunny;

MonsterBunny.prototype.update = function() {

    //  Automatically called by World.update
    this.angle += this.rotateSpeed;

};

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });

function preload() {

    game.load.image('bunny', 'assets/sprites/bunny.png');

}

function create() {

    for (var i = 0.1; i < 2; i += 0.1)
    {
        new MonsterBunny(game, i);
    }

}

 

Link to comment
Share on other sites

Not exactly sure what you're asking as the example doesn't have any sprites being created within the MonsterBunny class, the MonsterBunny class itself is a subclass of Phaser.Sprite though.

But if I understand you correctly, the answer is; no, not automatically. It depends on how you create those sprites as to who their parent is...

game.add.sprite(x, y, key, frame); //Added to the world

game.add.sprite(x, y, key, frame, group); //Added to the provided group

var sprite = game.add.sprite(x, y, key, frame); //Added to the world
this.addChild(sprite); //Now it's added to the MonsterBunny instead

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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