Hello Guest

Author Topic: How can I change the sprite? Help me plz  (Read 6485 times)

roid100

  • Newbie
  • *
  • Posts: 6
    • View Profile
How can I change the sprite? Help me plz
« on: February 26, 2013, 03:13:09 am »
Hello,
I am almost the beginner.
I searched and use this way to change the sprite image.

tk2dSprite img =  GetComponent<tk2dSprite>();
img.collection = (tk2dSpriteCollectionData) Resources.Load ("World1", typeof(tk2dSpriteCollectionData));
img.SwitchCollectionAndSprite(img.collection, img.GetSpriteIdByName("test"));

When I tested it,
the names of collection and sprite in the Inspector were changed.
But not changed in the game view ( also scene view ).

What is the problem? Please help me.
Many Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How can I change the sprite? Help me plz
« Reply #1 on: February 26, 2013, 11:23:04 am »
Get 1.91, the API is really much nicer to use in there.

tk2dSprite img = GetComponent<tk2dSprite>();
tk2dSpriteCollectionData spriteCollection = (tk2dSpriteCollectionData) Resources.Load ("World1", typeof(tk2dSpriteCollectionData));
img.SetSprite (  spriteCollection, "test" );


roid100

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How can I change the sprite? Help me plz
« Reply #2 on: February 27, 2013, 05:22:25 am »
I've just updated it. Thanks a lot!

tzamora

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How can I change the sprite? Help me plz
« Reply #3 on: March 01, 2013, 12:29:43 am »
tk2dSprite img = GetComponent<tk2dSprite>();
tk2dSpriteCollectionData spriteCollection = (tk2dSpriteCollectionData) Resources.Load ("World1", typeof(tk2dSpriteCollectionData));
img.SetSprite (  spriteCollection, "test" );

Just to confirm, in this example the "World1" name referes to a sprite collection? Does this code works to change the sprite collection of a sprite at runtime?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How can I change the sprite? Help me plz
« Reply #4 on: March 01, 2013, 12:43:17 am »
"World1" is the name of the sprite collection data object. Yup, you can use this to change collections at runtime.

hntoan

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: How can I change the sprite? Help me plz
« Reply #5 on: March 02, 2013, 02:01:07 am »
Hello unikron, another problem arises when I tried to change the sprite: the collider doesn't change to the new one.
Could you please help me with that ?
Thanks very much !

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How can I change the sprite? Help me plz
« Reply #6 on: March 02, 2013, 03:09:43 pm »
The collider won't change if it is a mesh collider. This is because mesh colliders are really expensive to change at runtime. If you know what sprites you'll be changing to, it might be better to create disabled copies of them, and enable when you need to.

Also don't forget, if you want to move a collider you should always have a kinematic rigid body.