PDA

View Full Version : Simultaneous alpha tweens



freestyle
May 12th, 2008, 12:01 AM
I'm a Web Teacher for a HS academy. We want to create a slideshow of different size images using Flash CS3.

In Flash AS3, I have two Sprites being dynamically loaded with different dimension images. In the end, I want these different size photos to fade out and fade in simultaneously. I can only get Tween to perform one after the other but not simultaneously with

var fadeout:Tween = new Tween(one, "alpha", Strong.easeOut, 100, 0, 3, true);
var fadeIn:Tween = new Tween(two, "alpha", Strong.easeIn, 0, 100, 3, true);

Basically, how do you perform 2 simultaneous alpha tweens (in my case, because of DIFFERENT SIZE photos)?

Thanks!

RebuiltJorge
May 12th, 2008, 12:40 AM
here try this, you might have to fine tune it to meet your specifications

package {
import flash.display.Sprite;
import flash.events.Event;

public class Pulse extends Sprite {
private var ball:Ball;
private var angle:Number = 0;
private var centerScale:Number = 1;
private var range:Number = -1;
private var speed:Number = .5;

public function Pulse() {
init();
}
private function init():void {
for (var i:int = 0; i < 2; i++) {
ball = new Ball();
addChild(ball);
ball.x = stage.stageWidth / 2 + 100 * i;
ball.y = stage.stageHeight / 2;
ball.addEventListener(Event.ENTER_FRAME, startPulse);
}
}
public function startPulse(event:Event):void {
event.target.alpha = centerScale + Math.sin(angle) * range;
angle += speed;

}
}
}

Rezmason
May 12th, 2008, 12:56 AM
Basically, how do you perform 2 simultaneous alpha tweens (in my case, because of DIFFERENT SIZE photos)?

Great question. It'd be nice if Tweens would let you manipulate multiple properties of multiple objects at the same time. To overcome their inflexibility, you can do this with a single Tween:

var guineaPig1:GuineaPig = new GuineaPig();
var guineaPig2:GuineaPig = new GuineaPig();
var tween:Tween = new Tween(guineaPig1, "alpha", Regular.easeInOut, 0, 1, 1, true);
// ...
tween.stop(); // Tweens play automatically. I hate this!
tween.addEventListener(TweenEvent.MOTION_CHANGE, updateOtherThings);
// ...
function updateOtherThings(event:TweenEvent):void {
guineaPig2.alpha = guineaPig1.alpha;
}

The disadvantage to doing this is that now the objects affected by the Tween are in two places: in the Tween's constructor, and in the updateOtherThings function. We can improve the code above by doing this:

var guineaPig1:GuineaPig = new GuineaPig();
var guineaPig2:GuineaPig = new GuineaPig();
var prop:Number;
var tween:Tween = new Tween(this, "prop", Regular.easeInOut, 0, 1, 1, true);
// ...
tween.stop();
tween.addEventListener(TweenEvent.MOTION_CHANGE, updateThings);
// ...
function updateOtherThings(event:TweenEvent):void {
guineaPig1.alpha = prop;
guineaPig2.alpha = prop;
}