Dissolving behaviors
January 8, 1998
by Pat McClellan
Dear Multimedia Handyman
How can I create smooth fades in and out for text bulletpoints? I don't like the pixel or bit dissolves that come with Director. I need something that looks more like video dissolves.
Karen Entner
Dear Karen,
The dissolves you describe are called "keying" in video, as is typical for any character generated text such as a newscasters name. In video, this is accomplished by using the alpha channel to omit the background, while the rest of the picture dissolves in. Director's transitions handle dissolves by using pixel replacement, where one picture changes to another pixel by pixel or bit by bit. The different "look" comes from the fact that each pixel appears at 100% opacity, rather than being blended by using the alpha channel.
All this alpha channel stuff may sound confusing, but it's really something that you're probably used to. When you set a sprite to background transparent ink, you're using the alpha channel -- or background color -- to omit part of the picture. That's the alpha channel. The blend ink effect also uses this, so to get the effect you want, we'll need to set your text sprites to blend ink.
Let's write some behaviors to handle the fade in and fade out. By making them behaviors, you can drop them on any sprite you want to transition. Start with thinking about what you want to be able to specify or customize when you drop the behavior on a sprite. For now, we'll just give you control over the number of frames for the transition. So we'll declare that as a property and write a getPropertyDescriptionList handler which allows us to set it within a range of 3 to 45 frames, defaulting to 5 frames.
property frames -- number of frames for fade in property currentBlend on getPropertyDescriptionList set p_list = [ #Frames: [ #comment: "Number ¬ of frames for fade duration", #format: ¬ #integer, #range: [ min:3, max:45 ], ¬ default: 5 ]] return p_list end
It's good form to include a getBehaviorDescription handler, so that if others use your behavior, there's some explanation of what it does. This is an easy handler to write.
on getBehaviorDescription return "This behavior fades in the sprite ¬ for a duration of frames specified by ¬ the author." end
Now, to the guts of the functionality. When the sprite is first encountered, we want the blend value to be set to 0. This will make the text invisible. We'll use our property "currentBlend" (declared at the top of the script) to hold the blend value, then set the blend of sprite to the value held by currentBlend.
on beginSprite me set currentBlend = 0 set the blend of sprite the spriteNum ¬ of me = currentBlend end
On frame 1 of our sprite's existence, its blend is set to 0. Now, we need to increment that blend value up to 100 over the duration of frames specified in our property "frames". Let's say that you specify that the fade in should take 5 frames. That means that in first 5 frames of the sprite, currentBlend needs to increment 0, 20, 40, 60, 80, and 100. To make this change happen every frame, I used a prepareFrame handler (you could use an enterFrame or exitFrame handler as well.) This handler will run on every frame of the sprite's existence.
First, I see if the fade in has already been completed by checking if the value of currentBlend is less than 100. If not, that means that the sprite is faded all the way in and the handler skips to the end. So let's say that we're on the first frame & currentBlend is 0. We want to add to currentBlend an increment equal to 100 divided by the number of frames in our transition. I call this amount "blendIncrement". After that is done, I need to make sure that this amount doesn't exceed 100 because Director can't handle a blend value > 100. Finally, I set the blend of the sprite equal to the currentBlend value.
on prepareFrame me if currentBlend < 100 then set blendIncrement = integer(100/frames) set currentBlend = currentBlend ¬ + blendIncrement if currentBlend > 100 then set ¬ currentBlend = 100 set the blend of sprite the spriteNum ¬ of me = currentBlend end if end
That's all there is to it. Now, here's the corresponding behavior for the fade out.
property frames -- number of frames for fade in property currentBlend on getPropertyDescriptionList set p_list = [ #Frames: [ #comment: "Number ¬ of frames for fade duration", #format: ¬ #integer, #range: [ min:3, max:45 ], ¬ #default: 5 ] ] return p_list end on getBehaviorDescription return ¬ "This behavior fades OUT the sprite for a duration ¬ of frames specified by the author." end on beginSprite me set currentBlend = 100 set the blend of sprite the spriteNum of ¬ me = currentBlend end on prepareFrame me if currentBlend > 0 then set blendIncrement = integer(100.00/frames) set currentBlend = currentBlend ¬ - blendIncrement if currentBlend < 0 then set ¬ currentBlend = 0 set the blend of sprite the spriteNum ¬ of me = currentBlend end if end
For those of you who are ambitious or want a little challenge, think about how to modify this behavior for greater functionality. Some suggestions:
- Create one handler for both fade in and fade out by adding a check box selection in the property description dialog box.
- Add sliders that allow the user to set the start and end blend values so that you can do partial fades in and out.
- Change the behavior to make it time based (ticks) rather than frame based.
Good luck & let me know how you have modified this for your applications.
Copyright 1997-2024, Director Online. Article content copyright by respective authors.