GeneralSanguo工作笔记4——自定义MovieClip效果

    技术2025-07-06  12

    从昨天下午一直到现在,费了九牛二虎之力才勉勉强强搞通了一点东西,先记下来,等明天继续研究。

     

    目标:当某个事件发生时(例如鼠标点击),在指定的控件上能够播放一段Swf动画

    前提:

    1、理解如何自定义效果。

    2、理解Flex4中MovieClip的使用方法(到目前我也不是很理解,还请高人指点)。

    失败:

    1、除了使用Embed来预编译Swf文件外,有其他的方法能够在运行时加载swf文件吗?

    2、swf文件不能被MovieClip类中的stop方法所终止。

    3、指定控件添加效果的方法比较愚蠢

    参考:

    1、http://flexperiential.com/2010/12/19/creating-a-custom-animation-effect-in-flex-4/

    2、http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf68e80-7fee.html

    3、http://www.efflex.org

     

    正题:

    1、根据参考参考2,我得知想自定义一个效果需要实现两个类:工厂类和实例类。

    工厂类代码:

    package myEffects { // createcomps_effects/myEffects/MyFlashParam.as import mx.effects.Effect; import mx.effects.EffectInstance; import mx.effects.IEffectInstance; import mx.core.UIComponent; public class MyFlash extends Effect { public var popUpContentPane:UIComponent = new UIComponent(); // Define constructor with optional argument. public function MyFlash(targetObj:Object = null) { // Call base class constructor. super(targetObj); // Set instanceClass to the name of the effect instance class. instanceClass= MyFlashInstance; } // Override getAffectedProperties() method to return an empty array. override public function getAffectedProperties():Array { return ["MovieClip"]; } // Override initInstance() method. override protected function initInstance(inst:IEffectInstance):void { super.initInstance(inst); MyFlashInstance(inst).popUpContentPane = popUpContentPane; } } }

    实例类代码:

    package myEffects { // createcomps_effects/myEffects/MySoundInstance.as import flash.display.MovieClip; import flash.media.Sound; import flash.media.SoundChannel; import mx.core.UIComponent; import mx.effects.EffectInstance; import mx.effects.effectClasses.TweenEffectInstance; public class MyFlashInstance extends TweenEffectInstance { [Embed(source="assets/MovieClipSource.swf")] [Bindable] private var flashCls:Class; public var popUpContentPane:UIComponent = new UIComponent(); public var mv:MovieClip = MovieClip (new flashCls()); // Define constructor. public function MyFlashInstance(targetObj:Object) { super(targetObj); } // Override play() method. // Notice that the MP3 file is embedded in the class. override public function play():void { super.play(); popUpContentPane.addChild(mv); tween = createTween( this, 0, 1); mv.play(); } } }

     

    具体说明请参照参考2,我会在以后抽时间给出解释。

    2、效果就是按照上面的方法定义,下面是使用的方法:

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:myEffects="myEffects.*"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> <myEffects:MyFlash id="MyFlasheffect" target="{mybutton}" /> </fx:Declarations> <s:Button label="start" click="this.addElement(MyFlasheffect.popUpContentPane);MyFlasheffect.end();MyFlasheffect.play();" id="mybutton" /> <s:Button click="MyFlasheffect.end();" label="stop" id="mysbutton" x="0" y="68"/> </s:Application>

     

    这个例子还需要继续修改。

     

    最新回复(0)