Hello Guest

Author Topic: Possible Bug  (Read 2083 times)

cloudcamaleoniv

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
Possible Bug
« on: May 24, 2013, 08:35:31 pm »
I've asked a friend to follow the same steps, and the bug still occurs.

1. Create a Sprite Collection, rename it to "SomeCollection" (doesn't matter)
2. Open it, drag 2 textures to it. Commit.
3. Create a tk2dSprite, select the collection "SomeCollection", select one of the sprites (does not matter which)
4. Rename the sprite "MySprite" (doesnt' matter)
5. Create a C# script and drag it to something on the scene (could be the camera).
6. On the 'Start' method of the script, write Debug.Log(GameObject.Find("MySprite").getComponent<tk2dSprite>().Collection.Count).
7. Test the game.

It should appear "2" on the Debug Log, 'cos the collection has 2 textures in it. Okay. Stop the game.

8. Open the "SomeCollection". Select one of the textures, do you see "Sprite ID" on the top? Well, select the texture with SpriteID "0". It must be the one with 0.
9. Delete the texture with SpriteID 0.
10. Test the game.

It should appear "1" on the Debug Log, after all, the Collection only have 1 Sprite, but it shows "2".

This is breaking the randomness of my random sprite generator.

Just want to expose it to see if it's a bug or not.

Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Possible Bug
« Reply #1 on: May 24, 2013, 08:53:26 pm »
Its not a "bug" exactly, but I understand the confusion it causes.
The sprite collection always keeps the same index into an array. That means that if you have 100 sprites, and delete the first 99 (by id), it will still have 100 elements in it. Its fine in almost all cases, as you don't care about the number of sprites in a collection otherwise.... except in this one case.

To work out the real number of sprites in a collection, do something like this.

Code: [Select]
               int count = 0;
foreach (var v in sprite.Collection.inst.spriteDefinitions)
{
if (v.Valid)
count++;
}

It also means you can't directly index into spriteDefinitions with the index (i.e. spriteId won't be the same as count), but you can work that out.
I'll make a note of this though, maybe make a note in the docs for sprite.Collection.Count;

cloudcamaleoniv

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Possible Bug
« Reply #2 on: May 24, 2013, 09:04:47 pm »
Oh, thanks. It really confused me the "Count" definition. Didn't know also that the spriteDefinitions worked that way. I can use that array to achieve the same random result now.

Thanks for your fast replies.