Hello Guest

Author Topic: How to resize a sliced sprite from script  (Read 7683 times)

dmcdonough

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to resize a sliced sprite from script
« on: April 27, 2012, 10:01:45 pm »
Docs and reference suggest there's two ways -- changing tk2dSlicedSprite.dimensions and tk2dSlicedSprite.scale. When the sprite was authored, the editor had a field that read "Scale (Pixel Units)" with an X and Y box. I set the X to 14. When the game is running and a certain event happens, I want to expand that by 32. Here's what happens:

If I set the dimensions property to a new Vector2 with that value, nothing happens. The sprite doesn't change. I've tried calling Build() immediately afterwards -- nothing. I can see the value changing in the debugger, but the sprite in the game is not resizing.

If I try to set the scale property, as the inspector suggested might be correct, the scale is actually stored as a very small float: 0.3111 in this instance. So adding 32 to that is, obviously, wrong -- it ends up being 1400+ pixels wide.

The question is: what gives? How are you supposed to do this?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to resize a sliced sprite from script
« Reply #1 on: April 27, 2012, 11:43:31 pm »
What version are you running? I'll look into it for you, and try to get it fixed for 1.70 final.

nikolic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: How to resize a sliced sprite from script
« Reply #2 on: April 28, 2012, 08:09:48 am »
If I set the dimensions property to a new Vector2 with that value, nothing happens. The sprite doesn't change. I've tried calling Build() immediately afterwards -- nothing. I can see the value changing in the debugger, but the sprite in the game is not resizing.

I change sliced sprite size by changing dimensions property in scripts and it works without calling Build() or any additional methods. And I use this feature extensively.

I have 2D Toolkit Version 1.7 beta 2.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to resize a sliced sprite from script
« Reply #3 on: April 28, 2012, 09:39:33 am »
Docs and reference suggest there's two ways -- changing tk2dSlicedSprite.dimensions and tk2dSlicedSprite.scale. When the sprite was authored, the editor had a field that read "Scale (Pixel Units)" with an X and Y box. I set the X to 14. When the game is running and a certain event happens, I want to expand that by 32. Here's what happens:

If I set the dimensions property to a new Vector2 with that value, nothing happens. The sprite doesn't change. I've tried calling Build() immediately afterwards -- nothing. I can see the value changing in the debugger, but the sprite in the game is not resizing.

If I try to set the scale property, as the inspector suggested might be correct, the scale is actually stored as a very small float: 0.3111 in this instance. So adding 32 to that is, obviously, wrong -- it ends up being 1400+ pixels wide.

The question is: what gives? How are you supposed to do this?

2 things - firstly, your sprite is most likely using the "old" method. Recreating it will make it use the new method, which is definitely the preferred method to adjust these values.

You're probably seeing these small numbers because you're not using the tk2dCamera. The tk2dCamera works in pixel units, and each world unit = 1 pixel.

So what you need to do to get these numbers into pixel units and vice versa, is to scale them by the texel size.

Vector3 scale = slicedSprite.GetCurrentSpriteDef().texelSize; // this is the size of one texel
So basically construct your dimensions of a scale of this. The dimensions are in world units, so ...

dimensions = new Vector3(numberOfPixels / scale.x, numberOfpixelsHeight / scale.y, 1);

Hope that helps

dmcdonough

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to resize a sliced sprite from script
« Reply #4 on: April 30, 2012, 01:39:35 pm »
Just checked, we're still using 1.57 + Patch 1.

For now I'll use the suggestion of multiplying by the texel size, and wait for the 1.7 final to come out.

Thanks!