-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix FlxSound
fade problems
#114
Fix FlxSound
fade problems
#114
Conversation
Also reduces a single line of repeated code
Since you can't fade out and fade in at the same time, having two different variables handling the total amount of time to fade was unecessary. Merges `_fadeInTotal` and `_fadeOutTotal` into `_fadeTotal`.
All fading and all timers reset once a sound has stopped (or is paused)
I'm still not sure what to do in some cases. What if:
|
I think some of your questions can be answered if we add a About your questions (assuming the
Do nothing.
Save the current fading state. If the user resumes the sound, it keeps fading from where it was before. If
Stop the fading and start playing from the beginning.
Continue from where "fade out" left off. |
All of those sound like good solutions. I'll go ahead and add them to this commit when I get a few minutes (don't merge it yet).
Let's say the user is in the process of fading out, so the volume is at Sorry for the hassle, but I prefer having a clear direction before coding. |
Using your example, I think the "fade in" should last 2 seconds and go from If I were going to call |
Hehe, I agree with you that it is a better approach, but I was hoping you wouldn't think so. :P It's actually more difficult due to the way the code is set up. It's hard-coded to adjust the volume linearly: protected var _fadeInTimer:Number;
protected var _fadeTotal:Number;
// Inside of `update()`
if(_fadeInTimer > 0)
{
_fadeInTimer -= FlxG.elapsed;
fade = _fadeInTimer/_fadeTotal;
if(fade < 0) fade = 0;
fade = 1 - fade;
} If it were me, I would have all the fading logic and variables separated off into a separate class with a |
Actually, with some clever algebra, I think I can pull it off without adding a bunch of extra variables. This code should automatically calculate the public function fadeIn(Seconds:Number):void
{
if (_fadeOutTimer > 0)
{
var currentVolume:Number = _fadeInTimer/_fadeTotal;
// Just to avoid "infinity" value for `_fadeTotal`
// which may occur if they fade out immediately after fading in
// (without an `update()` in between)
if (currentVolume >= 1)
{
_fadeInTimer = 0;
_fadeTotal = 0;
}
else
{
_fadeInTimer = Seconds;
_fadeInTotal = Seconds / (1-currentVolume);
}
}
else
{
_fadeInTimer = Seconds;
_fadeTotal = _fadeInTimer;
}
_fadeOutTimer = 0;
play();
} Could someone double-check my math before I put this in a pull request? Or is it better just to add an additional variable |
Remove unecessary check if `_channel` is `null` (it won't ever be when `update()` runs unless the developer overrides that behavior) Reset the `amplitude` variables if the sound is muted (rather than leave them at their previous value)
I realize we aren't supposed to add, remove, or modify public members in this release, but this one was minor, and won't break any existing code.
* Only able to `fadeIn()` if the sound is stopped or paused * Only able to `fadeOut()` if the sound is playing
I think this approach is better than adding another variable. About your math, it seems ok to me, but I think I found a typo:
Shouln't it be?
|
This allows you to resume fading in/out after resuming a paused sound.
I added a few commits based on the points discussed earlier (it does not include this needlessly complex workaround for fading from a certain point). One of the commits adds a public I also included a quick bugfix for the
You are entirely correct. Good catch. |
Another tiny question, what if the sound has 2 seconds remaining, but the user sets the sound to fade out over a span of 4 seconds, should the sound end at Shall we assume it's more important for the user to end the sound at volume 0, or to take |
I see no problem with the This approach, however, might add some (needless) extra complexity to the sound class. Maybe we should just end the fade process at the current volume when the sound ends. |
I have been walking on eggshells to avoid adding extra variables to this function, but quite frankly, the code for tweening the volume value doesn't even belong in this class in the first place. So I went so far as to separate off all the "tweening" values to a new, separate class, and made the code oh so much nicer and tidier. :) (it even removed several extra variables!) Since this additional change is going to be rather different than the changes presented in this pull request, if everything so far looks good and functional, shall we merge this request and add the larger changes to a different pull request, or should I just append them to this one? |
(loved the image!) 😄 I think you should append them to this one (since they are connected). Separating the tweening code will certainly make the sound class easier to undersand regarding all those fading stuff. Commit your changes so we can check it. |
A simple class for tweening a value from one to another. Note that this class does not use the "global" time to progress the tween, but must manually be incremented with the `progress` property.
Isn't that much clearer? :)
That's what I get for using a text editor rather than an IDE.
Alrighty then. The proposed changes have been pushed. I also updated the Flixel Sandbox to make sure this worked properly, and it seems to be working beautifully (try interrupting a "fade out" with a "fade in" and see how it handles it just fine). The new However, Flash has a cap of only being able to play 32 sounds at a time. So, at the most, you will be calling 64 extra functions each frame (assuming every single sound is fading at the same time) compared to using the inline code. I don't really see this tiny performance change as an issue, but let me know if you guys disagree. Since all the tweening is delegated to the new |
If a looped sound is resumed, it only plays once from the "pause" point, THEN it resets and plays 9999 loops from the beginning. Fixes FlixelCommunity#120
I added the fix for Issue 120 to the pull request. FlixelSandbox has been updated as well to test the changes (you can checkout previous versions of the sandbox if you want to switch between Flixel versions). |
Any thoughts on |
I really liked This pull request was tested and looks good to me. I think you can proceed merging. |
Great.
It was brought to my attention that there are a few items that still need to be disposed of in the |
Ok, great! |
Made sure to properly destroy all objects, though, a few objects seem to be destroyed more than once. Removes all event listeners before destruction. Also cleans up some of the ASDoc or adds it where needed.
There! All done. Every object should now clean up nicely (and without too much repetitive code). I haven't tested the code in the Sandbox, but it compiles to a SWC without problems. Sadly, it seems up until my last commit the pull request was automatically mergeable; now it is not. This is despite the fact that no other commit has edited Just say the word and I can do the manual merge when you guys have approved the new code. |
Feel to start manually merging. |
This code has already been tested, it is good to go. |
In case anyone was curious, there was actually a merge conflict in |
I guess it was me fixing the pan problems in the past :) |
Actually, it was the following two lines inside of
Which if you use "blame" (also available as a command, |
Fixes a few "fade" problems I forgot to address in the previous pull request.
If the sound is paused or stops, all fade modifiers are reset.