views:

164

answers:

2

for example:

var mc:MovieClip=new MovieClip();

mc.graphics.beginFill(0x000000,0.5);
mc.graphics.drawRect(0,0,100,100);
mc.graphics.endFill();

mc.graphics.beginFill(0x000000,0.5);
mc.graphics.drawRect(0,0,100,100);
mc.graphics.endFill();

with this i will get a box with an alpha value darker than 0.5. I want a 0.5 alpha square without using something like:

var mc:MovieClip=new MovieClip();

mc.graphics.beginFill(0x000000,1);
mc.graphics.drawRect(0,0,100,100);
mc.graphics.endFill();

mc.graphics.beginFill(0x000000,1);
mc.graphics.drawRect(0,0,100,100);
mc.graphics.endFill();

var bmp:BitmapData=new BitmapData(100,100);
bmp.draw(mc);
var mc2:MovieClip=new MovieClip();
mc2.graphics.beginBitmapFill(bmp);
mc2.graphics.drawRect(0,0,100,100);
mc2.graphics.endFill();

obvoiusly i don´t want it for drawing squares, it´s for a shadow casting algorithm i´m working on.

A: 

Instead of using alpha ,you could use ColorTransform. With TweenMax , it's pretty easy but of course you could code your own solution.

var mc:MovieClip=new MovieClip();
mc.graphics.beginFill(0xffffff); mc.graphics.drawRect(0,0,100,100); mc.graphics.endFill();
TweenMax.to( mc , 0 , {colorTransform:{tint:0x000000 , tintAmount:.5}} );

mc.graphics.beginFill(0xffffff); mc.graphics.drawRect(0,0,100,100); mc.graphics.endFill();
TweenMax.to( mc , 0 , {colorTransform:{tint:0x000000 , tintAmount:.5}} );
PatrickS
hi, aplying a colorTransform didn´t work. mc.transform.colorTransform=new ColorTransform(1,1,1,.5); produces the same result as before... perhaps there is no way to change the way that flash manages alpha
s-kira
Doing it like this is the same as setting the alpha property to .5 , this is why I used TweenMax (greensock.com) as a shortcut to changing the amount of color.
PatrickS
mmh yes, you are right :) ... but i realized that rendering the entire sprite on a bitmap and then modify the alpha with copypixels and an alphabitmap is faster than render the sprite itself (my app is running at 2x zoom using a 375x275 canvas)
s-kira
A: 

Depending on what you are doing, you can just draw everything as alpha 1, set the alpha of the object itself to 0.5, and then use a LAYER blending mode which flattens the transparency before applying the alpha.

var mc:MovieClip = new MovieClip();
addChild(mc);

var sp:Shape;

sp = new Shape();
sp.graphics.beginFill(0x000000,1);
sp.graphics.drawRect(0,0,100,100);
sp.graphics.endFill();
mc.addChild(sp);

sp = new Shape();
sp.graphics.beginFill(0x000000,1);
sp.graphics.drawRect(50,50,100,100);
sp.graphics.endFill();
mc.addChild(sp);

mc.alpha = 0.5;
mc.blendMode = BlendMode.LAYER;

It's a bit convoluted, but you get the idea.

Using a bitmap would be the best for performance, though. Again, it depends on exactly what you have to implement and how.

Also, depending on what you have to draw, if you can rely on drawPath() instead, you could just use it with winding set to GraphicsPathWinding.NON_ZERO.

zeh
Wow!, yes, this is what i was looking for, but is pretty slow, 6 fps compared with the 35 fps of bitmapdata D:
s-kira
setting cacheAsBitmap on the parent will probably bring them up to par performance wise, unless you're rotating and scaling alot
grapefrukt