2D Toolkit Forum
2D Toolkit => Support => Topic started by: DogsLoveChocolate on December 04, 2013, 05:44:46 pm
-
Hello,
I am trying to figure out a way to horizontally flip my animated sprite on a key input when it changes in the x-direction. I am using the TutorialAnimController tutorial I found HERE (http://www.unikronsoftware.com/2dtoolkit/doc/2.10/tutorial/scripting_a_sprite_animator.html).
So for example when the user presses the A key (going left) I want the sprite to mirror and face the direction the sprite is moving in. I'm guessing it would be somewhere in this if statement (C#):
if (Input.GetKeyDown(KeyCode.A)) {
if (!anim.IsPlaying("walk")) {
anim.Play("walk");
anim.AnimationCompleted = null;
walking = true;
}
}
If anyone knows a way to flip the sprite horizontally in the code that would be greatly appreciated.
-
I know that if you set the x scale to -1 it will flip the sprite. I am not sure if there is a better way to do this though.
Edit: Alternatively there are FlipX and FlipY functions on the sprite, but I am unsure if they work with animations. Hope one of these helps.
-
Thank you. That definitely helps, but I'm not exactly sure how to actually script and tell my sprite to scale it's x value. I'm a novice when it comes to programming...
-
If anim is a tk2dSpriteAnimator you can use anim.Sprite to get the sprite object.
-
The console said that "Type `tk2dBaseSprite' does not contain a definition for `Transform' and no extension method `Transform' of type `tk2dBaseSprite' could be found". Does 'Transform' not work when using 2dtk sprites?
-
Can you post the code that you are using to set the scale? the sprite should have a transform that you can access via .transform.
-
From your error message, it looks like you are using a capital 'T' instead of lowercase to reference the 'transform' property. If that is the case, switch it to a lower case 't'.