2D Toolkit Forum

2D Toolkit => Support => Topic started by: roid100 on February 26, 2013, 03:13:09 am

Title: How can I change the sprite? Help me plz
Post by: roid100 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.
Title: Re: How can I change the sprite? Help me plz
Post by: unikronsoftware 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" );

Title: Re: How can I change the sprite? Help me plz
Post by: roid100 on February 27, 2013, 05:22:25 am
I've just updated it. Thanks a lot!
Title: Re: How can I change the sprite? Help me plz
Post by: tzamora 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?
Title: Re: How can I change the sprite? Help me plz
Post by: unikronsoftware 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.
Title: Re: How can I change the sprite? Help me plz
Post by: hntoan 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 !
Title: Re: How can I change the sprite? Help me plz
Post by: unikronsoftware 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.