I need help with "setSpriteAnimation()" difficulties...
-
I've been going through the tutorials and finally have been trying to freestyle random stuff on my own with what i know, and I have a 2d humanoid sprite than can move left and right. If I type in setSpriteAnimation BEFORE the main loop it will start with the running or the idle animation (whichever i don't comment out) however.. when i stick the setSpriteAnimation anywhere inside of the main loop he does not move at all..
I know I need to set states, most likely, for each animation (and flag those states within movement conditionals) BUT where do I then check for the animation flags to actually get them to run within the main loop?
I basically can't get the "setSpriteAnimation(sprite, 0 , 180, 20)" to work ANYWHERE within the main loop to start with..
//up here it works loop clear() drawBackgroundStuff..() getControlInputs...() // the "setSpriteAnimation()" does not work between any of these in the loop.. drawSprites() updateSprites() update() repeat
Does that make sense?
What am I missing?Thank you!
-Mike
-
Hi Mike
The reason your animation isn't working when the command is in the main loop is because you are setting the animation back to the beginning every frame (up to 60 times per second).
Unfortunately I have not worked with sprite animation yet so am not able to tell you what you need to do about it, just why it is happening.
-
Ah.. so it is happening, yet it is constantly restarting.. hmm what to do?
-
@lawyerlounge Sorry I am not clear what you are trying to do.
https://fuzearena.com/help/view/setSpriteAnimation just defines the animation sequence.
Calls to https://fuzearena.com/help/view/updateSprites cause the animation to be cycled through automatically.
You can control the speed of the animation using https://fuzearena.com/help/view/setSpriteAnimSpeed (0 would make it stop)
It is also possible to handle the animation yourself by setting the animation frame manually:
-
My end goal would be to have the character perform certain animations at different times. To start, I just want him to perform an "Idle" animation when not pressing any buttons, and when moving in any direction, perform a "walking" animation. If i put setSpriteAnimation(target sprite, start frame, end frame, speed of animation) anywhere within the loop (just to have it running no matter what direction I am moving) it will not work.
I wonder then, how any animation works within a loop, since the loop is running so fast that the animation is just continuing to start over and over again (appearing to not move at all).
Does that make more sense?
Edit: I looked at your example "setSpriteAnimSpeed()", and in the example, the setSpriteAnimation() just sets the speed to 0 at the top (out of the main loop) and they use, within the loop, the SpriteAnimSpeed(). I'm not home, but that's worth a try i suppose.
-
I have a bit of experience in this area and I think I have a system which is quite nice.
What you need is a state variable, with your states indexing an array of animation data. Then you'll need a Boolean (true or false) switch to perform
setSpriteAnimation()
only once when an animation changes.Something like:
// This array of image files is only necessary if you're using assets which have multiple different sheets for different animations, for exmaple, the Ansimuz "GothicVania" assets. playerImgs = [ loadImage("file for the idle animation"), loadImage("file for the run animation"), loadImage("file for the attack animation") ] // Now, we create a some state variables in the same order as the loadimage files. If we always use this structure, we can index very handily using the states idle = 0 run = 1 atk = 2 state = idle // We'll also want an oldstate variable to check if our state has changed oldState = idle // Set up some animation data in the same layout: anim = [ [0, 4], // start and end tile of the idle animation - numbers here are of course just examples [0, 8], // start and end of the run animation [0, 6] // start and end tile of the attack animation ] // our boolean animation switch animSwitch = false
Now, in our main loop, we might have something like:
if j.lx > 0.3 and state != atk then state = run animSwitch = true endif // You might have a whole load of these Ifs, which check for buttons and states and set the correct state and turn the animSwitch on for each one // Then, somewhere later in the code, you'll need: if state != oldstate and animSwitch then setSpriteImage(player, playerImgs[state]) // Only necessary if using multiple spritesheets setSpriteAnimation(player, anim[state][0], anim[state][1], fps) // Here's your animation line. Because of the animSwitch, this will only trigger once, when the player state changes animSwitch = false // We must turn off the animSwitch or this will keep triggering endif
I hope this helps you dude! Let me know how you get on.
-
@Dave Did you write the 2d game tutorials? My son and I did the "animation" one a few days ago, and the code looks a lot like this. Very helpful!
-
@Spacemario I did indeed! It's awesome to know that you've been going through them with your son, and that you've found them useful. I believe the game tutorials don't use the sprite commands (the sprite functions were added after the game demo was made), and thus you don't need something like the
animSwitch
variable, due to usingdrawSheet
and notsetSpriteAnimation
. It makes the setup slightly different (I believe the game tutorials use a.start
and.length
property for the animation data, rather than start frame/end frame forsetSpriteAnimation
). Other than that, the code is largely the same. Still need to have states and check against the oldState!Nice one!
-
@Dave That's super cool! I've been telling people that the best part about Fuze are the tutorials. That's not to slight the incredible work that undoubtedly went into all other aspects of the product-- but rather, it's a compliment to just how well-written the tutorials are!
In a way, I think it's better that the tutorials teach you how to do things "the hard way" first-- for example, use a loop to draw the map from a big array, versus jumping straight to the map editor. It's like when I first did Java so many years ago (decades, lol), and I forced myself to use Notepad and javac for awhile before jumping to an IDE.
-
@Spacemario The documentation in general is going to be critical to the success of the system as an educational tool, and it's really heartening that the devs understand this.
-
@Spacemario That is indeed a huge compliment. Means an absolutely huge amount - I'm so glad they're doing their job and I'm excited to bring more to the product :)