Skip to content

Instantly share code, notes, and snippets.

@shamruk
Created May 24, 2014 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamruk/0ae1431244dd8b4e706d to your computer and use it in GitHub Desktop.
Save shamruk/0ae1431244dd8b4e706d to your computer and use it in GitHub Desktop.
Starling getObjectsUnderPoint replacement
package {
import flash.geom.Matrix;
import flash.geom.Point;
import starling.display.DisplayObject;
import starling.display.DisplayObjectContainer;
import starling.display.Sprite;
import starling.utils.MatrixUtil;
public class ObjectsUnderPointUtil {
private static const sHelperMatrix : Matrix = new Matrix();
private static const sHelperPoint : Point = new Point();
/**
/* It is a getObjectsUnderPoint replacement
/*
/* Not done:
/* 1) ignores touchGroups for now
/* 2) stage is not included, and not checked if got in stage
*/
public static function findObjectsUnderPoint(display : DisplayObject, localPoint : Point, forTouch : Boolean, to : Vector.<DisplayObject>) : void {
if (forTouch && (!display.visible || !display.touchable)) {
return;
}
if (display is DisplayObjectContainer) {
if (display is Sprite) {
var sprite : Sprite = display as Sprite;
if (sprite.clipRect && !sprite.clipRect.containsPoint(localPoint)) {
return;
}
}
const container : DisplayObjectContainer = DisplayObjectContainer(display);
const localX : Number = localPoint.x;
const localY : Number = localPoint.y;
const numChildren : int = container.numChildren;
for (var i : int = numChildren - 1; i >= 0; --i) {
const child : DisplayObject = container.getChildAt(i);
container.getTransformationMatrix(child, sHelperMatrix);
MatrixUtil.transformCoords(sHelperMatrix, localX, localY, sHelperPoint);
findObjectsUnderPoint(child, sHelperPoint, forTouch, to);
}
} else if (display.hitTest(localPoint, forTouch)) {
to.push(display);
}
}
// /** more reliable but slower */
//
// private static const UNTOUCHED : Vector.<DisplayObject> = new Vector.<DisplayObject>();
// function {
// var displayObject : DisplayObject;
// while (displayObject = root.hitTest(POINT, true)) {
// displayObject.touchable = false;
// UNTOUCHED.push(displayObject);
// // do smth
// }
// for each(displayObject in UNTOUCHED) {
// displayObject.touchable = true;
// }
// UNTOUCHED.length = 0;
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment