Flash CS4中"将动画复制为ActionScript"使用初探

来源:互联网 发布:mac 显示.gitignore 编辑:程序博客网 时间:2024/06/05 18:05

刚开始用CS4的时候就注意到了这个这个功能不过都没实践过,这几天实践了一下发现是个很好很和谐的的技能 .有了它我们就可以很傻瓜的编辑一些我们程序员很头疼的代码,比如flash动画的滤镜效果,如果没有这个功能估计帮助文档里面的flash.filters包得看半天…有个这个功能我们就不用愁了.比如现在我想动态添加添加一个元件的模糊效果.

首先我们在时间轴的第一帧放一个元件,然后在后面的时间轴比如第十帧右击插入帧,再右击创建补间动画(不是传统补间哦)接下来打开动画编辑器点击滤镜->模糊分别在实践轴的第一(我都设置为100)和末位帧(我都设置0)设置元件的x模糊值和y模糊值,按下Enter就可以发现是一个从模糊到清晰地渐变效果,我把自己的动画效果代码贴出来:

   1: import fl.motion.AnimatorFactory;
   2: import fl.motion.MotionBase;
   3: import flash.filters.*;
   4: import flash.geom.Point;
   5: var __motion_元件2_3:MotionBase;
   6: if(__motion_元件2_3 == null) {
   7:     import fl.motion.Motion;
   8:     __motion_元件2_3 = new Motion();
   9:     __motion_元件2_3.duration = 10;
  10:  
  11:     // Call overrideTargetTransform to prevent the scale, skew,
  12:     // or rotation values from being made relative to the target
  13:     // object's original transform.
  14:     // __motion_元件2_3.overrideTargetTransform();
  15:  
  16:     // The following calls to addPropertyArray assign data values
  17:     // for each tweened property. There is one value in the Array
  18:     // for every frame in the tween, or fewer if the last value
  19:     // remains the same for the rest of the frames.
  20:     __motion_元件2_3.addPropertyArray("x", [0]);
  21:     __motion_元件2_3.addPropertyArray("y", [0]);
  22:     __motion_元件2_3.addPropertyArray("scaleX", [1.000000]);
  23:     __motion_元件2_3.addPropertyArray("scaleY", [1.000000]);
  24:     __motion_元件2_3.addPropertyArray("skewX", [0]);
  25:     __motion_元件2_3.addPropertyArray("skewY", [0]);
  26:     __motion_元件2_3.addPropertyArray("rotationConcat", [0]);
  27:     __motion_元件2_3.addPropertyArray("blendMode", ["normal"]);
  28:  
  29:     // This call to initFilters supplies the Motion with an Array
  30:     // of the fully-qualified class names of the filters in the
  31:     // target's DisplayObject.filters list, in the same order and
  32:     // indices.
  33:     __motion_元件2_3.initFilters(["flash.filters.BlurFilter"], [0], -1, -1);
  34:  
  35:     // The following calls to addFilterPropertyArray assign data
  36:     // values for each tweened filter's properties.
  37:     __motion_元件2_3.addFilterPropertyArray(0, "blurX", [53,47.1111,41.2222,35.3333,29.4445,23.5556,17.6667,11.7778,5.88889,0], -1, -1);
  38:     __motion_元件2_3.addFilterPropertyArray(0, "blurY", [48,42.6667,37.3333,32,26.6667,21.3333,16,10.6667,5.33334,0], -1, -1);
  39:     __motion_元件2_3.addFilterPropertyArray(0, "quality", [BitmapFilterQuality.LOW], -1, -1);
  40:  
  41:     // Create an AnimatorFactory instance, which will manage
  42:     // targets for its corresponding Motion.
  43:     var __animFactory_元件2_3:AnimatorFactory = new AnimatorFactory(__motion_元件2_3);
  44:     __animFactory_元件2_3.transformationPoint = new Point(0.500000, 0.500000);
  45:  
  46:     // Call the addTarget function on the AnimatorFactory
  47:     // instance to target a DisplayObject with this Motion.
  48:     // The second parameter is the number of times the animation
  49:     // will play - the default value of 0 means it will loop.
  50:     // __animFactory_元件2_3.addTarget(, 0);
  51: }
那么怎么把这段代码用起来呢,注意到了最后一句洋文么,大意就是讲需要此动画的实例名字写在第一个参数里,第二个是动画循环次数,0表示无限循环;
好了我们可以写一段代码来测试下:
   1: var motionTest:Sprite = new Sprite();
   2:  
   3: //画一个三角形
   4: motionTest.graphics.beginFill(0x000000)
   5: motionTest.graphics.moveTo(50,0);
   6: motionTest.graphics.lineTo(0,100);
   7: motionTest.graphics.lineTo(100,100);
   8: addChild(motionTest);
   9:  
  10: //应用动画
  11: __animFactory_元件2_3.addTarget(motionTest, 2);
我们可以发现运行后将会在左上角出现一个黑色三角形,并且这个三角形模糊渐渐两次;