Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

'root' now returns the topmost class *below* the stage; new property …

…'base' replaces the old functionality. (closes #121)
  • Loading branch information...
commit feef50312feb98f8a099b70c7a0258a02a10a8aa 1 parent ef1ed6d
@PrimaryFeather PrimaryFeather authored
View
47 starling/src/starling/display/DisplayObject.as
@@ -50,7 +50,7 @@ package starling.display
* A container is simply a display object that has child nodes - which can, again, be either
* leaf nodes or other containers.</p>
*
- * <p>At the root of the display tree, there is the Stage, which is a container, too. To create
+ * <p>At the base of the display tree, there is the Stage, which is a container, too. To create
* a Starling application, you create a custom Sprite subclass, and Starling will add an
* instance of this class to the stage.</p>
*
@@ -183,10 +183,10 @@ package starling.display
resultMatrix.copyFrom(transformationMatrix);
return resultMatrix;
}
- else if (targetSpace == null || targetSpace == root)
+ else if (targetSpace == null || targetSpace == base)
{
- // targetCoordinateSpace 'null' represents the target space of the root object.
- // -> move up from this to root
+ // targetCoordinateSpace 'null' represents the target space of the base object.
+ // -> move up from this to base
currentObject = this;
while (currentObject != targetSpace)
@@ -280,14 +280,14 @@ package starling.display
/** Transforms a point from the local coordinate system to global (stage) coordinates. */
public function localToGlobal(localPoint:Point):Point
{
- getTransformationMatrix(root, sHelperMatrix);
+ getTransformationMatrix(base, sHelperMatrix);
return sHelperMatrix.transformPoint(localPoint);
}
/** Transforms a point from global (stage) coordinates to the local coordinate system. */
public function globalToLocal(globalPoint:Point):Point
{
- getTransformationMatrix(root, sHelperMatrix);
+ getTransformationMatrix(base, sHelperMatrix);
sHelperMatrix.invert();
return sHelperMatrix.transformPoint(globalPoint);
}
@@ -382,14 +382,6 @@ package starling.display
else scaleY = 1.0;
}
- /** The topmost object in the display tree the object is part of. */
- public function get root():DisplayObject
@PrimaryFeather Owner

The same here:

var quad:Quad = new Quad();
quad.base == quad;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- {
- var currentObject:DisplayObject = this;
- while (currentObject.mParent) currentObject = currentObject.mParent;
- return currentObject;
- }
-
/** The x coordinate of the object relative to the local coordinates of the parent. */
public function get x():Number { return mX; }
public function set x(value:Number):void
@@ -501,8 +493,31 @@ package starling.display
/** The display object container that contains this display object. */
public function get parent():DisplayObjectContainer { return mParent; }
+ /** The topmost object in the display tree the object is part of. */
+ public function get base():DisplayObject
+ {
+ var currentObject:DisplayObject = this;
+ while (currentObject.mParent) currentObject = currentObject.mParent;
+ return currentObject;
+ }
+
+ /** The root object the display object is connected to (i.e. an instance of the class
+ * that was passed to the Starling constructor), or null if the object is not connected
+ * to the stage. */
+ public function get root():DisplayObject
@fljot
fljot added a note

Maybe DisplayObjectContainer? Can it really be NOT container?

@PrimaryFeather Owner

In theory, yes!

var quad:Quad = new Quad(100, 100);
var starling:Starling = new Starling(quad, stage);

Does not make much sense, but it's possible!

@fljot
fljot added a note

looks like update didn't go through. 2nd part:

it won't return valid value if object was added to the stage directly.

@PrimaryFeather Owner

You mean if someone uses "stage.addChild(something);", right?
I think that's fine. One shouldn't do that, anyway, and I'd define "root" as the top-most non-stage parent of an object. (But that's rather theoretical, anyway.)

@fljot
fljot added a note

Hm.. quick test in Flash CS:

var s:Sprite = new Sprite();
addChild(s);
trace(s.root);//[object MainTimeline]
stage.addChild(s);
trace(s.root);//[object Stage]

actually not what I'd expect. But still, maybe u should do the same?

@PrimaryFeather Owner

Wow, that's indeed unexpected.
Actually, I think I rather leave it as it is, then. I don't think we need to mimic such small nuances -- especially given the rather huge changes I made at different places. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ var currentObject:DisplayObject = this;
+ while (currentObject.mParent)
+ {
+ if (currentObject.mParent is Stage) return currentObject;
+ else currentObject = currentObject.parent;
+ }
+
+ return null;
+ }
+
/** The stage the display object is connected to, or null if it is not connected
- * to a stage. */
- public function get stage():Stage { return this.root as Stage; }
+ * to the stage. */
+ public function get stage():Stage { return this.base as Stage; }
}
}
View
4 starling/src/starling/events/Touch.as
@@ -66,7 +66,7 @@ package starling.events
public function getLocation(space:DisplayObject):Point
{
var point:Point = new Point(mGlobalX, mGlobalY);
- mTarget.root.getTransformationMatrix(space, sHelperMatrix);
+ mTarget.base.getTransformationMatrix(space, sHelperMatrix);
return sHelperMatrix.transformPoint(point);
}
@@ -75,7 +75,7 @@ package starling.events
public function getPreviousLocation(space:DisplayObject):Point
{
var point:Point = new Point(mPreviousGlobalX, mPreviousGlobalY);
- mTarget.root.getTransformationMatrix(space, sHelperMatrix);
+ mTarget.base.getTransformationMatrix(space, sHelperMatrix);
return sHelperMatrix.transformPoint(point);
}
View
34 tests/src/tests/DisplayObjectTest.as
@@ -22,6 +22,7 @@ package tests
import starling.display.Quad;
import starling.display.Sprite;
+ import starling.display.Stage;
import starling.utils.deg2rad;
public class DisplayObjectTest
@@ -29,7 +30,7 @@ package tests
private static const E:Number = 0.0001;
[Test]
- public function testRoot():void
+ public function testBase():void
{
var object1:Sprite = new Sprite();
var object2:Sprite = new Sprite();
@@ -38,9 +39,40 @@ package tests
object1.addChild(object2);
object2.addChild(object3);
+ Assert.assertEquals(object1, object1.base);
+ Assert.assertEquals(object1, object2.base);
+ Assert.assertEquals(object1, object3.base);
+
+ var quad:Quad = new Quad(100, 100);
+ Assert.assertEquals(quad, quad.base);
+ }
+
+ [Test]
+ public function testRootAndStage():void
+ {
+ var object1:Sprite = new Sprite();
+ var object2:Sprite = new Sprite();
+ var object3:Sprite = new Sprite();
+
+ object1.addChild(object2);
+ object2.addChild(object3);
+
+ Assert.assertEquals(null, object1.root);
+ Assert.assertEquals(null, object2.root);
+ Assert.assertEquals(null, object3.root);
+ Assert.assertEquals(null, object1.stage);
+ Assert.assertEquals(null, object2.stage);
+ Assert.assertEquals(null, object3.stage);
+
+ var stage:Stage = new Stage(100, 100);
+ stage.addChild(object1);
+
Assert.assertEquals(object1, object1.root);
Assert.assertEquals(object1, object2.root);
Assert.assertEquals(object1, object3.root);
+ Assert.assertEquals(stage, object1.stage);
+ Assert.assertEquals(stage, object2.stage);
+ Assert.assertEquals(stage, object3.stage);
}
[Test]
Please sign in to comment.
Something went wrong with that request. Please try again.