辉光滤镜 alpha 不循环
public function pulse(obj:DisplayObject, glow:GlowFilter):void
{
obj.filters = [glow];
obj.addEventListener(Event.ENTER_FRAME, function():void {
if(glow.alpha >= 1)
glow.alpha -= 0.05;
else if(glow.alpha <= 0)
glow.alpha += 0.05;
else
glow.alpha -= 0.5;
obj.filters = [glow];
});
}
辉光滤镜 Alpha 循环一次到一半(从 1 下降到 0),但永远不会恢复。我知道这与中间值(0 和 1 之间)有关,但我想不出一个优雅的解决方案。即使使用两个 while(一个用于高于零,另一个用于低于零),它仍然只循环一半。我该如何解决这个问题。
public function pulse(obj:DisplayObject, glow:GlowFilter):void
{
obj.filters = [glow];
obj.addEventListener(Event.ENTER_FRAME, function():void {
if(glow.alpha >= 1)
glow.alpha -= 0.05;
else if(glow.alpha <= 0)
glow.alpha += 0.05;
else
glow.alpha -= 0.5;
obj.filters = [glow];
});
}
The glow filters alpha cycles once halfway (drops from one to zero), but never goes back up. I understand that it has to do with the midrange values (between 0 and 1) but I cannot think of an elegant solution. Even when using two whiles (one for above zero, another for below), it still only cycles halfway. How can I solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想想这里发生了什么......
第一次发光时,我假设 alpha 为 1。如果不是,我的解释有点偏离,但非常接近。
if(glow.alpha >= 1)
为 true。因此,减去 0.05,应用过滤器并重复。glow.alpha -= 0.5
。并且循环重复。glow.alpha -= 0.5
再次运行。并重复。else if
运行,添加 0.05。所以 alpha 现在为 0。重复。else
。减去0.5。剩下-0.45。按照你的方式去做,我会采取稍微不同的方法。
创建一个新的 .fla,将其设为 DocumentClass 并运行它。
然而,我什至不会那样做。我建议使用一个库来执行此 Tween,例如 TweenLite/TweenMax。 http://www.greensock.com/tweenmax/
在一个方向上执行此操作的代码看起来像与此类似:
您只需交替进行补间即可。您甚至可以使用glowEffect Tween 的onComplete 在相反方向触发它。
如果您想要更简洁的代码,您可以将脉冲函数更改为:
Think about what is going on here....
First time the glow comes in, I assume the alpha is 1. If not, my explanation is a little off, but pretty close.
if(glow.alpha >= 1)
is true. So, subtract off 0.05, apply the filter and repeat.glow.alpha -= 0.5
. And the loop repeats.glow.alpha -= 0.5
runs again. And repeat.else if
runs, adding 0.05. So alpha is now at 0. Repeat.else
. Subtracting off 0.5. Leaving us at -0.45.Doing it your way, I would take a slightly different approach.
Make a new .fla, make this the DocumentClass and run it.
However, I wouldn't even do it that way. I would suggest using a library for doing this Tween, such as TweenLite/TweenMax. http://www.greensock.com/tweenmax/
The code to do it in one direction would look similar to this:
The you would just have to alternate doing the tween. You could even use the onComplete of the glowEffect Tween to trigger it in the opposite direction.
If you wanted more terse code, you could change the pulse function to this:
当 alpha 大于(或等于)1 时,您会从 alpha 上减少 0.05。当它在 0 和 1 之间时,您仍然会减少(最后的 else)。当它小于(或等于)0 时,增加 0.05。但是,你看,当它再次为0时,你会减少,然后它会小于0,所以你会增加。此时您将不再看到该元素。 :P
尝试使用 TweenMax 库。您可以通过一行或两行代码获得此效果。如果您仍然想这样做,请使用布尔变量,当它为 0 时,将该布尔值转换为 true。由于该布尔值为 true,因此您可以增加 alpha 值。当 alpha 值为 1 时,您将布尔值设置为 false,并减少 alpha,因为它是 false。 :)
You decrease 0.05 from the alpha when it's greater (or equals to) than 1. When it's between 0 and 1, you still decrease (the final else). When it's less than (or equals to) 0, you increase 0.05. But, look, when it's 0 again, you will decrease, and then it will be less than 0, so you will increase. At this point you won't see the element anymore. :P
Try using the TweenMax library. You can get this effect with one line of code, or maybe two. If you still want to do that this way, use a boolean var, when it's 0, turn that boolean to true. As this boolean is true, you increase the alpha value. When the alpha value is 1, you make the boolean false and decrease the alpha as it's false. :)