Jump to content

Why is my extended sprite's body null?


Miniversal
 Share

Recommended Posts

I have a very simple extended sprite that just adds an index value of what spawn position that particular sprite occupies. I can't figure out why the body is null when I try to set some other attributes of the new sprite though. Any input on what am I doing wrong here?

//////////////////////////////////////////////////////////////////////////
//  BRICK
//  Custom Sprite object that will included a property to keep track of
//  which spawnPoint the brick occupies
//////////////////////////////////////////////////////////////////////////
            var Brick = function(game, key, x, y, idx){
                Phaser.Sprite.call(this, game, x, y, key);
                this.anchor.x = 0.5;
                this.anchor.y = 0.5;
                this.body.bounce.set(1); //<--ERROR this.body is null???
                this.body.immovable = true;
                this.spawnPoint = idx;
            }
            Brick.prototype = Object.create(Phaser.Sprite.prototype);
            Brick.prototype.constructor = Brick;

//The code that creates the sprite...

var brick = new Brick(game, 'brick_' + brickNum, spawnPoint.x, spawnPoint.y, spawnPoint.idx);
bricks.add(brick);

 

If I don't use the extended sprite and just use group.create() then the sprite body is not null and everything works. Is it because brick is a reference to the group's object and not the sprite? If so, how do I go about setting the attributes on the sprite's group element?

brick = bricks.create(spawnPoint.x, spawnPoint.y, 'brick_' + brickNum);
brick.anchor.x = 0.5;
brick.anchor.y = 0.5;
brick.body.bounce.set(1);
brick.body.immovable = true;

 

Link to comment
Share on other sites

A Sprite doesn't have a body by default. You have to add one manually : either by applying physics to the sprite in its constructor, or by putting the sprite into a physics group (which you are doing). 

Sprite = image

Body = physics.

A sprite can very well live without a body. Think about a Candy Crush-like game : lots of images, but no physics!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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