PDA

View Full Version : A pretty tween for sprites


jubishop
07-12-2007, 08:58 PM
This works for tweening sprites:

package {
import flash.display.Sprite;
import mx.effects.Tween;

public class Test extends Sprite {
private var mySprite:Sprite;

/************************************************
* Constructor.
* Test()
************************************************/
public function Test() {
mySprite = new Sprite();
mySprite.graphics.beginFill(0xff0000);
mySprite.graphics.drawRect(0, 0, 100, 100);
mySprite.graphics.endFill();
addChild(mySprite);

var myTween:Tween = new Tween(this, 0, 100, 1000, 24);
myTween.setTweenHandlers(updateSpriteX, null);
}

private function updateSpriteX(newX:Number):void { mySprite.x = newX; }
}
}

but it is sooo ugly! The mx.effects.Move, Fade, etc. classes that subclass TweenEffect are really nice, but apparently only work with classes that implement the IUIComponent Interface (of which there are only two: UIComponent, and UITextField). UIComponent is a subclass of Sprite, so I thought I could do this:

package {
import flash.display.Sprite;
import mx.core.UIComponent;
import mx.effects.Move;

public class Test extends Sprite {
/************************************************
* Constructor.
* Test()
************************************************/
public function Test() {
var myComponent:UIComponent = new UIComponent();
myComponent.graphics.beginFill(0xff0000);
myComponent.graphics.drawRect(0, 0, 100, 100);
myComponent.graphics.endFill();
addChild(myComponent);

var myMove:Move = new Move(myComponent);
myMove.xTo = 200;
myMove.play();
}
}
}

But then the red box doesn't show up at all...which I find mysterious because it clearly has a graphics property just like Sprite...and it doesn't seem to be overriding Sprite's display methods...

Is there any way I can use UIComponents to draw with the Graphics class property "graphics"? Why can the drawing API only be used with Sprite, even though UIComponent and it's subclasses still contain it? Or if it can be used, why does the above example not produce a red box?

Alternatively, is there any way I can use mx.effects.Move with Sprites?

Thanks!

dr_zeus
07-12-2007, 09:01 PM
No need to start a new thread. You could have kept the conversation going in the other one. I just recommended Tweener (http://code.google.com/p/tweener/) to you in that thread.

jubishop
07-12-2007, 09:46 PM
Thanks for much for your help dr zeus! Sorry to generate a duplicate thread...I'll start using Tweener.