开发者

Blending objects with eachother but not with background actionscript 3

I have a number of objects (represented as DisplayObjects) that i wish to blend with eachother.

However behind these objects there is a background that i do not want to involve in the blending.

So basically i want to blend these objects with eachother and afterwards use the result of this blending as a new DisplayObject (for example to put it on top of a randomly colored background).

So what i have is:

开发者_开发知识库
var obj1:DisplayObject = getFirstObj();
var obj2:DisplayObject = getSecObj();
var background:DisplayObject = getBackground();

obj1.blendMode = BlendMode.ADD;
obj2.blendMode = BlendMode.ADD;

A first attempt i tried was putting these objects into a common DisplayObjectContainer hoping that blending mode would only be relative to all objects contained by the same DisplayObjectContainer, but this does not seem to be the case.

var objectsPool:Sprite = new Sprite();
objectsPool.addChild( obj1 );
objectsPool.addChild( obj2 );

addChild( background );
addchild( objectsPool );

So that diddent get me anywhere. Any help is appreciated.

EDIT: changed DisplayObjectContainer to Sprite in the last code snippet


If you put the objects into a container, and remove it from the stage, you can then draw it with the BitmapData class and create a new Bitmap object representing the combination. This will have a transparent background, and it's blendMode will be normal, allowing you to use it on the background.

var obj1:DisplayObject = getFirstObj();
var obj2:DisplayObject = getSecObj();
var background:DisplayObject = getBackground();

obj1.blendMode = BlendMode.ADD;
obj2.blendMode = BlendMode.ADD;

var objectsPool:DisplayObjectContainer = new DisplayObjectContainer();
objectsPool.addChild( obj1 );
objectsPool.addChild( obj2 );

var bmd:BitmapData = new BitmapData(objectsPool.width,objectsPool.height,true,0);
bmd.draw(objectsPool);

var drawnObject:Bitmap = new Bitmap(bmd);

addChild( background );
addchild( drawnObject );

(untested code, good luck)


Rather going to the effort of drawing a Bitmap yourself, there are options that cause Flash to rasterize a layer and it's children automatically. Try:

container.cacheAsBitmap = true;

or try:

container.blendMode = "layer";

or try:

container.filters = [new GlowFilter(0,0,0,0)];

Any of those options should cause the children to render into a Bitmap under the hood, invalidating their individual blend modes/effects on the background.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