Jump to content

Composite Sprite / Group Sprite with a Body?


rolandino
 Share

Recommended Posts

Hi,

Wonder if anyone can help.

I'd like to build a composite sprite - so - has a base image, then ontop of this a turret that swivels, and below the turret some eyes (I know, odd, but hey). I've been experimenting implementing this using a Group.

What I'd to be able to do is say something like:

"

this.game.physics.arcade.moveToObject(this, this.player.tank, this.speed);

"

where the "this" in this context is an "extended Sprite Group" - see the "extending a group" example up on http://examples.phaser.io/sideview.html.

Now the first query is - is it possible for a SpriteGroup to have a "body"?

If I currently run my code, the

"this.game.physics.arcade.moveToObject(this, this.player.tank, this.speed);" fails at:

"

displayObject.body.velocity.x = Math.cos(this._angle) * speed;

"

in the Phaser source *because* my Group Sprite does not have a body.

So, I'd like to be able to create a composite Sprite that I can move around as one entity, apply physics to, test for collisions, etc.

If there's an example, happily look at it.

Many thanks for any help,

R

Link to comment
Share on other sites

It's not well established as it's buried deeper in the pixi.js code, but sprites can also have children because they inherit from pixi's DisplayObjectContainer. By that token, it means that extending the sprite and/or using addChild you can add more sprites (or indeed images if you don't need all the extras a sprite gives) to a sprite just like a group, and still have the original sprite function as expected, with a body and so on.

Link to comment
Share on other sites

Hi Lewster32,

Thanks that's v helpful, but not the answer I expected!

So, what you're saying, I think, is that it sounds like to do what I wish to do, I should look at extending a sprite (call it UberSprite), and use addChild to make sprites part of UberSprite ? Please correct me if I'm wrong.

Presumably I can then use all the physics/collision gubbins that I'm looking to use ... ?

Thanks again btw.

R

Link to comment
Share on other sites

Yes that's exactly right. Any child objects added to the sprite will honour the scenegraph and be positioned relative to the parent, just like groups. I've found that extending a sprite and then adding images rather than sprites is a good way to improve performance, as usually the sprite's collision and physics need only be determined by the main 'container' sprite. If of course you wish to make a composite sprite with lots of sprites, each with their own body, you can do that too.

 

One caveat to the latter is that currently it seems pivot is slightly broken and can lead to weird issues with the bodies not lining up with the textures, especially when rotating. As long as you stick to using anchor though, everything should be fine.

Link to comment
Share on other sites

OK, terrific. Many thanks. And thanks for tip re: image and rotation, that's probably enough.

One follow-up (sorry) - I currently have something like the following:

"

newBitmap = this.game.make.bitmapData(22,22, "bgnd_bitmap);

newBitmap.circle(6,6,6,"#ffffff");

newSprite = game.add.sprite(x,y, newBitmap);

"

Instead of creating a sprite from the bitmap, can I not just create a Phaser.Image from a bitmap?

Doh. Read-the-f-documentation ... just found ... Phaser.Image.loadTexture(key, frame) ....

OK. Looks like I'm set ... for now.

Many, many thanks for your really super-terrific answers.

R

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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