Hello Guest

Author Topic: Flipping an Animated Sprite  (Read 6141 times)

DogsLoveChocolate

  • Newbie
  • *
  • Posts: 3
    • View Profile
Flipping an Animated Sprite
« 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

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#):
Code: [Select]

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.

Dreamkind

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Flipping an Animated Sprite
« Reply #1 on: December 04, 2013, 05:51:13 pm »
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.
« Last Edit: December 04, 2013, 05:54:44 pm by Dreamkind »

DogsLoveChocolate

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Flipping an Animated Sprite
« Reply #2 on: December 04, 2013, 06:04:07 pm »
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...

Dreamkind

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Flipping an Animated Sprite
« Reply #3 on: December 04, 2013, 06:07:41 pm »
If anim is a tk2dSpriteAnimator you can use anim.Sprite to get the sprite object.

DogsLoveChocolate

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Flipping an Animated Sprite
« Reply #4 on: December 04, 2013, 06:22:32 pm »
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?

Dreamkind

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Flipping an Animated Sprite
« Reply #5 on: December 04, 2013, 06:43:39 pm »
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.

stacktrace

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Flipping an Animated Sprite
« Reply #6 on: December 04, 2013, 08:06:17 pm »
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'.