Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / I wish SetColor and GetColor functions accepted float values natively

Author
Message
JonEnstrom
3
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 30th Oct 2020 18:59
I ran into an issue trying to slowly adjust alpha values (say you want a sprite (or object, or text) to slowly fade into view, or fade out of view).

Using code like this:

SetSpriteColorAlpha(sprite, GetSpriteColorAlpha(sprite) + 20 / ScreenFPS())

At a sync rate of 60 FPS this actually doesn't increment the alpha value at all because 20 / 60 = 0.33 which is getting rounded to 0 (because the function takes an integer not a float). I understand that there are only 255 levels of alpha value or RGB color values, but if AppGameKit stored the value as a float (and then rounded off internally) then the code above would increment the alpha by 20 per second (at any FPS). Instead it results in nothing happening.

Obviously you can maintain your own float type variable for the alpha value of every object/sprite/text in your game, and then pass that to the function, but it would be easier and cleaner if the native functions themselves supported floats in the first place.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 30th Oct 2020 20:24
You could use tweens. Then the effect would be time based as it should be
JonEnstrom
3
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 31st Oct 2020 00:24
Yes of course that would work as well, however at that point I might as well just maintain my own float variable with the alpha value rather than creating, setting, playing and then deleting tweens.

For example in my game when you score points or grab a powerup it generates a text event, which then fades out over a few seconds. Instead of a simple array of integers for the text ID I have to make an object with one integer for the ID and one float for the alpha value and use an array of that object. It's not difficult by any means, it's just too bad I need to store the alpha values myself for more precision when AppGameKit already stores the alpha value of everything in the game anyway.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 31st Oct 2020 00:58
Just check out tweens, it is designed specifically for your example.
It's a lot easier than you think and you won't have to have your alpha float or manage it yourself.
The tween will do everything for you and you can add some really nice motion like easing and bounce
JonEnstrom
3
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 31st Oct 2020 01:14 Edited at: 31st Oct 2020 04:19
Oh I actually use tweens a lot I am very familiar! I typically use them for motion and rotation but you're right it's a decent solution for fading as well.
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 1st Nov 2020 19:41
Ceil() round a float up.

JonEnstrom
3
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 2nd Nov 2020 16:21
Thanks but no help from that because the idea is to increment the value slowly (for example increment by 0.33 per frame so it takes 768 frames to increment by 256) Rounding up the float will just make it increment 1.0 per frame so it would only take 256 frames to increment by 256.

In the end I took the advice of BlinkOk and changed my manual fading code to tweens. It required making a separate tween for each "speed" of fading I use, and separate set of those tweens for each direction (fade in or fade out), and then separate sets of all those same tweens for fading text versus sprites, but in the end it's not that much trouble and is the only way to actually make FPS independent fades work.

As I am typing this I wonder if I should have just used custom value tweens instead of the TweenText / TweenSprite commands lol. I could have used a smaller set of tweens on more things that way.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 2nd Nov 2020 20:22
This is where a tween editor would be priceless i think
Lupo4mica37
3
Years of Service
User Offline
Joined: 1st Jun 2020
Location:
Posted: 3rd Nov 2020 02:15
You need it to increment by 256 in 768 frames, so like Blink0k suggested it's time related, so you can create a clock kinda code to count out the measures for you to increment only when 3 Sync() are complete. With 60 FPS, needing it to increment 1 per 3 frames, that would be 20 per second. It actually worked out 12.8 seconds for the 256 to increment at 60 FPS, however it got to 256 within 13 seconds, must be some fault in my logic, but it seems to be able to do what you want it to.



It's an alternative, not sure how applicable this can be to your programme.
????????
JonEnstrom
3
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 3rd Nov 2020 14:47
Thanks as well but you're severely overthinking the solution! I mentioned in my first post that I could maintain my own float value for the alpha, so for example:

mygameobject.myalphafloat += 20 / ScreenFPS()
SetSpriteColorAlpha(mygameobject.sprite, mygameobject.myalphafloat)

Simple as that! However I landed on using tweens instead.

Also += is syntax from c++ but you get the idea.
Lupo4mica37
3
Years of Service
User Offline
Joined: 1st Jun 2020
Location:
Posted: 4th Nov 2020 21:04
You're wlcome. You did mention passing the value to the function in the last sentence, wasn't sure what you meant. You never mentioned that code you just posted in the first post though. Language is language, as long as you can do something with it that is worth doing, that is all that really matters. Take care.
????????

Login to post a reply

Server time is: 2024-04-24 22:13:44
Your offset time is: 2024-04-24 22:13:44