开发者

Setting mask for sprite

So i have this sprite that is say arbitrarily 100 x 100 which is added to the stage. Now when content is added to this sprite the sprite expands automatically as it should. I dont want this to happen, what i want is to set a defined boundary i.e. 100 x 100, if too much content is added i can be able to scroll it. My problem is i cannot add a mask to the stage like the conventional way because i have many other boxes like this that vary in dimensions and number. I found a way i can do this but its very inefficient, what i did is i created a movieclip filled it say 100x100 with some color then instantiated it. Next i added the sprite and if content overlaps it wont be visible, only 100x100. I sort of stumbled on this and dont know exactly why this works. Any help is well appreciated, thank you.

Here is the code.

//a movieclip with its linkage called containerMasker has a blue square that serves as the mask

var myMasker = new containerMasker(); //the mask
var myContainer = new Sprite(); //the content be开发者_运维问答ing masked
//x and y coordinates fixed for both items, width also but no height

myMasker.addChild(myContainer);
addChild(myMasker);


here's the standard way

myContainer.mask = myMasker;
addChild(myContainer);
addChild(myMasker);

now, if you need to do this with many boxes you could create a class to handle it

public class MaskedItem extends MovieClip
{
  public function MaskedItem(params:Object)
  {  
      var masker:Sprite = new Sprite();
      masker.graphics.beginFill(0);
      masker.graphics.drawRect( 0 , 0 , params.maskWidth , params.maskHeight );
      masker.graphics.endFill();

      var mc:MovieClip = params.displayObject as MovieClip;
      mc.mask = masker;

      addChild( masker );
      addChild( mc );
  }
}

then you could do

 var mc1:MovieClip = new MaskedItem({maskWidth:100, maskHeight:100 , displayObject:mc1} );

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