views:

38

answers:

2

How is cropping/clipping accomplished on a Sprite in Flex?

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="Init()">
  <mx:Script>
    <![CDATA[
      public function Init():void {
        var spr:Sprite=new Sprite();
        uic.addChild(spr);
        uic.graphics.lineStyle(2,0);
        uic.graphics.moveTo(22, 22);
        uic.graphics.lineTo(2222, 2222);
      }
    ]]>
 </mx:Script>
 <mx:Panel title="StackOverflow">
    <mx:UIComponent width="200" height="200" id="uic"/>
  </mx:Panel>
</mx:WindowedApplication>

Notice that the lineTo completely leaves the UIComponent and Panel.

How can I cause my UIComponent or Sprite, Or Panel for that matter, to be cropped/clipped? alt text

I realize I could just change the hard-coded 2222's to something more reasonable, but I need a generalized solution to this, since the actual project doesn't involve hard-coded values that I can alter, but works with dynamic data.

+1  A: 

Use a mask.

var mask:Shape = new Shape();
with(mask.graphics)
{
    beginFill(0xFFFFFF, 1); // white, opaque
    drawRect(0, 0, width, height);
    endFill();
}
uic.mask = mask;
Mahir
@Mahir. I agree, use a mask. But please don't use with ;)
Juan Pablo Califano
Why don't use "with"?
TandemAdam
Okay, this worked. Anybody else who needs this should also know that you have to add the mask as a child of the component being masked, or it won't work.
Joshua
+1  A: 

You should also try using scrollRect, this will be faster in performance than a mask. Introduction to the scrollRect from Grant Skinner.

stickupkid