libspark.swfassist的体会

来源:互联网 发布:sql字段拼接 编辑:程序博客网 时间:2024/06/16 20:19
 介绍:
swfassist是生成swf文件的一个库,好像是小鬼子(无种族歧视涵义)开源的,可以通过代码生成一个swf,而不用通过AS或fB等开发工具发布(其实原理都一样),可以通过AIR生成swf,而无论用户机子上是否有flash、FB等工具(废话

这个库资料之少,文档之不明确,真是很让人震精,而且网络上介绍也比较少。

不废话先看代码
  1. var swf:SWF = new SWF();
  2.                        
  3. swf.header.version = 9;
  4. swf.header.frameSize.xMax = 500;
  5. swf.header.frameSize.yMax = 375;
  6. swf.header.frameRate = 24;
  7. swf.header.numFrames = 0;
  8.                        
  9. var bgColor:SetBackgroundColor = new SetBackgroundColor();
  10. bgColor.backgroundColor.fromUint(0x000000);
  11. swf.tags.addTag(bgColor);

  12. for (var i:uint = 0; i < files.length; ++i)
  13. {
  14.         var file:File = File(files[i]);
  15.         var defineJPEG:DefineBitsJPEG2 = new DefineBitsJPEG2();
  16.         defineJPEG.characterId = (i * 2) + 1;
  17.                                
  18.         var fileStream:FileStream = new FileStream();
  19.         fileStream.open(file, FileMode.READ);
  20.         fileStream.readBytes(defineJPEG.jpegData);
  21.         fileStream.close();
  22.         fileStream = null;
  23.                
  24.         swf.tags.addTag(defineJPEG);
  25.                                
  26.         var defineShape:DefineShape = new DefineShape();
  27.         defineShape.shapeId = (i * 2 + 1) + 1;
  28.         defineShape.shapeBounds.xMax = 500;
  29.         defineShape.shapeBounds.yMax = 375;
  30.         var fillStyle:FillStyle = new FillStyle();
  31.         fillStyle.fillStyleType = FillStyleTypeConstants.CLIPPED_BITMAP_FILL;
  32.         fillStyle.bitmapId = (i * 2) + 1;
  33.         fillStyle.bitmapMatrix.hasScale = true;
  34.         fillStyle.bitmapMatrix.scaleX = 20;
  35.         fillStyle.bitmapMatrix.scaleY = 20;
  36.         var shape:ShapeWithStyle = defineShape.shapes;
  37.         shape.fillStyles.fillStyles.push(fillStyle);
  38.         var r1:StyleChangeRecord = new StyleChangeRecord();
  39.         r1.fillStyle0 = 1;
  40.         r1.moveDeltaX = 0;
  41.         r1.moveDeltaY = 0;
  42.         r1.stateFillStyle0 = true;
  43.         r1.stateMoveTo = true;
  44.         var r2:StraightEdgeRecord = new StraightEdgeRecord();
  45.         r2.verticalLine = true;
  46.         r2.deltaY = 375;
  47.         var r3:StraightEdgeRecord = new StraightEdgeRecord();
  48.         r3.horizontalLine = true;
  49.         r3.deltaX = 500;
  50.         var r4:StraightEdgeRecord = new StraightEdgeRecord();
  51.         r4.verticalLine = true;
  52.         r4.deltaY = -375;
  53.         var r5:StraightEdgeRecord = new StraightEdgeRecord();
  54.         r5.horizontalLine = true;
  55.         r5.deltaX = -500;
  56.         shape.shapeRecords.push(r1, r2, r3, r4, r5);
  57.                                
  58.         swf.tags.addTag(defineShape);
  59.                                
  60.         var placeObject:PlaceObject2 = new PlaceObject2();
  61.         placeObject.characterId = (i * 2 + 1) + 1;
  62.         placeObject.depth = i + 1;
  63.         placeObject.hasCharacter = true;
  64.                                
  65.         swf.tags.addTag(placeObject);
  66.                                
  67.         for (var fadeIn:uint = 0; fadeIn < 20; ++fadeIn) {
  68.                 var fadeObject:PlaceObject2 = new PlaceObject2();
  69.                 fadeObject.depth = i + 1;
  70.                 fadeObject.isMove = true;
  71.                 fadeObject.hasColorTransform = true;
  72.                 fadeObject.colorTransform.hasMultiplication = true;
  73.                 fadeObject.colorTransform.redMultiplication = 256;
  74.                 fadeObject.colorTransform.greenMultiplication = 256;
  75.                 fadeObject.colorTransform.blueMultiplication = 256;
  76.                 fadeObject.colorTransform.alphaMultiplication = (((1.0 / 20) * fadeIn) * 256);
  77.                 swf.tags.addTag(fadeObject);
  78.                 swf.tags.addTag(new ShowFrame());
  79.                 swf.header.numFrames++;
  80.         }
  81.                                
  82.                 if (i > 0) {
  83.                         var removeObject:RemoveObject2 = new RemoveObject2();
  84.                         removeObject.depth = (i - 1) + 1;
  85.                         swf.tags.addTag(removeObject);
  86.                         }
  87.                                
  88.                         for (var n:uint = 0; n < (24 * 3); ++n) {
  89.                                 swf.tags.addTag(new ShowFrame());
  90.                                 swf.header.numFrames++;
  91.                         }
  92.                 }
  93.                        
  94.                 var swfBytes:ByteArray = new ByteArray();
  95.                        
  96.                 new SWFWriter().writeSWF(new ByteArrayOutputStream(swfBytes), new WritingContext(), swf);
  97.                        
  98.                 var swfStream:FileStream = new FileStream();
  99.                 swfStream.open(File.desktopDirectory.resolvePath('slideshow.swf'), FileMode.WRITE);
  100.                 swfStream.writeBytes(swfBytes);
  101.                 swfStream.close();
复制代码
这段东西是库自带的一段demo,网络上的例子基本是改至这段东西。

下面是我知道的一些东西:

SWF这个类代表将要生成的SWF,最好这个文件会以二进制流方式生成一个swf文件。所以是必须的。
然后 参数代表
【head】文件头包涵基本swf文件属性:
--version  :版本,此swf支持最低版本的播放器
--frameSize :stage的大小,数据类型是一个flash.geom.Rectangle类似的一个矩形数据类型,是一个帧(尺码、大小)字段(FrameSize field)会有这些属性
Xmin、Ymin、还有Xmax和Ymax(成员声明宽度和高度),后面我们还会见到。
--frameRate  :帧频

所以
frameSize.xMax = 500;
frameSize.yMax = 375
frameRate = 24
实际是设置了这个生成这个swf文件的场景宽高,就如:[SWF(width = 500, height = 375, frameRate = 24)]

--isCompressed :是否是压缩格式,设置后生成swf会被写入0x46, 0x57, 0x53(未压缩)或0x46,0x57, 0x43(压缩)

下面是具体的tag,大家都知道或将知道,swf文件是tag叠加的结果,就是下一个tag会对上一个相关的tag有依赖,简单说比如你要先定义一个叫“lewis_mc”的元件后,才能对这个元件进行控制(这是废话,但很重要
原文是这样的:Flash Player processes all the tags in a SWF file until a ShowFrame tag is encountered. At this point, the displaylist is copied to the screen and Flash Player is idle until it is time to process the next frame.The contents of the first frame are the cumulative effect of performing all the control tag operations before the first ShowFrame tag. The contents of the second frame are the cumulative effect of performing all the control tag operations from the beginning of the file to the second ShowFrame tag, and so on.
  1. var bgColor:SetBackgroundColor = new SetBackgroundColor();
  2.                         bgColor.backgroundColor.fromUint(0x000000);
  3.                         swf.tags.addTag(bgColor);
复制代码
这句话实际上完成了我们代码里设置场景背景的颜色的工作,fromUint这东西会把你的十六进制的颜色通过右移">>"分离成RGB单独色彩数据,反正改fromUint里面的值就好了。接着我们会用到很重要的一个API  :tags.addTag(bgColor),addTag()是写入tag到swf的意思,这是第一个我们加入的tag,如果要自定义一个tag必须继承于AbstractTag这个抽象类,其实tag可以自定义,因为FP发现不能解释的tag 的时候是会放过的,所以可以随便增加tag而不破坏这个swf,有些直接改swf文件个工具就是这样的原理。

接下来是功能核心了,就是如果导入一张图片,并且生成一个包涵这种图片的swf文件。
  1. var defineJPEG:DefineBitsJPEG2 = new DefineBitsJPEG2();
  2.                                 defineJPEG.characterId = (i * 2) + 1;
  3.                                
  4.                                 var fileStream:FileStream = new FileStream();
  5.                                 fileStream.open(file, FileMode.READ);
  6.                                 fileStream.readBytes(defineJPEG.jpegData);
  7.                                 fileStream.close();
  8.                                 fileStream = null;
  9.                                
  10.                                 swf.tags.addTag(defineJPEG);
  11.                                
  12.                                 var defineShape:DefineShape = new DefineShape();
  13.                                 defineShape.shapeId = (i * 2 + 1) + 1;
  14.                                 defineShape.shapeBounds.xMax = 500;
  15.                                 defineShape.shapeBounds.yMax = 375;
  16.                                 var fillStyle:FillStyle = new FillStyle();
  17.                                 fillStyle.fillStyleType = FillStyleTypeConstants.CLIPPED_BITMAP_FILL;
  18.                                 fillStyle.bitmapId = (i * 2) + 1;
  19.                                 fillStyle.bitmapMatrix.hasScale = true;
  20.                                 fillStyle.bitmapMatrix.scaleX = 20;
  21.                                 fillStyle.bitmapMatrix.scaleY = 20;
  22.                                 var shape:ShapeWithStyle = defineShape.shapes;
  23.                                 shape.fillStyles.fillStyles.push(fillStyle);
  24.                                 var r1:StyleChangeRecord = new StyleChangeRecord();
  25.                                 r1.fillStyle0 = 1;
  26.                                 r1.moveDeltaX = 0;
  27.                                 r1.moveDeltaY = 0;
  28.                                 r1.stateFillStyle0 = true;
  29.                                 r1.stateMoveTo = true;
  30.                                 var r2:StraightEdgeRecord = new StraightEdgeRecord();
  31.                                 r2.verticalLine = true;
  32.                                 r2.deltaY = 375;
  33.                                 var r3:StraightEdgeRecord = new StraightEdgeRecord();
  34.                                 r3.horizontalLine = true;
  35.                                 r3.deltaX = 500;
  36.                                 var r4:StraightEdgeRecord = new StraightEdgeRecord();
  37.                                 r4.verticalLine = true;
  38.                                 r4.deltaY = -375;
  39.                                 var r5:StraightEdgeRecord = new StraightEdgeRecord();
  40.                                 r5.horizontalLine = true;
  41.                                 r5.deltaX = -500;
  42.                                 shape.shapeRecords.push(r1, r2, r3, r4, r5);
  43.                                
  44.                                 swf.tags.addTag(defineShape);

  45. var placeObject:PlaceObject2 = new PlaceObject2();
  46.                                 placeObject.characterId = (i * 2 + 1) + 1;
  47.                                 placeObject.depth = i + 1;
  48.                                 placeObject.hasCharacter = true;
  49.                                
  50.                                 swf.tags.addTag(placeObject);
复制代码
var defineJPEGefineBitsJPEG2 = new DefineBitsJPEG2();
这个tag表示有渐变的JPG
===============================================================
defineJPEG.characterId = (i * 2) + 1;
关键,这里要解释一下

在swf文件里tag有俩类型Definition Tags(定义类型标签)和 Control Tags(控制类型标签)

【定义类型标签】:简单说就是图片,声音,文字等等资源型的东西,相当于我们flash里的库文件就对了,所以他可以很复杂,资源可以嵌套,但因为是库文件,所以他们是不会被FP被渲染出来的,这个库专业的名字叫:Dictionary 字碘

【控制类型标签】:简单的说就是控制库文件的,他来控制如何,何时渲染我们的资源
所以,每个在字典里的资源必须有一个唯一的标示,让控制类型标签调用。
而characterId就是字典标签,每个资源必须有并且有唯一的一个characterId,和库里定义一个Linkage一样。
注意必须是唯一的,的,的,的……(回音)

所以DefineBitsJPEG2 是一个【定义类型标签】
说完继续代码
  1. var fileStream:FileStream = new FileStream();
  2.                                 fileStream.open(file, FileMode.READ);
  3.                                 fileStream.readBytes(defineJPEG.jpegData);
  4.                                 fileStream.close();
  5.                                 fileStream = null;
复制代码
这是AIR的打开文件二进制流读入一个jpg文件不多说。

swf.tags.addTag(defineJPEG);加入这个图片的tag,里面包含图片的二进制信息,想象相当于在库里边有一张图片了。
  1. var defineShape:DefineShape = new DefineShape();
  2.                                 defineShape.shapeId = (i * 2 + 1) + 1;
  3.                                 defineShape.shapeBounds.xMax = 500;
  4.                                 defineShape.shapeBounds.yMax = 375;
复制代码
建立一个用来放置我们的图片,xMax、yMax 代表是一个帧(尺码、大小)字段(FrameSize field),代表这个shape的宽高。这段代码开始,直到swf.tags.addTag(defineShape);
这句,是在定义一个【控制类型标签】,用来在字典中提取刚才那种图,渲染到场景上。(冗长吧~~
  1. var tagFS:FillStyle = new FillStyle();  
  2.                         tagFS.color.fromUint(0xff000000);  
  3.                         tagFS.fillStyleType = 67;  
  4.                         tagFS.bitmapId = (_frame-1)*2+1;  
  5.                         tagFS.bitmapMatrix.hasScale = true;  
  6.                         tagFS.bitmapMatrix.scaleX = 20;  
  7.                         tagFS.bitmapMatrix.scaleY = 20;
复制代码
这个标签是定义渲染的一些东西,scaleX = scaleY = 20 这句是必须要的,意思是不缩放,有兴趣可以调30、40试试就知道。tagFS.bitmapMatrix中还有俩个属性translateX、translateY这是图片的偏移量,如果你不想图片以父容器(0,0)点渲染。就要改变它们了。

var shape:ShapeWithStyle = defineShape.shapes;
shape.fillStyles.fillStyles.push(fillStyle);

把我们的defineShape和fillStyle加到一个Shape【控制类型标签】里。

关键就是这个Shape控制标签了
  1. shape.fillStyles.fillStyles.push(fillStyle);
  2.                                 var r1:StyleChangeRecord = new StyleChangeRecord();
  3.                                 r1.fillStyle0 = 1;
  4.                                 r1.moveDeltaX = 0;
  5.                                 r1.moveDeltaY = 0;
  6.                                 r1.stateFillStyle0 = true;
  7.                                 r1.stateMoveTo = true;
  8.                                 var r2:StraightEdgeRecord = new StraightEdgeRecord();
  9.                                 r2.verticalLine = true;
  10.                                 r2.deltaY = 375;
  11.                                 var r3:StraightEdgeRecord = new StraightEdgeRecord();
  12.                                 r3.horizontalLine = true;
  13.                                 r3.deltaX = 500;
  14.                                 var r4:StraightEdgeRecord = new StraightEdgeRecord();
  15.                                 r4.verticalLine = true;
  16.                                 r4.deltaY = -375;
  17.                                 var r5:StraightEdgeRecord = new StraightEdgeRecord();
  18.                                 r5.horizontalLine = true;
  19.                                 r5.deltaX = -500;
  20.                                 shape.shapeRecords.push(r1, r2, r3, r4, r5);
复制代码
我们看到最后这句shape.shapeRecords.push(r1, r2, r3, r4, r5);他定义这个Shape渲染的位置!!
而最经常要改动是这段:

var r1:StyleChangeRecord = new StyleChangeRecord();
                                r1.fillStyle0 = 1;
                                r1.moveDeltaX = 0;
                                r1.moveDeltaY = 0;
                                r1.stateFillStyle0 = true;
                                r1.stateMoveTo = true;
他定义了这个Shape类似于初始点的东西,就是MC左上角的那个点,定义这个店到底在哪里?

其中moveDeltaX ,moveDeltaY 就是这个shape的点的左边,以场景最左上角(0,0)算起。
但如果你想定义我们这个图片出现在(3,3)这个点
只是这样:

r1.moveDeltaX = 3;
r1.moveDeltaY = 3;
是不行的。因为shape是移动过去了,但你的图片还是在(0,0)这个点……

那怎么办?
刚才我们知道tagFS.bitmapMatrix有translateX、translateY属性我们要改变这俩个值。
tagFS.bitmapMatrix.translateX = 3;
tagFS.bitmapMatrix.translateY = 3;
这样才完成了一个图片的移动。

不过这只是我的方法而已,不知道那位大神可以出来指点一下,我先谢谢了~~

好,最后,
  1. var placeObject:PlaceObject2 = new PlaceObject2();
  2.                                 placeObject.characterId = (i * 2 + 1) + 1;
  3.                                 placeObject.depth = i + 1;
  4.                                 placeObject.hasCharacter = true;
  5.                                
  6.                                 swf.tags.addTag(placeObject);
复制代码

必须要有的。他代表我们图片一些属性,比如深度、旋转(旋转竟然在这里定义……)、元件上的代码(as2.0好像是onClipEvent开头)这样吧他加入他tag里边去。就OK了。

 

  zhuanzai :http://bbs.9ria.com/thread-85155-1-1.html

 

 

 

SWFAssist:

http://hi.baidu.com/%BA%DA%F2%E1/blog/item/f43945dac1a6db3e33fa1cbd.html

http://privatefunction.net/tag/swfassist/

http://vanillaloveyou.wordpress.com/2008/02/14/swfassist-%E6%B7%BB%E5%8A%A0%E5%B9%B6%E4%BF%9D%E5%AD%98mp3%E5%AE%9E%E9%AA%8C/