Controling sprite animation by comparing two values

How do I:
Control sprite animation by comparing two values.

What is the expected result:
The animation of the sprite would change based on the compared values.

What is the actual result:
The sprite is stuck at animation #0 regardless of the conditions comparing the values.

Related screenshots:


I don’t understand what the variables represent or what is supposed to happen. The 2nd group of events could probably be simplified to a couple of events if you use a formula based on the math expressions on the left. If it doesn’t return a whole number, you would need to use trunc() as in truncate to remove the decimal portion. Depending on your needs round() might be better.

You may still need conditions to make sure it’s within range or you could use the clamp() expression to clamp it within a range.

Example unrelated to your project

Set animation to trunc(a * 10) + 1

Or

Conditions:
A >= 0
A < 11

Action:
set animation to A

Anytime you use events more than a couple of times, it helps to look for a pattern. Look for a formula or other method to keep things simple.

To debug this, I would check the value of the variables and scale either through the debugger or by adding a text box or two to display the values at different locations.

The value CandleLifespanSnapshot/11 will always be 1. The first event’s value ((CandleLifespanSnapshot/11)*12 will be 12 and the largest value you are comparing with.

The y scale of TimeBar will always be more than 36 (400 /11 or BarHeight/CandleLifeSpan).

So none of the conditions will ever be met, and the candle animation never changes.

I’d suggest you remove all the events in the “Candle Animation 2a (Main)” event block, and replace it with one unconditional event with the action Change the number of the animation of CandleStick set to CandleLifespan. The best place to add that would be the same event where TimeBar scale is modified.

I don’t understand what the variables represent or what is supposed to happen.

I want the Player’s ammunition capacity to gradually increase as the game progresses, without the ammunition bar scale to increase. I created a system that can work with any arbitrary amount of ammunition without setting it all by hand.

CandleLifespan: How long the in game weapon (a candle) will last, in seconds. It’s 11 in this example for debugging purposes, as the candle sprite has 12 animation frames and I can see the candle melt every second as a test.
CandleLifespanSnapshot: Something I thought I needed. Will be deleted. Ignore it.
BarHeight: Sets the max height the ammunition bar can have. I don’t want it increase even if the ammunition capacity it represents increases, for UI consistency.
BarSegments: Determines by how many segments the bar is made of, and one segment is decreased every second until the bar reaches zero or a shot is performed. It is identical to CandleLifespan, but I made a different variable with a more descriptive name for clarity’s sake.
SegmentHeight: Is how tall the segments that form the bar will be and is defined by BarHeight divided by the amount of BarSegments. In this example it’s 400/11, but in the actual game it could be any number like 400/200 or 400/1000. The result of this fraction is how tall a segment’s height will be.

As for the rest of your post, I’m not sure how I could use a formula.

The value CandleLifespanSnapshot/11 will always be 1. The first event’s value ((CandleLifespanSnapshot/11)*12 will be 12 and the largest value you are comparing with.
The y scale of TimeBar will always be more than 36 (400 /11 or BarHeight/CandleLifeSpan).
So none of the conditions will ever be met, and the candle animation never changes.

It turned out this was correct, and I realized I’ve been using the wrong variables or objects for comparison. Adding a new variable (BarBlock = BarHeight/11 (eleven, because the candle sprite has 12 animation frames)), I have fixed it and it’s now working as intended.
Relevant screenshots:

If there’s an easier way to make these events above work like in a formula, please let me know!
Thanks everyone for the help!

1st thing
Why it is stuck
Look here


I covered your calculation cause it does not matter
What matters is increasing value
Your last event check if its less than 1
2nd if its less than 2
But when its lest then 2 its also less than 1 do you get it?

If you have less than 20 years
ANYTHING below being 20 years old will qualify
And since your last event set animation to 0 that is why it is stuck to at frame 0
Multiple animations to play at once so it is stuck on last executed animation

2nd thing
You DO NOT check if its less than something
And it will apply to every single event you check scale on y axis of TimerBar

You check if its less than 1
If its Less than 2 but higher than 1
If its less than 3 but higher than 2
And so go on

And it will work perfectly fine

So your 2nd event from bottom should be
Condition
The scale on Y axis of TimeBar > 1
The scale on Y axis of TimeBar < 2

Action
Set animation to 1

3rd event from bottom should be

Condition
The scale on Y axis of TimeBar > 2
The scale on Y axis of TimeBar < 3

Action
Set animation to 2

And so go on

Yeah, I gave that in my post:


BTW, read my whole post. It explains why you aren’t seeing a change in animation.

As stated in my previous post:

I’ve been using the wrong variables or objects for comparison. Adding a new variable (BarBlock = BarHeight/11 (eleven, because the candle sprite has 12 animation frames)), I have fixed it and it’s now working as intended.

So the events work even checking if a value is “less than something” because the events are essentially separate if statements. This is also how it works in most programming languages.

As for the rest of your post, I appreciate the cleverness of it, thank you so much!

As stated in my previous post:

CandleLifespan: How long the in game weapon (a candle) will last, in seconds. It’s 11 in this example for debugging purposes, as the candle sprite has 12 animation frames and I can see the candle melt every second as a test.

Given that the candle sprite only has 12 frames I couldn’t directly use the action you mentioned. However that led me to have the idea of normalizing the bar’s Y scale to BarHeight and use a single event:
image
So thank you for that!

I did read your entire post.

As stated in my previous post:

The value CandleLifespanSnapshot/11 will always be 1. The first event’s value ((CandleLifespanSnapshot/11)*12 will be 12 and the largest value you are comparing with.
The y scale of TimeBar will always be more than 36 (400 /11 or BarHeight/CandleLifeSpan).
So none of the conditions will ever be met, and the candle animation never changes.

It turned out this was correct, and I realized I’ve been using the wrong variables or objects for comparison. Adding a new variable (BarBlock = BarHeight/11 (eleven, because the candle sprite has 12 animation frames)), I have fixed it and it’s now working as intended.

So it directly addressed the problem and helped me solve it.
Again, thanks everyone for the help!

1 Like

Ah, my bad, there was a wall of writing between you and ZeroX4 that I did a tldr; and just read the last line.

Good to see you worked out a formula for the candle animation, and thanks for sharing it :smiley: