SPDisplayObject Class Reference
| Inherits from | SPEventDispatcher : NSObject |
| Declared in | SPDisplayObject.h |
Overview
The SPDisplayObject class is the base class for all objects that are rendered on the screen.
In Sparrow, all displayable objects are organized in a display tree. Only objects that are part of the display tree will be displayed (rendered).
The display tree consists of leaf nodes (SPImage, SPQuad) that will be rendered directly to the screen, and of container nodes (subclasses of SPDisplayObjectContainer, like SPSprite). A container is simply a display object that has child nodes - which can, again, be either leaf nodes or other containers.
A display object has properties that define its position in relation to its parent
(x, y), as well as its rotation, skewing and scaling factors (scaleX, scaleY). Use the
alpha and visible properties to make an object translucent or invisible.
Every display object may be the target of touch events. If you don’t want an object to be
touchable, you can disable the touchable property. When it’s disabled, neither the object
nor its children will receive any more touch events.
Points vs. Pixels
All sizes and distances are measured in points. What this means in pixels depends on the contentScaleFactor of the device. On a low resolution device (iPhone 3GS / iPad 1+2), one point corresponds to one pixel. On devices with a retina display, one point may be 2 pixels.
Transforming coordinates
Within the display tree, each object has its own local coordinate system. If you rotate a container, you rotate that coordinate system - and thus all the children of the container.
Sometimes you need to know where a certain point lies relative to another coordinate system.
That’s the purpose of the method transformationMatrixToSpace:. It will create a matrix that
represents the transformation of a point in one coordinate system to another.
Subclassing SPDisplayObject
As SPDisplayObject is an abstract class, you can’t instantiate it directly, but have to use one of its subclasses instead. There are already a lot of them available, and most of the time they will suffice.
However, you can create custom display objects as well. That’s especially useful when you want to create an object with a custom render function.
You will need to implement the following methods when you subclass SPDisplayObject:
- (void)render:(SPRenderSupport *)support;
- (SPRectangle *)boundsInSpace:(SPDisplayObject *)targetSpace;
Have a look at SPQuad for a sample implementation of those methods.
Tasks
Methods
-
– render:Renders the display object with the help of a support object.
-
– removeFromParentRemoves the object from its parent, if it has one.
-
– alignPivotToCenterMoves the pivot point to the center of the object.
-
– alignPivotX:pivotY:Moves the pivot point to a certain position within the local coordinate system of the object.
-
– transformationMatrixToSpace:Creates a matrix that represents the transformation from the local coordinate system to another.
-
– boundsInSpace:Returns a rectangle that completely encloses the object as it appears in another coordinate system.
-
– localToGlobal:Transforms a point from the local coordinate system to global (stage) coordinates.
-
– globalToLocal:Transforms a point from global (stage) coordinates to the local coordinate system.
-
– hitTestPoint:Returns the object that is found topmost on a point in local coordinates, or nil if the test fails.
-
– broadcastEvent:Dispatches an event on all children (recursively). The event must not bubble. */
-
– broadcastEventWithType:Creates an event and dispatches it on all children (recursively). */
Properties
-
xThe x coordinate of the object relative to the local coordinates of the parent.
property -
yThe y coordinate of the object relative to the local coordinates of the parent.
property -
pivotXThe x coordinate of the object’s origin in its own coordinate space (default: 0).
property -
pivotYThe y coordinate of the object’s origin in its own coordinate space (default: 0).
property -
scaleThe scale factor. “1” means no scale, a negative value inverts the object. Note: Accessing this property will always return scaleX, even if scaleX and scaleY are not equal.
property -
scaleXThe horizontal scale factor. “1” means no scale, negative values flip the object.
property -
scaleYThe vertical scale factor. “1” means no scale, negative values flip the object.
property -
skewXThe horizontal skew angle in radians.
property -
skewYThe vertical skew angle in radians.
property -
widthThe width of the object in points.
property -
heightThe height of the object in points.
property -
rotationThe rotation of the object in radians. (In Sparrow, all angles are measured in radians.)
property -
alphaThe opacity of the object. 0 = transparent, 1 = opaque.
property -
visibleThe visibility of the object. An invisible object will be untouchable.
property -
touchableIndicates if this object (and its children) will receive touch events.
property -
boundsThe bounds of the object relative to the local coordinates of the parent.
property -
parentThe display object container that contains this display object.
property -
rootThe root object the display object is connected to (i.e. an instance of the class that was passed to
property[SPViewController startWithRoot:]), or nil if the object is not connected to it. -
stageThe stage the display object is connected to, or nil if it is not connected to a stage.
property -
baseThe topmost object in the display tree the object is part of.
property -
transformationMatrixThe transformation matrix of the object relative to its parent.
property -
nameThe name of the display object (default: nil). Used by
propertychildByName:of display object containers. -
filterThe filter that is attached to the display object. Beware that you should NOT use the same filter on more than one object (for performance reasons).
property -
blendModeThe blend mode determines how the object is blended with the objects underneath. Default: AUTO
property -
hasVisibleAreaIndicates if an object occupies any visible area. (Which is the case when its
propertyalpha,scaleXandscaleYvalues are not zero, and itsvisibleproperty is enabled.) -
physicsBodyThe physics body associated with the display object. Sparrow does not provide physics on its own, but this property may be used by any physics library to link an object to its physical body.
property
Properties
alpha
The opacity of the object. 0 = transparent, 1 = opaque.
@property (nonatomic, assign) float alphaDiscussion
The opacity of the object. 0 = transparent, 1 = opaque.
Declared In
SPDisplayObject.hbase
The topmost object in the display tree the object is part of.
@property (weak, nonatomic, readonly) SPDisplayObject *baseDiscussion
The topmost object in the display tree the object is part of.
Declared In
SPDisplayObject.hblendMode
The blend mode determines how the object is blended with the objects underneath. Default: AUTO
@property (nonatomic, assign) uint blendModeDiscussion
The blend mode determines how the object is blended with the objects underneath. Default: AUTO
Declared In
SPDisplayObject.hbounds
The bounds of the object relative to the local coordinates of the parent.
@property (weak, nonatomic, readonly) SPRectangle *boundsDiscussion
The bounds of the object relative to the local coordinates of the parent.
Declared In
SPDisplayObject.hfilter
The filter that is attached to the display object. Beware that you should NOT use the same filter on more than one object (for performance reasons).
@property (nonatomic, strong) SPFragmentFilter *filterDiscussion
The filter that is attached to the display object. Beware that you should NOT use the same filter on more than one object (for performance reasons).
Declared In
SPDisplayObject.hhasVisibleArea
Indicates if an object occupies any visible area. (Which is the case when its alpha,
scaleX and scaleY values are not zero, and its visible property is enabled.)
@property (nonatomic, readonly) BOOL hasVisibleAreaDiscussion
Indicates if an object occupies any visible area. (Which is the case when its alpha,
scaleX and scaleY values are not zero, and its visible property is enabled.)
Declared In
SPDisplayObject.hheight
The height of the object in points.
@property (nonatomic, assign) float heightDiscussion
The height of the object in points.
Declared In
SPDisplayObject.hname
The name of the display object (default: nil). Used by childByName: of display object containers.
@property (nonatomic, copy) NSString *nameDiscussion
The name of the display object (default: nil). Used by childByName: of display object containers.
Declared In
SPDisplayObject.hparent
The display object container that contains this display object.
@property (weak, nonatomic, readonly) SPDisplayObjectContainer *parentDiscussion
The display object container that contains this display object.
Declared In
SPDisplayObject.hphysicsBody
The physics body associated with the display object. Sparrow does not provide physics on its own, but this property may be used by any physics library to link an object to its physical body.
@property (nonatomic, strong) SP_PHYSICS_CLASS *physicsBodyDiscussion
The physics body associated with the display object. Sparrow does not provide physics on its own, but this property may be used by any physics library to link an object to its physical body.
Declared In
SPDisplayObject.hpivotX
The x coordinate of the object’s origin in its own coordinate space (default: 0).
@property (nonatomic, assign) float pivotXDiscussion
The x coordinate of the object’s origin in its own coordinate space (default: 0).
Declared In
SPDisplayObject.hpivotY
The y coordinate of the object’s origin in its own coordinate space (default: 0).
@property (nonatomic, assign) float pivotYDiscussion
The y coordinate of the object’s origin in its own coordinate space (default: 0).
Declared In
SPDisplayObject.hroot
The root object the display object is connected to (i.e. an instance of the class
that was passed to [SPViewController startWithRoot:]), or nil if the object is not connected
to it.
@property (weak, nonatomic, readonly) SPDisplayObject *rootDiscussion
The root object the display object is connected to (i.e. an instance of the class
that was passed to [SPViewController startWithRoot:]), or nil if the object is not connected
to it.
Declared In
SPDisplayObject.hrotation
The rotation of the object in radians. (In Sparrow, all angles are measured in radians.)
@property (nonatomic, assign) float rotationDiscussion
The rotation of the object in radians. (In Sparrow, all angles are measured in radians.)
Declared In
SPDisplayObject.hscale
The scale factor. “1” means no scale, a negative value inverts the object. Note: Accessing this property will always return scaleX, even if scaleX and scaleY are not equal.
@property (nonatomic, assign) float scaleDiscussion
The scale factor. “1” means no scale, a negative value inverts the object. Note: Accessing this property will always return scaleX, even if scaleX and scaleY are not equal.
Declared In
SPDisplayObject.hscaleX
The horizontal scale factor. “1” means no scale, negative values flip the object.
@property (nonatomic, assign) float scaleXDiscussion
The horizontal scale factor. “1” means no scale, negative values flip the object.
Declared In
SPDisplayObject.hscaleY
The vertical scale factor. “1” means no scale, negative values flip the object.
@property (nonatomic, assign) float scaleYDiscussion
The vertical scale factor. “1” means no scale, negative values flip the object.
Declared In
SPDisplayObject.hskewX
The horizontal skew angle in radians.
@property (nonatomic, assign) float skewXDiscussion
The horizontal skew angle in radians.
Declared In
SPDisplayObject.hskewY
The vertical skew angle in radians.
@property (nonatomic, assign) float skewYDiscussion
The vertical skew angle in radians.
Declared In
SPDisplayObject.hstage
The stage the display object is connected to, or nil if it is not connected to a stage.
@property (weak, nonatomic, readonly) SPStage *stageDiscussion
The stage the display object is connected to, or nil if it is not connected to a stage.
Declared In
SPDisplayObject.htouchable
Indicates if this object (and its children) will receive touch events.
@property (nonatomic, assign) BOOL touchableDiscussion
Indicates if this object (and its children) will receive touch events.
Declared In
SPDisplayObject.htransformationMatrix
The transformation matrix of the object relative to its parent.
@property (nonatomic, copy) SPMatrix *transformationMatrixReturn Value
CAUTION: not a copy, but the actual object!
Discussion
The transformation matrix of the object relative to its parent.
Declared In
SPDisplayObject.hvisible
The visibility of the object. An invisible object will be untouchable.
@property (nonatomic, assign) BOOL visibleDiscussion
The visibility of the object. An invisible object will be untouchable.
Declared In
SPDisplayObject.hwidth
The width of the object in points.
@property (nonatomic, assign) float widthDiscussion
The width of the object in points.
Declared In
SPDisplayObject.hInstance Methods
alignPivotToCenter
Moves the pivot point to the center of the object.
- (void)alignPivotToCenterDiscussion
Moves the pivot point to the center of the object.
Declared In
SPDisplayObject.halignPivotX:pivotY:
Moves the pivot point to a certain position within the local coordinate system of the object.
- (void)alignPivotX:(SPHAlign)hAlign pivotY:(SPVAlign)vAlignDiscussion
Moves the pivot point to a certain position within the local coordinate system of the object.
Declared In
SPDisplayObject.hboundsInSpace:
Returns a rectangle that completely encloses the object as it appears in another coordinate system.
- (SPRectangle *)boundsInSpace:(SPDisplayObject *)targetSpaceDiscussion
Returns a rectangle that completely encloses the object as it appears in another coordinate system.
Declared In
SPDisplayObject.hbroadcastEvent:
Dispatches an event on all children (recursively). The event must not bubble. */
- (void)broadcastEvent:(SPEvent *)eventDiscussion
Dispatches an event on all children (recursively). The event must not bubble. */
Declared In
SPDisplayObject.hbroadcastEventWithType:
Creates an event and dispatches it on all children (recursively). */
- (void)broadcastEventWithType:(NSString *)typeDiscussion
Creates an event and dispatches it on all children (recursively). */
Declared In
SPDisplayObject.hglobalToLocal:
Transforms a point from global (stage) coordinates to the local coordinate system.
- (SPPoint *)globalToLocal:(SPPoint *)globalPointDiscussion
Transforms a point from global (stage) coordinates to the local coordinate system.
Declared In
SPDisplayObject.hhitTestPoint:
Returns the object that is found topmost on a point in local coordinates, or nil if the test fails.
- (SPDisplayObject *)hitTestPoint:(SPPoint *)localPointDiscussion
Returns the object that is found topmost on a point in local coordinates, or nil if the test fails.
Declared In
SPDisplayObject.hlocalToGlobal:
Transforms a point from the local coordinate system to global (stage) coordinates.
- (SPPoint *)localToGlobal:(SPPoint *)localPointDiscussion
Transforms a point from the local coordinate system to global (stage) coordinates.
Declared In
SPDisplayObject.hremoveFromParent
Removes the object from its parent, if it has one.
- (void)removeFromParentDiscussion
Removes the object from its parent, if it has one.
Declared In
SPDisplayObject.hrender:
Renders the display object with the help of a support object.
- (void)render:(SPRenderSupport *)supportDiscussion
Renders the display object with the help of a support object.
Declared In
SPDisplayObject.htransformationMatrixToSpace:
Creates a matrix that represents the transformation from the local coordinate system to another.
- (SPMatrix *)transformationMatrixToSpace:(SPDisplayObject *)targetSpaceDiscussion
Creates a matrix that represents the transformation from the local coordinate system to another.
Declared In
SPDisplayObject.h