It looks like you're new here. If you want to get involved, click one of these buttons!
All, does anyone have a good example/tutorial of using tweens to animate a series of sprites. Like a walking man across the screen. Just can not seem to get my head round them and they seem to be very useful.
Comments
@Bri_G Here’s a simple tween. You create a variable with the starting position, then you set up the tween with the tween time, the start variable, the end position, and the tween info of how you want the tween to move. There’s also a function you can execute when the tween completes it move.
@dave1707 - thanks for the example and notes, noe beginning to understand - the focus is on the location of the sprite. With animation in mind I am looking at several spritesused in sequence.
Taking your example I managed to achieve this by using a table containing sprite data - but I had to use counters (my own timer) to switch sprite. Here is my main code, builder() is a routine for loading a sprite sheet into an image then stripping out the sprites into a table adv[].
Thanks again.
@Bri_G You can add a counter within the tween code. That way as the tween runs, the counter will increment. Here’s the above code with count added. You set a starting and ending value to count and it will increment for the time of the tween.
@dave1707 - interesting, but your code doesn’t actually cycle through the sprite sheet - you have only used one sprite. Using an array with adv[count] as the array and count also doesn’t cycle through the sprites.
Count isn’t a counter it’s a decimal timer so you need to floor it count//1. But that doesn’t work either until I realised counter wasn’t addressed properly and what you need is sp.counter//1.
Bingo moving animated sprites.
Thanks for your help.
@Bri_G How about defining a callback function that increments the counter? Or is it that the sprites continually change during the motion?
@dave1707, @LoopSpace - update with the code, I managed to get an example written where you load up the image, set up arrays, extract the sprites to an array and set up the Tween.
Note image details given in code by !!! I had to load the image into Gimp, export the image so that the image was not compressed (should be 128x128 then) and rename so that the file type is lower case ie .png not .PNG. Then the code runs OK.
Try switching sp.count//1 to sp.count or even count!!!
@LoopSpace it’s meant to be an animation, the above shows running on the spot but if you change the start and finish position you should see the caveman run across the screen. There are 16 sprites in the image covering the animation itself so they cycle forever, kinda like Mo Farrah !! As long as the cycle runs.
I think that Tween.sequence may be the proper way of doing this, but I am still learning.
Tried to include the amended image, hope I’ve added the correct one.
@Bri_G Here’s your above code modified so the caveman runs across the screen from right to left. When I loaded your img0273.png, it ended up being 64x64 pixels, so I had to change some of your values. Since I don’t have hard coded values, it might work OK with a size of 128x128.
@dave1707 - thanks for that, made a few mods cm.width and cm.height give the size decimal notation, spriteSize() returns the integer. As printed above my caveman made it part way across before crashing with an error. Code below goes right across the screen.
Must be a relationship between image cycles and time.
@Bri_G Not sure why mine didn’t work. Even if your image was 128x128, the divide should have resulted in an integer. When I run yours, the moving feet don’t match the movement across the screen. The image moves too fast across the screen. The sp.count should be a higher number so you go thru more table iterations as the image moves. That’s why I was using a sp.count of 128 and doing a % divide. I was looping thru the image table 8 times to cross the screen.
@Bri_G I just figured out the problem. The % divide should be % 16 + 1 instead of % tsz + 1. Your table has 16 entries and the value of tsz using 128x128 would be 32, that’s why it crashed. It was indexing past the end of the table.
@dave1707 - well spotted. Must admit I didn’t do the math. Changed to your suggestion (except I used 64 instead of 50 for sprite size) and the caveman jogs along great. Thanks again, eventually I’ll get to know all about this app!!!!
@Bri_G Glad everything worked OK. The more you play around with Tweens, the more comfortable you’ll get using them. But then that works for everything. The more you do something, the better you get at it.
Getting into Tweens now, does anyone know if you can move Tweens in Parallel (same time slot), as I’d like to animate a sprite against a scrolling background - so action sprite appears to move against a moving background?
@Bri_G Not sure if this is what you’re after. You can create a table of Tweens and set each one at a different position and time.
But, eye opener - shows my ignorance with Lua - what is subject? Don't answer figured it out. How generic can you get??!!!!
@Bri_G
subject
is not pure Lua, it is really private code within the tween library. Without Simeon or John's say-so, it should be regarded as not safe to use as the internals of a library can change. They could change the variable name to something else without changing the functionality.Without having tested it, here's what I would do:
(Note that I've switched to
ipairs
instead ofpairs
since the table is enumerated. Also, despite despairing of @dave1707 ever usinglocal
, I've used it so that I avoid polluting the global namespace.)@LoopSpace I very seldom use locals in my examples. I try to make my examples small, mostly to show how to do something. I also don’t spend a lot of time writing them. I hope if someone uses my code, they add locals where or if needed. Since I don’t write anything that goes beyond this forum, the only time I use locals is when I want the most speed out of the code.