Jump to content

Creating a sprite without adding it (and its body) to world (p2)


Recommended Posts



I would like to create a sprite (with a texture and a polygon body) without adding it to the world.

I know that without game.add.existing(myEnnemy); it will not be shown, but his body IS in the world (yeah that's strange).


Thank you :)


Edit: I would like a kind of SpriteFactory actually with the ability to create multiple instance of the same sprite.

Like this:

Ennemy = new Ennemy(x,y, key, body);var ennemy1 = Ennemy.createNew();var ennemy2 Ennemy.createNew();var ennemy3 Ennemy.createNew();

But it seems a bit complicated.

Link to comment
Share on other sites

Rats, hit enter too soon.


game.make.sprite will create a Sprite and not add it to the world. If you want to give it a P2 body but not have the P2 body added to the physics world you currently have to do it like this:

var yourSprite = game.make.sprite(x, y, ...);yourSprite.exists = false;game.physics.p2.enable(yourSprite);

The 'exists' false will still give the sprite a body, but the body won't be added into the simulation.


This is far easier to wrap up when done in your own class.

Link to comment
Share on other sites

Oh nice. Thank you.

Now the problem is that I don't want to make a sprite but a custom class that extends Sprite.


Edit: Oh, this second answer is quite making me happy. I already tried mySprite.exists = false; but noticed that the body was still in the world, I didn't know I should have done it BEFORE enabling physics (and it works like this).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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