MatrixTurn源码阅读

时间:2021-11-01 15:38:12

在看cacheAsBitmap 相关资料时,找到bit101的一篇文章,http://www.bytearray.org/?p=290 全文如下:

One of the feature I would really love to see in a future release of the Flash Player is a native rasterizer for display objects. Imagine something like :

myMovieClip.rasterize = true;

This code would rasterize your DisplayObject as an animated PNG. It would bring crazy improvements for designers and coders who want to improve the rendering performance of any animation. This would be in fact the same behavior as the Banana Slice component I talked about.

When talking about this, some people could think "Well, you have cacheAsBitmap for that". The reason why such a feature would rock is mainly due to the fact that cacheAsBitmap is very dangerous. Well, it won't hurt you MatrixTurn源码阅读 , but it can definitely hurt your application performance.

As I said before, when cacheAsBitmap is set, the DisplayObject is cached as a bitmap on memory and the Flash Player is using this copy to render it on screen. The main problem that we have here is that if you do more than moving this DisplayObject on x and y, for each frame the Flash Player is updating the cached copy on memory before updating the screen. So if you apply any rotation, scale, alpha, or even if you have any key frames in your DisplayObject you will get performance decrease.

Hopefully, you can bypass this limitation by creating a BitmapData by yourself, draw the MovieClip on it, and pass it to multipe Bitmap instances. So let's take a simple example, in the folllowing movie, when you click on the "cacheAsBitmap" button, I create multiple instances of an Apple MovieClip, set cacheAsBitmap, and move them on screen :

var apple:Apple;

for (var i:int = 0; i< 100; i++)
{
apple = new Apple();

apple.cacheAsBitmap = true;

addChild ( apple );
}

When you click on the "draw" button, I first create an instance of the apple MovieClip, draw it on a BitmapData and then create Bitmap instances linked to the one and unique BitmapData instance :

var source:AppleSource = new AppleSource();

var buffer:BitmapData = new BitmapData ( source.width, source.height, true );

buffer.draw ( source );

var bitmapApple:BitmapApple;

for (var i:int = 0; i< 100; i++ )
{
bitmapApple = new BitmapApple( buffer );

addChild ( bitmapApple );
}

很不错的一篇文章,文章中又提到了Banana Slice,地址http://www.bytearray.org/?p=117

文章是建议adobe对movieclip做一些优化操作。在这篇文章的评论里,有人提到了MatrixTurn,就是这篇博客的重点了。

代码虽然不多,但写的实在复杂,花了整整一天才看懂大概。写了一些简单的笔记:

aGroupName
下标是自增的index
值是groupName

aGroup
下标是每个groupName所对应的index
值是 一个array
在这个array中,每一个元素又是一个array对象,代表state相关信息
即是和aGroupState是对应的
自array的信息如下:
0 = new Array
1 =

在0下标表示的array中,
其信息又是如下:
2 = sSnap
3 = bSmooth
9 = layer
5 = nBorder //这个位置和state貌似不对应
4 = totalFrames

aGroupValue
下标是每个groupName所对应的index
值是 一个array
在这个array中,
0 = convert 表示convert操作是否开始,这个是在convertGroup方法中被修改的,如果开始了那么再调用addState就会报错
1 = _bSmooth
2 = _sSnap
3 = new Array,存储这个groupName下的所有实例,这个是在convertGroup方法中被修改的
4 = _colorTransform
5 = _fctBitmapDataTransformation
6 = _bAutoFirstFrameConvert
9 = _nLayers
10 = _fctMovieClipTransformation
11 = 0 这个是在convertGroup方法中赋值的,代表已经完成initialisation的实例数,也是在convertGroup中修改的
即是这个array存放是groupName对应的mc在转换时的一些参数!!!

