Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Created April 19, 2012 17:02
Show Gist options
  • Save PrimaryFeather/2422317 to your computer and use it in GitHub Desktop.
Save PrimaryFeather/2422317 to your computer and use it in GitHub Desktop.
Simple Sprite subclass with a rectangular mask in stage coordinates
package starling.extensions
{
import flash.display3D.Context3D;
import flash.geom.Point;
import flash.geom.Rectangle;
import starling.core.RenderSupport;
import starling.core.Starling;
import starling.display.DisplayObject;
import starling.display.Sprite;
import starling.errors.MissingContextError;
public class ClippedSprite extends Sprite
{
private var mClipRect:Rectangle;
public override function render(support:RenderSupport, alpha:Number):void
{
if (mClipRect == null) super.render(support, alpha);
else
{
var context:Context3D = Starling.context;
if (context == null) throw new MissingContextError();
support.finishQuadBatch();
support.scissorRectangle = mClipRect;
super.render(support, alpha);
support.finishQuadBatch();
support.scissorRectangle = null;
}
}
public override function hitTest(localPoint:Point, forTouch:Boolean=false):DisplayObject
{
// without a clip rect, the sprite should behave just like before
if (mClipRect == null) return super.hitTest(localPoint, forTouch);
// on a touch test, invisible or untouchable objects cause the test to fail
if (forTouch && (!visible || !touchable)) return null;
if (mClipRect.containsPoint(localToGlobal(localPoint)))
return super.hitTest(localPoint, forTouch);
else
return null;
}
public function get clipRect():Rectangle { return mClipRect; }
public function set clipRect(value:Rectangle):void
{
if (value)
{
if (mClipRect == null) mClipRect = value.clone();
else mClipRect.setTo(value.x, value.y, value.width, value.height);
}
else mClipRect = null;
}
}
}
@skolesnyk
Copy link

Daniel,

I see the ClippedSprite hasn't been updated for 7 months. Is it still compatible with latest Starling?

@zmarkan
Copy link

zmarkan commented Sep 25, 2013

As of Starling 1.4 there is no need for ClippedSprite anymore, as Sprite class features a clipRect parameter that does the same thing. See http://gamua.com/blog/2013/09/starling-14/ for more info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment