Alpha 0 vs Visible = false

2019-08-02 12:15发布

问题:

I'm developing a website in flash that has some animations (oh yeah) and I have a doubt about the way I got a better performance. Some of this animations got an alpha changing from 1 to 0. So there is two ways I did:

  • Tweening, of course, but the objects remains on the stage with alpha = 0, is there some processing job remaining? Or Flash is smart and just ignore the object?
  • Manually, and when alpha is below 0.1 I set visible = 0 and that's it. But I'm wondering if a tween is faster than a hand-made function...

Any opinions?

回答1:

Even when alpha is set to zero, Flash computes the display object on the display list.

Alpha determines opacity of a clip, whereas visible determines whether the clip is rendered by the player.

Therefore if you require no display or interaction, visible=false is higher performance.



回答2:

When an item has an alpha value of 0, it is still rendered by the player, still dispatches mouse/interaction events and still affects the bounds of it's parent.

When you set visible to false, an object is not rendered by the player per say, and does not dispatch mouse/interaction events. It does however, still affect the bounds of the parent.

If you really want to be efficient, I'd recommend removing the item from the display list entirely. eg. removeChild(item); then it no longer affects the bounds of the parent and is truly not rendered.

In regards to tweening, with any library it's pretty easy to set the visible to false when the tween is done or remove from the display list. greensocks tweening library has a special parameter to do just that actually - instead of using the alpha property you use use autoAlpha. WHenever the value dips to zero, it sets the visibility to false. if goes above 0 it flips back to true.

Whether a tween is faster than a hand-made function depends on the quality of the tween and hand-made function. Most tweening libraries are very efficient and I wouldn't worry about it.



回答3:

Definitely use the Greensock library.

To animate the alpha, then make it invisible, do this:

TweenLite.to(movieClip, 0.5, { autoAlpha: 0 });

More info about the Greensock Tweening engine: http://www.greensock.com/tweenlite/

If you have too many objects on the display list and it effects the performance, you still can move your objects out of the "render" area, which (If I am correct) 2000px out of the stage, so then you should do this:

TweenLite.to(movieClip, 0.5, {autoAlpha: 0, onComplete: function() {
    movieClip.x = movieClip.y = 10000;
}});

I hope it helps, Rob