/// <summary> /// Scan the sprites to find those that contain the specified position. /// Returns an array of all matching sprites. /// </summary> /// <param name="testPosition"></param> public SpriteObject GetSpriteAtPoint(Vector2 testPosition) { SpriteObject spriteObj; SpriteObject ret = null; float lowestLayerDepth = float.MaxValue; // Loop for all of the SelectableSpriteObjects foreach (GameObjectBase obj in GameObjects) { // Is this a SpriteObject? if (obj is SpriteObject) { // Yes... Cast it to a SelectableSpriteObject spriteObj = (SpriteObject)obj; // Is its layerdepth the same or lower than the lowest we have seen so far? // If not, previously encountered objects are in front of this one // and so we have no need to check it. if (spriteObj.LayerDepth <= lowestLayerDepth) { // Is the point in the object? if (spriteObj.IsPointInObject(testPosition)) { // Mark this as the current frontmost object // and remember its layerdepth for future checks ret = spriteObj; lowestLayerDepth = spriteObj.LayerDepth; } } } } return(ret); }
/// <summary> /// Scan the sprites to find those that contain the specified position. /// Returns an array of all matching sprites. /// </summary> /// <param name="testPosition"></param> public SpriteObject[] GetSpritesAtPoint(Vector2 testPosition) { SpriteObject spriteObj; SpriteObject[] hits = new SpriteObject[GameObjects.Count]; int hitCount = 0; // Loop for all of the SelectableSpriteObjects foreach (GameObjectBase obj in GameObjects) { // Is this a SpriteObject? if (obj is SpriteObject) { // Yes... Cast it to a SelectableSpriteObject spriteObj = (SpriteObject)obj; // Is the point in the object? if (spriteObj.IsPointInObject(testPosition)) { // Add to the array hits[hitCount] = spriteObj; hitCount += 1; } } } // Trim the empty space from the end of the array Array.Resize(ref hits, hitCount); return(hits); }