Hello Guest

Author Topic: Can I redefine SpriteCollection at runtime?  (Read 5924 times)

lofl

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Can I redefine SpriteCollection at runtime?
« on: June 12, 2013, 03:38:56 am »
Hi,

I am going to emigrate from another sprite system.

That code is creating all sprite at runtime.

I want to create sprite with another rect of the texture used by created SpriteCollection.

But, when creating the first sprite, the information on the sprite created later is unknown.

Can I add new rect to created SpriteCollection?



Thnaks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can I redefine SpriteCollection at runtime?
« Reply #1 on: June 12, 2013, 10:20:46 am »
You can to the sprite collection data object. Check tk2dRuntimeSpriteCollection.cs for how to create sprites from rects in a texture. You can use that to manipulate your runtime sprite collection in any way. Just remember - the sprite IDs should never change. So if you're adding one, increase the size of the array by all means, but when deleting the first out of the full array, don't shift everything up, just clear the first entry.

Tina

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Can I redefine SpriteCollection at runtime?
« Reply #2 on: June 12, 2013, 09:32:27 pm »
Hi,
I'm currently working in a 2d game in the newest version of the 2d toolkit and I would like to know how I create a sprite animator at runtime.  I’m developing a game where depending on a random number a different animation has to be displayed.
Currently I’m using this code:
public tk2dSpriteAnimator anim;
GameObject two = new GameObject ();
anim = two.AddComponent<tk2dSpriteAnimator>();
The problem with this code is that Unity shows this error:
Sprite not found attached to tk2dSpriteAnimator.
I will appreciate your help!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can I redefine SpriteCollection at runtime?
« Reply #3 on: June 12, 2013, 10:38:39 pm »
No need to post the same thing in 3 threads. Its been answered here:
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1871.0.html

lofl

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Can I redefine SpriteCollection at runtime?
« Reply #4 on: June 13, 2013, 07:07:10 am »
Thanks for reply.

I set tk2dSpriteCollectionData.spriteDefinitions to new tk2dSpriteDefinition[] referring to tk2dRuntimeSpriteCollection.cs.

But NullReferenceException occurred.

I think it will go if spriteNameLookupDict can be set to null, but it is private...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can I redefine SpriteCollection at runtime?
« Reply #5 on: June 13, 2013, 10:18:49 am »
Well you do have the source :)
Just reset it, and you should be good to go. Or don't use names, just use spriteIds and you won't need the dict at all.

lofl

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Can I redefine SpriteCollection at runtime?
« Reply #6 on: June 14, 2013, 08:56:11 am »
Thank you so much.
I'll try it.