Game Development Community

Changing T2DStaticSprite Material on an object

by George Gibson · in Torque X 2D · 09/24/2008 (12:43 pm) · 3 replies

Would someone know if there's a quick way (<-- I assume, here) that one could change out the material that an object uses? I'd like to accomplish this via an OnCollision delegate, but by default the method passes a T2DSceneObject (ourObject and theirObject). I'd like to change ourObject's material if it collides with a specific object type, but I'm once more at a loss.

Yet again, thanks for all the help,
George

#1
09/25/2008 (3:12 am)
Why are you lost? Maybe I am getting something wrong but it should work (what followes is most likely not working code but should give you an idea:

SimpleMaterial m = TorqueObjectDatabase.Instance.FindObject("MaterialToChangeTo") as SimpleMaterial;

ourObject.Material = m;

C.
#2
09/25/2008 (3:49 am)
I apologize, I should've been more specific. "ourObject" is passed to the OnCollision event as a T2DSceneObject, not a T2DStaticSprite, which means that there is no .Material available. It's possible for me to cast it to a T2DStaticSprite, but as bad programming examples go, that doesn't work (at least, not the way I've done it, apparently).

I was thinking that there was some way I might be able to create a reference to the object, but to be honest, I don't entirely understand references yet (I'm getting there).

Thanks,
George
#3
09/25/2008 (4:13 am)
I do not see the problem in casting it to T2DStaticSprite - as long as you know that it is indeed one. You can test that:

if(ourObject is T2DStaticSprite)
{
   T2DStaticSprite ourObjectAsSprite = ourObject as T2DStaticSprite;
   ourObjectAsSprite.Material = ...
}