Jump to content

Setting a sprite body size when sprite change size


Mike
 Share

Recommended Posts

If I do this:

 

function paddleUpdate() {

 

        if(isPaddleNerfed) {
            paddle.frameName = "paddle_small.png"; //48px width
        } else {
            paddle.frameName = "paddle_big.png"; //32px width
        }
 

//Do I have to set the size manually like this 

 

paddle.body.setSize(paddle._cache.width, paddle._cache.height);

 

}

 

I ask because with: game.debug.renderSpriteBounds(paddle);

 

I see the pink border and it's changing size (but only visually): collisions and also sprite.body.width still returns the old bigger size rather than the smaller/nerfed one...

 

Also visually it's all ok... and also sprite._cache.width returns correct values. :)

 

Is my aproach right or is there a better, shorter way ... i clearly understand that it can't and shouldn't be automatic because changing sprite/frames should change the

collision body region... but maybe it can be added as option ... or maybe shouldn't I'll be glad to here more opinions about this.

 

 

EDIT:

 

I just saw that there is: game.debug.renderSpriteBody(paddle); so the only question that leave is this the proper way of doing it since the sprite.body isn't updating on sprite size/frame change

Link to comment
Share on other sites

I believe you should never need to access the _cache contents (they'd be private if js supported it).

 

This crosses over with a question I wanted to ask - is it possible to find the dimensions of a resource game.load.image('fred', 'grfx/fredPic.png') without creating an instance of it?

If the answer to my question is yes, then the same approach would work for you to get the width of your paddle without accessing the _cache.

 

To the other part of your question, I think your general idea - change the image, then reset the size... seems right to me.  With 1.07 the built-in physics is becoming more optional so adding another parameter option or a new function call is probably going the wrong way.

Link to comment
Share on other sites

I purposely don't adjust the Sprite body size on a frame update. The reason is that you're supposed to set a body that fits the frames of an animation really, and if you want to totally change the body then you ought to update the body size at the same time. For example if a sprite was stood next to a wall and I adjusted the body because the animation frame changed, it could potentially penetrate the wall and would need to separate again, which just feels really messy to me. Better to set a body that will fit most common cases and adjust it manually if you drastically modify the sprite.

Link to comment
Share on other sites

  • 2 years later...
 Share

  • Recently Browsing   0 members

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