Actionscript 3 在AS3中使用colormatrix着色-请帮助!

Actionscript 3 在AS3中使用colormatrix着色-请帮助!,actionscript-3,Actionscript 3,在我的太空游戏中,我遇到了一个巨大的问题,那就是给四处飞行的物体上色 当我射击并击中他们时,受影响的敌人会眨眼。图形是预渲染的(即,有一个旋转数组和函数,其中存储/计算对象的度数及其适当旋转,以获得更好的性能) 所以-我的想法是通过增加着色功能来增强旋转功能;但彩色和旋转物体应与正常旋转物体分开存放。为此,我制作了一个嵌套数组: 第一行中有一个对象的360个旋转图形,第二行中有一个旋转的彩色对象的360个图形 问题:着色可以工作,但不会旋转(始终为0度)。请帮帮我-我花了好几个小时才弄明白为什么

在我的太空游戏中,我遇到了一个巨大的问题,那就是给四处飞行的物体上色

当我射击并击中他们时,受影响的敌人会眨眼。图形是预渲染的(即,有一个旋转数组和函数,其中存储/计算对象的度数及其适当旋转,以获得更好的性能)

所以-我的想法是通过增加着色功能来增强旋转功能;但彩色和旋转物体应与正常旋转物体分开存放。为此,我制作了一个嵌套数组: 第一行中有一个对象的360个旋转图形,第二行中有一个旋转的彩色对象的360个图形

问题:着色可以工作,但不会旋转(始终为0度)。请帮帮我-我花了好几个小时才弄明白为什么它不起作用,所以我放弃了。如果有人能找到问题,那就太酷了!多谢各位

public function createRotationWithColorBlitArrayFromBD(sourceBitmapData:BitmapData, inc:int, offset:int = 0):Array
{
    trace("sourceBitmapData.width=" + sourceBitmapData.width);
    trace("sourceBitmapData.height=" + sourceBitmapData.height);
    tileList = [];
    tileListSec = [];
    levelArray = [];
    var rotation:int = offset; 

    while (rotation < (360 + offset))
    {
        var angleInRadians:Number = Math.PI * 2 * (rotation / 360);
        var rotationMatrix:Matrix = new Matrix();

        rotationMatrix.translate(-sourceBitmapData.width * .5, -sourceBitmapData.height * .5);
        rotationMatrix.rotate(angleInRadians);
        rotationMatrix.translate(sourceBitmapData.width * .5, sourceBitmapData.height * .5);

        var matrixImage:BitmapData = new BitmapData(sourceBitmapData.width, sourceBitmapData.height, true, 0x00000000);

        matrixImage.draw(sourceBitmapData, rotationMatrix);
        tileList.push(matrixImage.clone());

        var colorMatrix:ColorMatrixFilter = new ColorMatrixFilter (
                                [1, 0, 0, 0, 0,
                                 0, 0, 0, 0, 0,
                                 0, 0, 0, 0, 0,
                                 0, 0, 0, 1, 0]);

        matrixImage.applyFilter(sourceBitmapData, sourceBitmapData.rect, point0, colorMatrix);

        tileListSec.push(matrixImage.clone());

        rotation += inc;

        matrixImage.dispose();
        matrixImage = null;
        rotationMatrix = null;
    }

    levelArray = [tileList, tileListSec];
    return(levelArray);
}
公共函数createRotationWithColorBlitArrayFromBD(sourceBitmapData:BitmapData,inc:int,offset:int=0):数组
{
跟踪(“sourceBitmapData.width=“+sourceBitmapData.width”);
跟踪(“sourceBitmapData.height=“+sourceBitmapData.height”);
tileList=[];
tileListSec=[];
levelArray=[];
变量旋转:int=偏移量;
同时(旋转<(360+偏移))
{
var angleInRadians:Number=Math.PI*2*(旋转/360);
var旋转矩阵:矩阵=新矩阵();
rotationMatrix.translate(-sourceBitmapData.width*.5,-sourceBitmapData.height*.5);
旋转矩阵。旋转(角度半径);
rotationMatrix.translate(sourceBitmapData.width*.5,sourceBitmapData.height*.5);
var matrixImage:BitmapData=新的BitmapData(sourceBitmapData.width,sourceBitmapData.height,true,0x00000000);
matrixImage.draw(sourceBitmapData,rotationMatrix);
tileList.push(matrixImage.clone());
var colorMatrix:ColorMatrixFilter=新ColorMatrixFilter(
[1, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1, 0]);
applyFilter(sourceBitmapData、sourceBitmapData.rect、point0、colorMatrix);
tileListSec.push(matrixImage.clone());
轮换+=股份有限公司;
matrixImage.dispose();
matrixImage=null;
旋转矩阵=零;
}
levelArray=[tileList,tileListSec];
返回(levelArray);
}

当您要将过滤器应用于源图像的旋转版本时,您似乎正在将过滤器应用于源图像

如果仔细查看BitmapData文档,则“应用过滤器”功能将执行以下操作:

获取源图像和过滤器对象,并生成过滤后的图像

您的apply filter调用正在使用源图像的新的未旋转副本在draw()函数中执行的矩阵变换上进行写入。applyFilter()与draw类似,只是它应用了一个过滤器而不是转换矩阵。在本例中,将转换矩阵应用于
matrixImage.draw()
(旋转),然后使用
matrixImage.applyFilter()
(着色)写入该数据

解决方案 快速解决方案是进行以下更改:

matrixImage.applyFilter(matrixImage, matrixImage.rect, point0, colorMatrix);
这使matrixImage在成为旋转图像后将颜色过滤器应用于自身

顺便说一句:在这种情况下,我认为不需要有两个matrixImage对象的clone()。仅由applyFilter()生成的最后一个对象就足够了,因为它是源和过滤器的一体。但是你可能需要它们来做其他你还没有发布的游戏逻辑

注: 此解决方案的唯一问题是以下性能问题:(来自adobe docs for BitmapData.applyFilter())

如果BitmapData对象和指定为sourceBitmapData参数的对象是同一对象,则应用程序将使用该对象的临时副本来执行筛选。为了获得最佳性能,请避免这种情况


实例化这些临时副本需要大量的资源,因此只需安装一个缓冲区对象并重新使用它就可以更快。如果发现性能问题,最好创建第三个BitmapData对象作为源和筛选器之间的缓冲区。在这种情况下,您将有3个bitmapData对象,而不仅仅是
sourceBitmapData
matrixImage

当您要将过滤器应用于源图像的旋转版本时,您似乎正在将过滤器应用于源图像

如果仔细查看BitmapData文档,则“应用过滤器”功能将执行以下操作:

获取源图像和过滤器对象,并生成过滤后的图像

您的apply filter调用正在使用源图像的新的未旋转副本在draw()函数中执行的矩阵变换上进行写入。applyFilter()与draw类似,只是它应用了一个过滤器而不是转换矩阵。在本例中,将转换矩阵应用于
matrixImage.draw()
(旋转),然后使用
matrixImage.applyFilter()
(着色)写入该数据

解决方案 快速解决方案是进行以下更改:

matrixImage.applyFilter(matrixImage, matrixImage.rect, point0, colorMatrix);
这使matrixImage在成为旋转图像后将颜色过滤器应用于自身

顺便说一句:在这种情况下,我认为不需要有两个matrixImage对象的clone()。仅由applyFilter()生成的最后一个对象就足够了,因为它是源和过滤器的一体。但是你可能需要它们来做其他你还没有发布的游戏逻辑

注: 此解决方案的唯一问题是以下性能问题:(来自adobe docs for BitmapData.a)