Changing the sprite
by Shiraz · in Torque X 2D · 02/14/2007 (1:59 pm) · 12 replies
How do I change the sprite for my object once it's been assigned?
T2DStaticSprite enemy = (T2DStaticSprite)(TorqueObjectDatabase.Instance.FindObject("enemyTmpl") as T2DStaticSprite).Clone();
TorqueObjectDatabase.Instance.Register(enemy);
and now I want to use the "angryEnemyTmpl".
T2DStaticSprite enemy = (T2DStaticSprite)(TorqueObjectDatabase.Instance.FindObject("enemyTmpl") as T2DStaticSprite).Clone();
TorqueObjectDatabase.Instance.Register(enemy);
and now I want to use the "angryEnemyTmpl".
About the author
#2
02/15/2007 (5:15 pm)
K, thanks. I got it to switch for a T2DStaticSprite. Now if I've declared the object as a T2DSceneObject can I still change the material? I don't get that option with that type.
#3
02/15/2007 (5:36 pm)
If you've declared it as a SceneObject you can just cast it back down to a StaticSprite. If you've actually constructed it as a SceneObject then it has no material.
#4
So I've got the player declared are a T2DSceneObject with a T2DAnimatedSprite assigned to it initally which is the player running north.
T2DSceneObject _player;
_player = (T2DSceneObject) (TorqueObjectDatabase.Instance.FindObject("PlayerNorth") as T2DAnimatedSprite).Clone();
That works great.
I have animations for South, East and West which I want swapped out when the player moves in those directions. Should the player actually be declared as a T2DAnimatedSprite for this to work?
Either way, how do I swap the animation is it
_player.AnimationData = TorqueObjectDatabase.Instance.FindObject("PlayerSouth") as AnimationData.
because when I tried this code the program compiles and runs but doesn't change the animation.
02/15/2007 (9:54 pm)
So you can't switch the materials on SceneObjects? But aren't they the main objects? In the examples the player and enemies seem to always be declared as SceneObjects. Of course there are no examples when the material changes.So I've got the player declared are a T2DSceneObject with a T2DAnimatedSprite assigned to it initally which is the player running north.
T2DSceneObject _player;
_player = (T2DSceneObject) (TorqueObjectDatabase.Instance.FindObject("PlayerNorth") as T2DAnimatedSprite).Clone();
That works great.
I have animations for South, East and West which I want swapped out when the player moves in those directions. Should the player actually be declared as a T2DAnimatedSprite for this to work?
Either way, how do I swap the animation is it
_player.AnimationData = TorqueObjectDatabase.Instance.FindObject("PlayerSouth") as AnimationData.
because when I tried this code the program compiles and runs but doesn't change the animation.
#5
02/16/2007 (2:20 am)
You can't swap materials on an object of type SceneObject because they don't have any graphical representation on the screen. If you are storing a derived type of SceneObject in a reference of type SceneObject then you need only to cast the SceneObject down to your derived type and change the material on that.
#6
T2DSceneObject _player;
_player = (T2DSceneObject) (TorqueObjectDatabase.Instance.FindObject("PlayerSprite") as T2DAnimatedSprite).Clone();
Isn't the sprite being casted as a Scene Object to be assigned to the player? Why isn't the player declared as a sprite?
Also, what would be the best way to declare a player object if I am going to apply different animations to it depending on direction?
02/19/2007 (8:56 pm)
Ok, I can buy that, but then why in the examples do I see...T2DSceneObject _player;
_player = (T2DSceneObject) (TorqueObjectDatabase.Instance.FindObject("PlayerSprite") as T2DAnimatedSprite).Clone();
Isn't the sprite being casted as a Scene Object to be assigned to the player? Why isn't the player declared as a sprite?
Also, what would be the best way to declare a player object if I am going to apply different animations to it depending on direction?
#7
As far as the best way to delcare a player object? Who knows, I can't presume to be an expert on your game architecture. It depends a lot on the specific implementation details on what you are doing and where you're talking about declaring it. I'm also assuming that by "declare" you mean declaring a local reference to a player object that was already declared and constructed elsewhere. If you are using an animated sprite then just declare it as an animated sprite if you are going to have to do a lot of animation stuff on it. The only rule in programming is make the program work for you, not the other way around. If a reference to T2DSceneObject doesn't do it for ya, don't use it! :)
02/20/2007 (12:31 am)
In general with OOP you want to treat an object as the most generalized type which suits your purposes. The examples you are looking at must assume T2DSceneObject is the most general type which suits their purposes. If T2DStaticSprite is the most general type that suits your purposes, you could store it as that.As far as the best way to delcare a player object? Who knows, I can't presume to be an expert on your game architecture. It depends a lot on the specific implementation details on what you are doing and where you're talking about declaring it. I'm also assuming that by "declare" you mean declaring a local reference to a player object that was already declared and constructed elsewhere. If you are using an animated sprite then just declare it as an animated sprite if you are going to have to do a lot of animation stuff on it. The only rule in programming is make the program work for you, not the other way around. If a reference to T2DSceneObject doesn't do it for ya, don't use it! :)
#8
T2DSceneObject _player;
_player = (T2DSceneObject) (TorqueObjectDatabase.Instance.FindObject("PlayerNorth") as T2DAnimatedSprite).Clone();
as I've seen in the examples. I've got the player running around fine. Got the enemies going fine and am happy with the functionality. Now in my PlayerControlComponent.cs, I was to just add the functionality to change the animated sprite depending on the direction. I have no idea how to do this. I tried assigning a new material and that compiled but crashed on runtime.
_player.Material = (RenderMaterial) TorqueObjectDatabase.Instance.FindObject("PlayerWest");
I tried assigning a new animation which compiled but had no effect at runtime.
_player.AnimationData = (AnimationData) TorqueObjectDatabase.Instance.FindObject("PlayerWest");
I don't think what I'm trying to do should be hard. I just don't know the syntax. My original statments creating the player may not apply for what I'm trying to do but I don't know. I just need an example of how this can be set up. Any example. I need syntax! Please!
02/20/2007 (1:02 pm)
Ok, I've declared the player in my game.cs usingT2DSceneObject _player;
_player = (T2DSceneObject) (TorqueObjectDatabase.Instance.FindObject("PlayerNorth") as T2DAnimatedSprite).Clone();
as I've seen in the examples. I've got the player running around fine. Got the enemies going fine and am happy with the functionality. Now in my PlayerControlComponent.cs, I was to just add the functionality to change the animated sprite depending on the direction. I have no idea how to do this. I tried assigning a new material and that compiled but crashed on runtime.
_player.Material = (RenderMaterial) TorqueObjectDatabase.Instance.FindObject("PlayerWest");
I tried assigning a new animation which compiled but had no effect at runtime.
_player.AnimationData = (AnimationData) TorqueObjectDatabase.Instance.FindObject("PlayerWest");
I don't think what I'm trying to do should be hard. I just don't know the syntax. My original statments creating the player may not apply for what I'm trying to do but I don't know. I just need an example of how this can be set up. Any example. I need syntax! Please!
#9
02/20/2007 (3:22 pm)
Do you have the Space Shooter example? That has some nice animation switching going on in it.
#10
(_player as T2DAnimatedSprite).Material = spriteHoldingTheAnimation.Material;
02/20/2007 (7:50 pm)
Ok found it there. Thanks. Turns out the code I was looking for was(_player as T2DAnimatedSprite).Material = spriteHoldingTheAnimation.Material;
#11
02/20/2007 (11:41 pm)
Well right on brutha!
#12
Edit: ok so after screwing around with my code for hours I found that doing (_player1 as T2DAnimatedSpite).Material = spriteholdinganimation.Material; is not correct.
The original assignments are correct but to change the animation of your character I found I needed these two lines of code instead:
(_player1 as T2DAnimatedSpite).AnimationData = spriteholdinganimation.AnimationData;
(_player1 as T2DAnimatedSpite).PlayAnimation();
If an expert could comment on why this is I would love to know. I am new to TGBX and while I absolutely love the tool there is a learning curve with the coding which I am slowly overcoming :)
05/30/2007 (10:25 pm)
I am using this exact some method, but for some reason when I switch the material the new animation begins scrolling vertically and is not behaving as it should. Are there some other settings that need to be configured before the material can be changed?Edit: ok so after screwing around with my code for hours I found that doing (_player1 as T2DAnimatedSpite).Material = spriteholdinganimation.Material; is not correct.
The original assignments are correct but to change the animation of your character I found I needed these two lines of code instead:
(_player1 as T2DAnimatedSpite).AnimationData = spriteholdinganimation.AnimationData;
(_player1 as T2DAnimatedSpite).PlayAnimation();
If an expert could comment on why this is I would love to know. I am new to TGBX and while I absolutely love the tool there is a learning curve with the coding which I am slowly overcoming :)
Torque Owner Thomas Buscaglia
You can get the materials from the TorqueObjectDatabase the same way you got the sprite.