An alternative to using the animate behavior

Working with a pretty heavy game which can sometimes slow down FPS considerably, I've noticed that the animate behavior is not so reliable in some cases. In cases when many things are happening at the same time during the game, animations made with the animate behavior can many times skip frames or sometimes stop altogether at a frame even if it is not the last frame. This makes the animations look bulky and can cause trouble if you have rules which fire when actors have specific images. For this reason I would like to share an alternative to animate behaviors that I discovered recently (some of you may know this trick already, but just in case I will share for those who may struggle with the same problem with animations). Keep in mind this tip will only help with games in which many events happen at the same time and thus consume a lot of RAM and slow down FPS, for simple games in which not too many things happen at the same time, it probably will not make a difference. So here it is:

First thing first, you will need all of your images (frames) in the animation to have the same name, followed by a number which represents its chronological order (so lets say you have an animation of an explosion, you may want to name each frame "explosion1.png", "explosion2.png", "explosion3.png" and so on, depending on what order they come. If you have more than nine frames, you will have to account for this by adding an extra zero after the name of each image, for example "explosion01.png", explosion02.png" and so on. If you are drawing and creating your own artwork in an engine like Adobe Flash, Blender or some other program, when you export your PNG sequence Adobe or whatever you are using should already do this for you. Normally, Adobe or whatever graphic creator you are using will also add several extra zeros in case you have several frames.

Next, after importing those images to gamesalad, what you will do is create a loop (with the loop behavior or a timer, whichever one you want, I will say that using a timer will give you more control over the speed at which the animation moves). If you use a timer, put the timer inside an attribute rule, if you are using a loop, simply put the attribute rule as the condition for the loop and set the condition to "While". You will need a self integer attribute set to 1 which I normally name "count". In the attribute rule, look at the name of the last image of your animation, and use the following rule:
If count <= (However many frames your animation has)
Next, inside the loop place a change attribute behavior that changes self.image to the following:
change self.image to "name of your image"..self.count
You HAVE to enter it exactly as it is, including the "" marks and the two dots between the " mark and your count attribute. Now, if you have more than nine frames, you HAVE to account for the change in digits from 09 to 10. This means you will need two different loops, one for when the name of the actor's image is "NameOfImage0"..self.count and one for when it is "NameOfImage"..self.count. If you have even more zeros in the names of your images, which if you exported your images with a graphic creator then you most likely will, then your change attribute behaviors will instead say "NameOfImage000"..self.count, if for example you have an image named NameOfImage0001 and your animation goes all the way to NameOfImage0009. Keep in mind you DO NOT include the format of the image, gamesalad does this for you, so DO NOT add .png or .jpg at the end of the image name.

Then, place a change attribute that adds one to your count attribute, so:
change self.count to self.count+1

If you are confused, here is an example what your finished product should look like if you have 26 frames with the names "explosion0001.png", "explosion0002.png", and so on all the way to "explosion0026.png":

Rule
If count <= 26
[
Loop
While self.count <= 9
{
Change attribute
(
self.image to "explosion000"..self.count
)
Change attribute
(
count to count+1
)
}
Loop
While self.count > 9
{
Change attribute
(
self.image to "explosion00"..self.count
)
Change attribute
(
count to count+1
)
}
]

For the sake of simplicity I used loop behaviors in my example, but you can go ahead and use timers to create loops as well which will give you more control over how fast your animation runs. Loop behaviors will cause your animation to run very fast, faster than 30 fps, so you may want to use timers instead of loop behaviors to slow down the animation. If you want your animation to run at 30 fps, simply divide 1 by 30, which should give you about 0.0333 seconds, so you would set your timer to every 0.0333 seconds. If your game is running on mobile and you have a lot of events happening at once which is causing your game to run slow, you may want to go ahead and use a loop behavior instead of a timer. You may just have to try both loop behaviors and timers and see which one runs best for you. You can also change it up so that it repeats when the animation is over if you want the animation to repeat every time or change it so that it goes back to the original image, just like a normal animate behavior would do.

Note that this method does have its draw backs. It is more reliable in the sense that it WILL run through the entire animation, frame by frame, without skipping or stopping at any frames. It also gives you more control over the speed of the animation, opening up a whole new range outside from gamesalad's limit of 0 - 30 fps. However, if your overall FPS does slow down during the game, the animation will not skip any frames but the speed of the animation will slow down. Personally, I prefer my animation to slow down rather than stay at the same speed while skipping over frames.

So there it is, I thought I would share this little trick with you guys as it really helped me out a lot. If you have any questions, please feel free to ask, sorry if I was not clear enough, I tried to explain as best as I could. I can also provide a sample screenshot if you guys would like.

Comments

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2016

    Nice method !

    You can also do the same thing with: Constrain image to floor( game.Time*30)%21

    (this is looped, the '30' is the frame rate, the '21' is the loop point)

    You check it out in more detail here: https://forums.gamesalad.com/discussion/63454/fast-frame-swap/p1

    . . . . . . . . . .

    "Now, if you have more than nine frames, you HAVE to account for the change in digits from 09 to 10 This means you will need two different loops, one for when the name of the actor's image is "NameOfImage0"..self.count and one for when it is "NameOfImage"..self.count."

    There's no need for two rules, simply name your sequence without the '0' (so NameOfImage_1 NameOfImage_2 NameOfImage_3 . . . . NameOfImage_8 NameOfImage_9 NameOfImage_10 NameOfImage_11 . . . etc)

  • zweg25zweg25 Member Posts: 738
    edited May 2016

    @Socks said:
    "Now, if you have more than nine frames, you HAVE to account for the change in digits from 09 to 10 This means you will need two different loops, one for when the name of the actor's image is "NameOfImage0"..self.count and one for when it is "NameOfImage"..self.count."

    I actually think you can use padInt(self.count, 2) and keep what you have and it'll work in one rule

  • bernardo.chuecosbernardo.chuecos Member, PRO Posts: 75

    @Socks said:
    Nice method !

    You can also do the same thing with: Constrain image to floor( game.Time*30)%21

    (this is looped, the '30' is the frame rate, the '21' is the loop point)

    You check it out in more detail here: https://forums.gamesalad.com/discussion/63454/fast-frame-swap/p1

    . . . . . . . . . .

    "Now, if you have more than nine frames, you HAVE to account for the change in digits from 09 to 10 This means you will need two different loops, one for when the name of the actor's image is "NameOfImage0"..self.count and one for when it is "NameOfImage"..self.count."

    There's no need for two rules, simply name your sequence without the '0' (so NameOfImage_1 NameOfImage_2 NameOfImage_3 . . . . NameOfImage_8 NameOfImage_9 NameOfImage_10 NameOfImage_11 . . . etc)

    Yeah you're right, I don't think you HAVE to add the zeros, I don't know what I was thinking lol :joy: however if the program you use exports the png sequence with the zeroes added and you have a lot of frames, I think it's easier to just keep the zeros

  • SocksSocks London, UK.Member Posts: 12,822

    @bernardo.chuecos said:
    Yeah you're right, I don't think you HAVE to add the zeros, I don't know what I was thinking lol :joy: however if the program you use exports the png sequence with the zeroes added and you have a lot of frames, I think it's easier to just keep the zeros

    If there are less than 99 frames, then you simply need to rename the first 9 images (remove the 0) - beyond that there are numerous free apps that will renumber / resequence your image sequences at the click of a button (I use NameChanger https://mrrsoftware.com/namechanger/ ), I'd say correctly preparing the image sequence was preferable to having GameSalad accommodate a less than ideal image sequence (for instance using additional rules to accommodate a numeric sequence / format that GS doesn't understand).

    But either way, I agree, your method is much better and much more flexible than the animate behaviour.

  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922

    @Socks said:

    @bernardo.chuecos said:
    Yeah you're right, I don't think you HAVE to add the zeros, I don't know what I was thinking lol :joy: however if the program you use exports the png sequence with the zeroes added and you have a lot of frames, I think it's easier to just keep the zeros

    If there are less than 99 frames, then you simply need to rename the first 9 images (remove the 0) - beyond that there are numerous free apps that will renumber / resequence your image sequences at the click of a button (I use NameChanger https://mrrsoftware.com/namechanger/ ), I'd say correctly preparing the image sequence was preferable to having GameSalad accommodate a less than ideal image sequence (for instance using additional rules to accommodate a numeric sequence / format that GS doesn't understand).

    But either way, I agree, your method is much better and much more flexible than the animate behaviour.

    OS X has a build in feature for renaming files.

Sign In or Register to comment.