aGroupState
下标是每个groupName所对应的index
值是 一个array
在这个array中,每一个元素又是一个array对象代表一个state信息
即是说,每个groupName会对应多个state,这些state组合成一个array存在aGroupState中
每一个state信息又是通过array来表示的,具体如下:
0 = _state; 状态名
1 = _bSmooth;
2 = _sSnap;
3 = _nBorder;
4 = _colorTransform;
5 = _fctBitmapDataTransformation;
9 = _nLayers;
10 = _fctMovieClipTransformation;
7 = nLastState 这个是在linkState方法中赋值的
8 = _sFunction 这个是在addLoadState方法中赋值的

aTaskList
下标是每个groupName所对应的index
值是 一个array
在这个array中,
0 = _mcRef;对应的movieclip
1 = nId; groupName所对应的index
2 = new Array; groupName下的实例 在convertGroup中改变

aTaskListId
下标是自增的index
值是groupName所对应的index
表示即将要被convert的group

aWaitingListId
下标是自增的index
值是groupName所对应的index
表示在等待中的group

aTaskComplete

aGroupComplete

每次createGroup之后,nGroupId就表示新插入的group
这个时候,一般都是调用addState addLoadState方法,对这个group做补充操作
addState addLoadState会用nGroupId做验证,
换句话说,createGroup一定要遭addState addLoadState之前调用!
同理,addLoadState也最好是在addState之后调用,因为它可能也会用到lastState

每一个group至少需要一个state
不然convertGroup方法报错

state其实就是movieclip中的帧label

这个开源项目,核心思想就是把一个 movieclip转换成bitmap,它还支持movieclip的scale rotation 等操作。

还有一个亮点是,movieclip到bitmap的转换过程是分散到多帧中进行的,这样避免某一帧的执行时间过长。

再每次draw之后都判断本回合操作时间,如果超过指定的时间就暂定draw操作。

但之后是怎么再次启动的,我没有看懂,惭愧一下~

还有一点不太理解的是,在bitmap切换过程中,都是通过removechild addchild来实现的,为什么不直接更换bitmapdata属性的?

是因为要支持不同的effect吗?

另外,项目中也用到了Event.FRAME_CONSTRUCTED,之前没碰到过,所以查了下资料,

可以参考这篇文章http://hi.baidu.com/hihuanshi/item/fefc976e6a53650aa0cf0f6a

全文摘录如下:

做flash IDE 项目时,碰到了一种令我难以理解的事情,假设在舞台上有个影片剪辑mc,mc含有多帧,假设第二帧上事先放有显示对象childrenA,在舞台我写代码如下:

mc.gotoAndStop(2);

mc.childrenA.visible=false; 

   问题出现了,flash竟然给我报访问空对象的错误,但查实“childrenA”对象是好好滴待在那的,trace(mc.numChildren)有是和帧上对象数吻合的,难道说这两句代码不能同步进行?抱着怀疑的心态,经测试n次得出结果,现分享:

分析:MovieClip的帧显示对象的构造和帧代码是异步执行,所以才造成在第二帧代码访问帧上的对象为null的情况。 注:如果帧上对象都是小东西的话,可能就不报此错误了。

    解决方案:

mc.gotoAndStop(2);

mc.addEventListener(Event.FRAME_CONSTRUCTED, onFRAME_CONSTRUCTED); 

function onFRAME_CONSTRUCTED(e:Event):void

{
     
    if (mc.getChildAt(mc.numChildren - 1) == null) {

        return;

    }

   mc.removeEventListener(Event.FRAME_CONSTRUCTED, onFRAME_CONSTRUCTED);

   mc.childrenA.visible=false

}

关于FRAME_CONSTRUCTED的解释:在帧显示对象的构造函数运行之后但在帧脚本运行之前调度。如果播放头不移动,或者只
有一帧,则会继续以帧速率调度此事件。此事件为广播事件,这意味着具有注册了此事件的侦听器的所有显示对象都会调度此事
件。
通俗点解释:就是添加一个对象完成后就派发一下这个事件,我监听最后一个显示对象不为null的话,程序就可以顺利进行了……

