Created
April 19, 2012 17:02
-
-
Save PrimaryFeather/2422317 to your computer and use it in GitHub Desktop.
Simple Sprite subclass with a rectangular mask in stage coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Daniel,
I see the ClippedSprite hasn't been updated for 7 months. Is it still compatible with latest Starling?
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
Hi - Thanks for this extension. Useful!
Looks like current implementation doesn't support scale -- if you scale the ClippedSprite, the clipping rect doesn't react as you'd expect (it stays the same size). Children sprites of the ClippedSprite, however, scale as you'd expect.
I'm wondering if translating the desired scale to x and y values and then updating the clipRect is ideal, or if perhaps there's a better way like using a matrix or just resetting the cliprect after the scale is complete - even though visually this wouldn't look correct during the scale.
I'll run some tests and will report back when / if I find a path. Thanks.