Hello Guest

Author Topic: Changing polygon collider when changing spriteID  (Read 6258 times)

Eldh

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Changing polygon collider when changing spriteID
« on: September 07, 2012, 11:40:03 am »
Hi,
So i instantiate a prefab, in this when i change the spriteID, randomly in my case, the polygon collider won't update. It stay the same since the instantiate.
Is there a way to update the collider when i change the sprite ?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing polygon collider when changing spriteID
« Reply #1 on: September 07, 2012, 06:41:46 pm »
It doesn't for a good reason, but it isn't convenient like this. I am aware of it and it is likely to change in a future update.

The easiest way around this is to create a new sprite using the sprite collection from the prefab - create a GameObject, and use tk2dSprite.AddComponent(...) to add a sprite to the gameObject. If you really need the prefab, destroy the attached collider before switching. I think that should sort it out. The reason it isn't doing this automatically is that this is a very costly process in terms of CPU usage, so its best not to do it that often.

Eldh

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing polygon collider when changing spriteID
« Reply #2 on: September 07, 2012, 07:58:35 pm »
Mhh i tried to use tk2dSprite.AddComponent(...) on an empty gameobject like you said but it does not create a collider at all...
I instantiate my prefab which contain several sprites, then i add the tk2dsprite component to the gameObject (in my prefab) cause only this need to change randomly, but i'm relatively a beginner so maybe i do something wrong ? all this happens on Start.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing polygon collider when changing spriteID
« Reply #3 on: September 08, 2012, 02:11:59 pm »
I'll look into this for you - I'm just recovering from a HD crash, so it'll likely be tomorrow before I get a chance to investigate this.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing polygon collider when changing spriteID
« Reply #4 on: September 09, 2012, 12:33:14 am »
Ahaa. I found the issue.

When creating a sprite using tk2dSprite.AddComponent(...), the collider isn't created. Change tk2dBaseSprite.CreateCollider like so -
Code: [Select]
public static T AddComponent<T>(GameObject go, tk2dSpriteCollectionData spriteCollection, int spriteId) where T : tk2dBaseSprite
{
T sprite = go.AddComponent<T>();
sprite._spriteId = -1;
sprite.collection = spriteCollection;
sprite.spriteId = spriteId;
sprite.Build();
return sprite;
}


Now it should work correctly.
« Last Edit: September 09, 2012, 12:35:43 am by unikron »

Eldh

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing polygon collider when changing spriteID
« Reply #5 on: September 17, 2012, 01:13:29 pm »
it's working, thx !