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.

DarkBASIC Professional Discussion / Sprite fade in/out based on timer?

Author
Message
Mugen Wizardry
User Banned
Posted: 6th Jan 2011 03:41
Hi all. I'm having a bit of a problem. You see, what I want to do, is fade out a sprite after a timer reaches 0 after a specified amount of time.

What I need is after the timer reaches 0, for the sprite to fade out.

I also need to fade in the sprite as well.

Here is the code I made:



Thanks for the help! ^^

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD AT Amazon.com!
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 6th Jan 2011 07:26 Edited at: 6th Jan 2011 07:42
Good to see some code from you, always the best way to get help.

Whenever something isn't working in your program like you think it should, adding print statements as debug code is the best way to track it down.
Here is your program, slightly modified to compile, you example wouldn't even compile because of this command


So I added some print statements to debug your code


The fadeval=0 with no fading happening
So time to check the code...
Seems you are in your repeat loop for fading with no sync command
that will redraw the screen to see the fading
So adding a sync command in your repeat loop will fade the sprite

While this works, it is really NOT what you want to do...
You really only want one sync command in your main loop unless you are getting user input or doing something special

You don't really want to fade an object in a repeat loop causing everything on the screen to halt until the fading is completed,
so fading it out or in a little bit each time through your main loop is the best way to do this.
Try it yourself, and if you get stuck, ask for help with your new code.
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 6th Jan 2011 07:37 Edited at: 6th Jan 2011 07:39
A few constructive comments...
1) Don't skip a line between each command
2) Change your tab spacing to 3, more common and easier for use to edit your code when asking for help
3) Always copy your code from your forum post into a newly loaded DBPro and see if it compiles,
that will tell you if you forgot to copy all the code or if there is a problem with your post code, your code wouldn't compile the way you posted it.

Hey, great to see you posting code when asking for help!
Good Luck to your programming efforts!
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 6th Jan 2011 08:00 Edited at: 6th Jan 2011 08:06
After writing my constructive comments, which I hope you will take in the
helpful manner that they were intended to be, I didn't want to seem to harsh, and to help a little more.

So here is some improved code that fades the sprite over and over
with only one sync command in the main loop

Hope this helps.

Work on your fade in sprite routine, if you get stuck, post your new code here for help.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 6th Jan 2011 13:53 Edited at: 6th Jan 2011 13:54
Just thought I'd add to this. Analyse your code cause there's a ton you don't need and this can be optimized quite a bit. Efficiency is key in programming.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Mugen Wizardry
User Banned
Posted: 6th Jan 2011 14:53
thanks guys.

How would i make it fade in after the timer?
here's the code:



Thanks!

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD AT Amazon.com!
Mugen Wizardry
User Banned
Posted: 6th Jan 2011 20:34
anyone? :/

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD AT Amazon.com!
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 6th Jan 2011 20:58 Edited at: 6th Jan 2011 22:44
Yep, I'm working on it...back in a few minutes.

OK, here it is.....I added a few things...


I built a typed array for sprite fading, as well as a little fading engine...you can:
Fade In
Fade Out
Fade In & Out

I will add comments to the code, just wanted to get it out here for now....Enjoy!
I think I will post a tutorial on this
Mugen Wizardry
User Banned
Posted: 7th Jan 2011 00:19
its PERFECT. thank u

Except 1 thing. how do i control how many times i can call a fade in or fade out or both?

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD AT Amazon.com!
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 7th Jan 2011 00:58 Edited at: 7th Jan 2011 02:17
Not sure exactly what you mean...
But you can call the fadesprite routine as many times as you want to.
You can change it if you want to, but this version expects your sprites to be numbered
from 1 to 100, and updates the correct slot in the sprite array every time you call the fadesprite routine.

Try using it, you can fade sprites, blink sprites by fading in & out faster, etc.
Here is 100 sprites fading in & out
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 7th Jan 2011 16:48
Nice, though it might be best to implement timer based fading so if the frame rate changes it runs a constant speed. So sort of like 2 key frames using linear interpolation.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Mugen Wizardry
User Banned
Posted: 7th Jan 2011 16:51
how exactly would i do that?

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD AT Amazon.com!
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 7th Jan 2011 17:09
I'll post an example later when I get back from work, but for now look up timer based movement.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 7th Jan 2011 18:49 Edited at: 7th Jan 2011 18:54
I guess no work today.

Ok, linear interpolation basically is a straight line between two points. I've seen you've been doing a bit with 3d bezier's, this can be known as bezier interpolation or B-Spline interpolation, so basically these are method from getting to one point and to another.

What we want to do is work out where a value would be between a specific time. To do this we need what we'd call a 'Key', which is a point with in a frame time. The 'Key' holds the time or frame of the point and holds a value. So if we wanted to move an object forward 100 units with in 3 seconds, we'd make two keys, one with a time of 0.0 and a value of 0, and the other with a time of 3 and a value of 100, then work out the value based on the elasped.

Now when we program something, we need to work out what is 'Specific' and what is 'Generic' (common). So for instance, I want to fade a sprite over certain amount of time. Now looking at that, you need to think what else could need to be faded over a certain amount of time or what needs a value changing over time. This would be classed as 'Generic' because the main feature, which is work out a value at a certain time, can be used for many things. If it was something that could only be applied to Sprites then it's 'Specific'.

So with this let's get in to this:

More info on: Linear Interpolation

I came up with a neat function to do this between two points, it can also deal with inc and dec between points. The only issue is I don't have a lot of time to explain the math. I will edit in how the math works in a bit when I get back to the comp.

The function:


And using it:


A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.

Login to post a reply

Server time is: 2026-07-18 00:35:03
Your offset time is: 2026-07-18 00:35:03