Hello Guest

Author Topic: Best way to retrieve 'native' size of a sprite prior to instantiating one?  (Read 4327 times)

CBridgman

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hi,

I want to retrieve the 'native/standard' size of a sprite prior to instantiating one. This would be fairly simple to do except that I'm also using multiple platforms on the sprite collection. Running spriteCollectionData.GetSpriteDefinition() results in an exception because the spriteDefinition array has no elements. (Is that correct, or a bug?) Instead, one has to go spriteCollectionData.inst.GetSpriteDefinition(), which works, but will return a different definition based on which platform is currently active. (I.e. At 2x I'd get say, 40x40, and at 4x I'd get 80x80)

In terms of setting positions and sizes of sprites, I'm working at 1024x768, which corresponds with the '2x' sprite collection. So, when I want to retrieve a sprite's width, I always want the 2x measurement, effectively allowing me to ignore the 4x platform's existence.

I've written the following code which works around this issue, but am concerned about it being unsupported:

Code: [Select]
      tk2dSpriteCollectionData spriteCollectionDataInst = this.spriteGroup.SpriteCollectionData.inst;
      tk2dSpriteDefinition definition = spriteCollectionDataInst.GetSpriteDefinition(this.name);
      Bounds bounds = definition.GetBounds();
      size = new IntVector2(
         Mathf.RoundToInt(bounds.size.x * spriteCollectionDataInst.halfTargetHeight),
         Mathf.RoundToInt(bounds.size.y * spriteCollectionDataInst.halfTargetHeight)
      );

As you can see, I access the .inst and retrieve the definition for the current platform, then multiply the bounds by the current platform's halfTargetHeight to get the 'native' size.

Is there a better way to achieve this, or is the above an acceptable way of doing it?

CBridgman

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Best way to retrieve 'native' size of a sprite prior to instantiating one?
« Reply #1 on: December 28, 2013, 07:09:34 pm »
The code I pasted above had a number of issues. It seems that the bounds don't change per platform size (and using halfTargetHeight was just totally wrong), so it can be simplified to this:

Code: [Select]
      tk2dSpriteCollectionData spriteCollectionDataInst = this.spriteGroup.SpriteCollectionData.inst;
      tk2dSpriteDefinition definition = spriteCollectionDataInst.GetSpriteDefinition(this.name);
      Bounds bounds = definition.GetBounds();
      size = new IntVector2(
         Mathf.RoundToInt(bounds.size.x),
         Mathf.RoundToInt(bounds.size.y)
      );

So essentially the only difference is using the .inst property.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Best way to retrieve 'native' size of a sprite prior to instantiating one?
« Reply #2 on: December 28, 2013, 08:24:20 pm »
Thats the correct way to go about it - bounds will be the same / close enough as long as your source images are the same size.

The inst property is necessary when dealing with platform collections, as the collection itself will not exist / be loaded until .inst is called for the first time on it.

CBridgman

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Best way to retrieve 'native' size of a sprite prior to instantiating one?
« Reply #3 on: December 29, 2013, 04:10:49 pm »
Thanks for the reply.

I'm not sure I understand this though: "bounds will be the same / close enough as long as your source images are the same size", as what I have here doesn't agree with that statement. The two source images aren't the same size.

I'm using the 2x and 4x platforms, with 2x as the default.
  grid-block-small@2x is 80x80 pixels
  grid-block-small@4x is 160x160 pixels

Irrespective of which platform is loaded in the inst property, I get a bounds of 80x80 for sprite 'grid-block-small'. That is the desired behavior for what I'm doing, but is that correct?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Best way to retrieve 'native' size of a sprite prior to instantiating one?
« Reply #4 on: December 30, 2013, 11:38:25 am »
Think of platform sprites as changing the amount of detail in your sprite vs changing the size. The bounds will always be the same on all different platforms, as will the physical size.