可以参考http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#event:frameConstructed

MatrixTurn源码阅读的更多相关文章

  1. 【原】FMDB源码阅读(三)

    [原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...

  2. 【原】FMDB源码阅读(二)

    [原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...

  3. 【原】FMDB源码阅读(一)

    [原]FMDB源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 说实话,之前的SDWebImage和AFNetworking这两个组件我还是使用过的,但是对于 ...

  4. 【原】AFNetworking源码阅读(六)

    [原]AFNetworking源码阅读(六) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这一篇的想讲的,一个就是分析一下AFSecurityPolicy文件,看看AF ...

  5. 【原】AFNetworking源码阅读(五)

    [原]AFNetworking源码阅读(五) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中提及到了Multipart Request的构建方法- [AFHTTP ...

  6. 【原】AFNetworking源码阅读(四)

    [原]AFNetworking源码阅读(四) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇还遗留了很多问题,包括AFURLSessionManagerTaskDe ...

  7. 【原】AFNetworking源码阅读(三)

    [原]AFNetworking源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇的话,主要是讲了如何通过构建一个request来生成一个data tas ...

  8. 【原】AFNetworking源码阅读(二)

    [原]AFNetworking源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中我们在iOS Example代码中提到了AFHTTPSessionMa ...

  9. 【原】AFNetworking源码阅读(一)

    [原]AFNetworking源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 AFNetworking版本:3.0.4 由于我平常并没有经常使用AFNetw ...

随机推荐

  1. C语言 &&num;183&semi; 查找整数 &&num;183&semi; 基础练习

    问题描述 给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个. 输入格式 第一行包含一个整数n. 第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000. 第三行包含一个 ...

  2. Deploying JRE &lpar;Native Plug-in&rpar; for Windows Clients in Oracle E-Business Suite Release 12 &lpar;文档 ID 393931&period;1&rpar;

    In This Document Section 1: Overview Section 2: Pre-Upgrade Steps Section 3: Upgrade and Configurati ...

  3. &period;NET MVC AjaxHelper

    我们首先必须开启 非入侵式 Ajax:导入Jquery和unobtrusiveAjax文件 已经默认开启客户端验证 和 非侵入式js <add key="ClientValidatio ...

  4. 资源&colon; CustomResource&comma; ResourceDictionary&comma; 加载外部的 ResourceDictionary 文件

    CustomResource ResourceDictionary 加载外部的 ResourceDictionary 文件 示例1.演示“CustomResource”相关知识点Resource/Cu ...

  5. ASP&period;net的url重写

    http://blog.csdn.net/windok2004/article/details/2432691 1. 有关于URL的重写,本文也只是拿来主意.相继有MS的组件“URLRewriter” ...

  6. Android通过HttpURLConnection链接到网络,并获取网络数据

    1.判断网络是否连接 private void networkIsconnected(String str){ ConnectivityManager connMgr = (ConnectivityM ...

  7. saltstack配置安装的一些关键步骤及安装时各种报错的分析

    以下其他仅做参考,官方网址才是安装重点:http://docs.saltstack.cn/topics/installation/rhel.html 与安装相关的一些文档或资料: 一.linux服务器 ...

  8. 如何用C&num;语言构造蜘蛛程序

    "蜘蛛"(Spider)是Internet上一种很有用的程序,搜索引擎利用蜘蛛程序将Web页面收集到数据库,企业利用蜘蛛程序监视竞争对手的网站并跟踪变动,个人用户用蜘蛛程序下载We ...

  9. leetcode--009 Linked List Cycle I

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZgAAACACAIAAAC5q+hAAAAJ+UlEQVR4nO2dwbXrKBJAOyelRShEQw

  10. 初识Haskell

    在COMP30026 Models of Computation中接触了新的编程语言Haskell,一个之前听都没有听过的语言,在此记录关于Haskell的一些最基本概念. 1.Haskell是一个函 ...