Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 mask weird result

I'm playing around with dynamic masking for part of a puzzle game I'm making.

I've got a test puzzle with 6 pieces. The puzzle exists out of 3 layers:

  • black shape = this is where you put your pieces
  • finished pieces = this is a layer showing the combined result of found pieces
  • loose pieces = can be moved into place.

The black shape is no problem, that's just a simple color transform on the result sprite.

When I combine the finished pieces in one sprite, I notice smaller than hairline gaps between the pieces. This doesn't look too good, so I've been thinking of a way around this:

One way I thought of was to put a mask on a complete result sprite so that only the found pieces are visible. I would add a 1px border around the pieces to avoid the hairline gaps.

So, I started playing around with masks:

// test
var test: Sprite = new TestSprite() as Sprite;
test.x = test.y = 100;
addChild(test);

// puzzle pieces            
var pieces: Vector.<Sprite> = new Vector.<Sprite>;
pieces.push(new TurtlePiece1());
pieces.push(new TurtlePiece2());
//pieces.push(new TurtlePiece3());
//pieces.push(new TurtlePiece4());
pieces.push(new TurtlePiece5());
pieces.push(new TurtlePiece6());

// finished locations for each piece
var points: Vector.<Point> = new Vector.<Point>;
points.push(new Point(0.3, 7.25));
points.push(new Point(110.35, 0));
//points.push(new Point(98.25, 52.6));
//points.push(new Point(23.95, 69.30));
points.push(new Point(157.25, 61.95));
points.push(new Point(146.7, 100.70));

var mask: Sprite = new Sprite();
for (var i: int = 0; i < pieces.length; i++) {
    pieces[i].x = points[i].x;
    pieces[i].y = points[i].y;
    mask.addChild(pieces[i]);
}
test.mask = mask;

The full shape and the mask shape look like this:

full image and mask shape

After applying the mask it looks like this:

masked image

I have tried caching as bitmap, without result. Anyone have an idea what the problem could be?

Tnx in advance,

With kind regards, Jeroen

like image 1000
Jeroen Avatar asked Nov 20 '25 09:11

Jeroen


1 Answers

i see what you're attempting to do but i'm not sure why it's not working for you. i've created a similar program an it works as expected:

//Imports
import flash.display.Shape;
import flash.display.Sprite;

//Draw Background Rect
var backgroundRect:Shape = new Shape();
backgroundRect.graphics.beginFill(0x000000, 1.0);
backgroundRect.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
backgroundRect.graphics.endFill();

addChild(backgroundRect);

//Build Mask From Circles
var backgroundMask:Sprite = new Sprite();

var circleA:Shape = circle(50, 0xFF0000);
circleA.x = 50;
circleA.y = 50;

var circleB:Shape = circle(50, 0x00FF00);
circleB.x = 100;
circleB.y = 50;

var circleC:Shape = circle(50, 0x0000FF);
circleC.x = 150;
circleC.y = 75;

backgroundMask.addChild(circleA);
backgroundMask.addChild(circleB);
backgroundMask.addChild(circleC);

addChild(backgroundMask);

//Assign Mask
backgroundRect.mask = backgroundMask;

//Create Circle
function circle(radius:uint, color:uint):Shape
{
    var result:Shape = new Shape();
    result.graphics.beginFill(color, 1.0);
    result.graphics.drawCircle(0, 0, radius);
    result.graphics.endFill();

    return result;
}

enter image description here

the only thing i can think of that the pieces you are adding to the mask sprite are overriting each other, similarly to what happens when you overlap two or more shapes within a single graphics call:

//Imports
import flash.display.Shape;
import flash.display.Sprite;

//Draw Circle
var circleA:Shape = circle(50, 0xFF0000);
circleA.x = 50;
circleA.y = 50;

addChild(circleA);

//Create Circle
function circle(radius:uint, color:uint):Shape
{
    var result:Shape = new Shape();
    result.graphics.beginFill(color, 1.0);
    result.graphics.drawCircle(0, 0, radius);
    result.graphics.drawCircle(50, 50, radius);
    result.graphics.endFill();

    return result;
} 

enter image description here

like image 72
Chunky Chunk Avatar answered Nov 23 '25 01:11

Chunky Chunk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!