Archive
May, 2009
Browsing all articles from May, 2009
0

This is what I usually do, but it may or may not be the easiest or most sophisticated way of implementing a mute button in actionscript 3. I use Flash CS3 IDE and actionscript 3.0 for the coding, this solution might not work in earlier versions.

To make a mute button, first we need to define two SoundTransform instances, one for muting and the other for un-muting. When the button is clicked, depending on the current state, either of these SoundTransform instances are assigned to the soundTransform property of the global SoundMixer.

var muteTransform:SoundTransform = new SoundTransform(0.0,0.0);
var playTransform:SoundTransform = new SoundTransform(1.0,0.0);

function muteSound():void {
	SoundMixer.soundTransform = muteTransform ;
}

function unmuteSound():void {
	SoundMixer.soundTransform = playTransform ;

}
7

Suspended tween in Flash

If you have ever tried instantiating some tween during run time in flash, you might have noticed that sometimes your tween does not reach its endpoint. This can cause problem ranging from the category of a small nuisance, when you try to move something or make something fade out, to the halting of a complex process by breaking a chain of events when, you rely on the tween completion event to trigger another event in a long chain.

I first came across this suspended tween effect (a.k.a tween got stuck and tween not completing) in flash while working on a flash project that required me to move some entities around the screen upon receiving a mouse click event. To keep things easy to understand, let us say I had an array of objects named box and I had to move all items in box 100 units to right upon receiving a mouse click. I looped through the entire array and instantiated a tween for each of the elements in box.

for(i:int=0;i<box.length;i++) {

	var tween:Tween = new Tween(box[i], "x", Strong.easeOut, 0, 100, 1, true);

	tween.looping = false;
}

Soon I found that some of the boxes would not complete the movement from x=0 to x=100 and get stuck somewhere in between the starting and finishing position. Later, after googling for a while, I found the cause and a solution for this.

The suspended tween effect is caused by garbage collection of orphaned tween objects. If you don’t let your tween become orphans, they don’t get cleared by the garbage collector and your tweens get to reach their respective finishing points. A tween object becomes orphan as soon as it becomes out of scope. Notice that, in my previous code snippet, I have instantiated tween objects inside the loop. As soon as we are out of the loop, all the tween objects instantiated inside are out of scope and become orphans. When the garbage collector finds these orphan tween objects, they are cleared immediately and you see that your tweens have just suspended instead of reaching completion.

The solution is to make sure that your tween objects are not orphaned or out of scope before completion. You can ensure this by keeping a valid reference for the tween object for as long as it is not completed. In my case, I used an array of tween objects declared outside of the loop to hold the tween objects and all was fine.

tweens:Array = new Array();

for(i:int=0;i<box.length;i++) {

	var tween:Tween = new Tween(box[i], "x", Strong.easeOut, 0, 100, 1, true);

	tween.looping = false;

	tweens.push(tween);
}