Android Question Flip sprite animation in libgdx?

Malky

Active Member
Licensed User
Longtime User
Is there a simple way to flip the animation sequence so that I can achieve a walking animation in libgdx?

I have the following which works walking right, but if I need to get the same effect when walking left?
B4X:
Batch.DrawRegion3(pandaFrames(0,(Frames/8) Mod 14),mypanda.x,mypanda.y,ScreenW*0.1,ScreenH*1,ScreenW*0.08,ScreenH*0.2,1,1,pandaAngle)
Flipping the sprite animation in an art package and using it only makes it animate backwards as expected.

Malky
 

Malky

Active Member
Licensed User
Longtime User
Found a split function available on the texture region, but it doesn't do anything?

B4X:
    pandaTextureLeft.Initialize("panda.png")
    pandaRegionLeft.InitializeWithTexture(pandaTextureLeft)
    pandaRegionLeft.Flip(True,False)
    Dim pandaFramesL(,) As lgTextureRegion = pandaRegionLeft.Split(75, 100) ' split buffer into animation frames

Am I missing something here?

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Yes, but that means stepping through the animation frame by frame, flipping?

In Gameview, I believe there was a function available to just flip the animation to reverse it which worked well?

EDIT - Incidentally, what does the posted flip function do then ?

Malky
 
Upvote 0

strat

Active Member
Licensed User
Longtime User
Sorry, because of my english is not perfect, I didn't understand what you want to do.

If you have animation frames that walking right and you want to make frames walking left, the example I mentioned above helps you. There is walkanim.png in the Files folder. There are only four frames walking left but the cavemen can walk right in the example.

If you want backward animation frames, you should decrease frame variable.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
I've tried this numerous times and it doesn't matter what frame appears, it is facing right!

Does this flip function work?

Very frustrating, particularly after a power outage!

Malky
 
Upvote 0

andymc

Well-Known Member
Licensed User
Longtime User
If you want to draw the character walking the other direction, then you can draw the region with a negative X size.

for example, a line from my invaders game:

Batch.DrawRegion2(img_rArrowFrames(0,1),vpW*0.7,vpH*0.05,-vpW*0.20,vpH*0.10)
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hey!!!! Who is this man?

Working now. Thank you SO much, after a very frustrating day I have a smile on my face :)

Jeez!

Malky
 
Upvote 0

andymc

Well-Known Member
Licensed User
Longtime User
No problem, I went crazy trying to write my game! I nearly have it complete now, and then I have a good framework for further games.

Good luck. I may not answer again for a few weeks. My wife is due to give birth on Sunday!
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hey, congratulations. I trust everything will go well.

Would be interested to see your framework as they seem scarce and all ideas are good to look at.

Will this be two weeks off drinking? :)

I have two myself, one of each, but grown up now.

My best to you and your wife and soon to be! One of the best times of your life!

Malky
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Reminder about texture regions: the regions do not change physically the image (thus they do not use extra memory, do not need time to process the image and it's one of their main interest). So many settings do not affect the image until it is drawn. Flipping the image, for example, does not flip the texture backing up the region. It just flips the drawing on screen.
Same comments for sprites.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Informatix, thanks for the reply, but I am trying to flip an animated strip with 15 sprites, not just one sprite texture/image.

This doesn't appear to work?

Malky
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Hi Informatix, thanks for the reply, but I am trying to flip an animated strip with 15 sprites, not just one sprite texture/image.

This doesn't appear to work?

Malky
The explanation is just above your post. If you flip a region, it will be flipped only when drawn. So flipping a region and splitting regions from it do no produce flipped images. You have to flip each resulting frame.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Informatix, thanks for that. I thought the flip function did the work for you?

So I have to set up a for/next loop with a negative step to go through the lgTextureRegion Array array flipping each one at a time?

I'll have a few of these animations, so it might be better if I create a small function for this?

So many choices with libGDX, it can be confusing, use actors, sprites etc.

Cheers,

Malky
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I'll have a few of these animations, so it might be better if I create a small function for this?

You can create a loop after splitting to call Flip(True, False) for each frame or just add the minus sign while you draw your frames (which is slightly faster) as suggested by andymc. Doing a function for that is up to you.

So many choices with libGDX, it can be confusing, use actors, sprites etc.
A texture is the basis of everything else and it is rarely used as is. If you don't use Scene2D, you will draw texture regions or sprites. You should choose sprites if you need rotation, scaling, change of colors, etc. (advanced features). If you use Scene2D, the images will be drawn with lgScn2DImage. In the end, there's not a lot of choice.
What may be confusing is the word "sprite" that can refer to the lgSprite class or simply to an animated object on screen.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
So (in your best judgement), what would you suggest would be best solution for an animated game, (walking/jumping with collision detection) would be best for a standard platform game with gravity and ground/landing detection?

(I did get the flip stuff to work, but having to make positional variations with the reverse sprites/textures).

I'm just just trying to piece things together as per your tutorial on creating a game and see what's possible?

Malky
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
So (in your best judgement), what would you suggest would be best solution for an animated game, (walking/jumping with collision detection) would be best for a standard platform game with gravity and ground/landing detection?

(I did get the flip stuff to work, but having to make positional variations with the reverse sprites/textures).

I'm just just trying to piece things together as per your tutorial on creating a game and see what's possible?

Malky
IMHO, every thing to display (UI, sprites, background) should be drawn and managed with the Scene2D classes (except if you need to use the Polygon sprite batcher for maximum performance or decal objects for an isometric game). I'm using the Scene2D widgets a lot currently for my own game and they save me a lot of time and work. Especially for handling events, scaling textures and doing animations. You can create complex sequence of animations with just a few lines. To handle collisions and gravity (for realistic jumps for example), use the lgBox2D classes. As everything new, you will lose some time to learn to use these new classes, but believe me, it's worth the time spent.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
IMHO, every thing to display (UI, sprites, background) should be drawn and managed with the Scene2D classes (except if you need to use the Polygon sprite batcher for maximum performance or decal objects for an isometric game). I'm using the Scene2D widgets a lot currently for my own game and they save me a lot of time and work. Especially for handling events, scaling textures and doing animations. You can create complex sequence of animations with just a few lines. To handle collisions and gravity (for realistic jumps for example), use the lgBox2D classes. As everything new, you will lose some time to learn to use these new classes, but believe me, it's worth the time spent.
Hi Informatix, thanks for that.
I wouldn't ask someone I didn't respect for advice if I wasn't going to take it, so will give it a try and have a serious look to see what options are available?
Looking through the examples, I still can't find an animated moving sprite example with Scene2D though which worries me? I can handle jumps etc without the gravity etc using code, so the animation is the most important part of what I want to do.
Also, is there a definitive list of the available classes anywhere?

I know you have your own stuff to do and are very busy, so sorry to trouble you again, but one kick in the right direction could help immensely. Please feel free to offer the kick in any direction you want? :) I'll understand.

Cheers,

Malky
 
Upvote 0
Top