这是因为使用Animation时,其目标对象没有被渲染,可以在该对象调用commitStyles()之前等待其被渲染或者使用callLater()方法来确保该对象被渲染后再执行该方法,示例如下:
//等待对象被渲染后再调用commitStyles() var myButton:Button = new Button(); myButton.addEventListener(FlexEvent.CREATION_COMPLETE, buttonCreationCompleteHandler); function buttonCreationCompleteHandler(event:FlexEvent):void{ myButton.removeEventListener(FlexEvent.CREATION_COMPLETE, buttonCreationCompleteHandler); myButton.setStyle("backgroundColor", "red"); }
//使用callLater()方法 private function delayedCall():void{ callLater(doAnimate); }
private function doAnimate():void{ var animation:Animate = new Animate(myButton); animation.duration = 1000; animation.play(); }