Bläddra i källkod

prevent getBounds() with a different scene (not supported) + fix some issues when getScene is null

Nicolas Cannasse 11 år sedan
förälder
incheckning
66df2b4b01
1 ändrade filer med 21 tillägg och 3 borttagningar
  1. 21 3
      h2d/Sprite.hx

+ 21 - 3
h2d/Sprite.hx

@@ -41,9 +41,27 @@ class Sprite {
 		if( out == null ) out = new h2d.col.Bounds();
 		if( out == null ) out = new h2d.col.Bounds();
 		if( relativeTo == null ) {
 		if( relativeTo == null ) {
 			relativeTo = getScene();
 			relativeTo = getScene();
-			if( relativeTo == null ) relativeTo = this;
-		} else
-			relativeTo.syncPos();
+		} else {
+			var s1 = getScene();
+			var s2 = relativeTo.getScene();
+			if( s1 != s2 ) {
+				// if we are getting the bounds relative to a scene
+				// were are not into, it's the same as taking absolute position
+				if( s1 == null && s2 == relativeTo )
+					relativeTo = null;
+				else if( s2 == null )
+					throw "Cannot getBounds() with a relative element not in the scene";
+				else
+					throw "Cannot getBounds() with a relative element in a different scene";
+			}
+			if( relativeTo != null )
+				relativeTo.syncPos();
+		}
+		if( relativeTo == null ) {
+			relativeTo = this;
+			while( relativeTo.parent != null )
+				relativeTo = relativeTo.parent;
+		}
 		syncPos();
 		syncPos();
 		getBoundsRec(relativeTo, out);
 		getBoundsRec(relativeTo, out);
 		if( out.isEmpty() ) {
 		if( out.isEmpty() ) {