Node Class Reference
| Inherits from | JavaScript Object |
| Location | aphid.g2d.Node |
Overview
The Node class represents a basic display unit in OpenAphid. It defines a rectangular area on the screen and the interfaces to manage the content inside the area.
The Node class itself provides basic behavior for controlling the position, visibility, size and other attributes of the rectangular area. More sophisticated content can be presented by its subclasses, like Sprite, ColorNode, etc.
The ‘Node’ class also acts as a container to embed other nodes and manage visual hierarchies. This creates a parent-child relationship between the node being embedded (known as the subnode) and the parent node doing the embedding (known as parent node).
A subnode’s visible area is not clipped to the bounds of its parent node. A clipToBounds attributes will be implemented in a future release to accomplish the same clipping behavior as iOS UIView does.
A parent node may contain multiple subnodes, but each subnode has only one parent node.
The geometry of a node is defined by its position, anchor and contentSize attributes.
Handling Touch Event
A node can be the target to handle touch events. The userInteractionEnabled and touchEnabled attributes determine whether a node can handle touch events.
If userInteractionEnabled of a node is false, then the node and its subnode cannot respond to any touch event; If touchEnabled is false while userInteractionEnabled is true, the node cannot respond to touch events, but its subnodes could handle touch events.
OpenAphid uses hit-testing and the node hierarchy to find the node to receive the touch event. In hit-testing, the systems calls hitTest(internal C++ method) on the running scene; this method proceeds by recursively calling pointInside(internal C++ method) on each node in the node hierarchy that return true. The hit-testing proceeds down the hierarchy until it finds the subnode within whose bounds the touch took place.
As the default contentSize of a node is Size.zero, pointInside ignores zero-sized node and proceeds to its children directly.
For a node which wants to handle touch events, it needs to meet the following conditions:
- The [#Node.userInteractionEnabled] attributes of the node and all its parent nodes are
true; - The touchEnabled attribute is
true; - The visible attribute is
true; - A touch event takes place inside the bounds of the node.
Please also refer to our blog post: Tutorial: Handling Touch Events in OpenAphid v0.1.1
Overview of Tasks
Attributes
- Name
-
anchorDefines the anchor point of the node’s bounds rectangle.
-
anchorInPixels -
boundingBox -
boundingBoxInPixels -
camera -
children -
contentSize -
contentSizeInPixels -
isRelativeAnchor -
isRunning -
onframeupdateRegisters a JavaScript callback, which can be invoked when updating each frame.
-
parent -
position -
positionInPixels -
rotation -
scale -
skew -
tag -
visible -
zOrderDetermines the display ordering of the node
- Type
-
Point -
Point -
Rect -
Rect -
Camera -
NodeList -
Size -
Size -
bool -
bool -
function -
Node -
Point -
Point -
float -
Vector2 -
Vector2 -
int -
bool -
int
- Writability
-
readwrite -
readonly -
readonly -
readonly -
readonly -
readonly -
readwrite -
readwrite -
readwrite -
readonly -
readwrite -
readonly -
readwrite -
readwrite -
readwrite -
readwrite -
readwrite -
readwrite -
readwrite -
readonly
Handling Touch Events
- Type
-
function -
function -
function -
function -
bool -
bool
- Writability
-
readwrite -
readwrite -
readwrite -
readwrite -
readwrite -
readwrite
Functions
- Signature
-
addChild(child, [z], [tag])Adds a node to the receiver’s list of subnodes.
-
cleanup() -
getChildByTag(tag) -
removeAllChildren([cleanup]) -
removeChild(child, [cleanup]) -
removeChildByTag(tag, [cleanup]) -
removeFromParent([cleanup]) -
reorderChild(child, z)
- Return Type
-
Node -
void -
Node -
void -
void -
void -
void -
void
Converting Between Node Coordinate Systems
- Signature
-
convertParentToNodeSpace(p) -
convertToNodeSpace(p) -
convertToNodeSpaceAR(p) -
convertToWorldSpace(p) -
convertToWorldSpaceAR(p) -
locationAROfTouch(touch) -
locationOfTouch(touch)
- Return Type
-
Point -
Point -
Point -
Point -
Point -
Point -
Point
Managing Actions
- Signature
-
runAction(action) -
stopAllActions()
- Return Type
-
void -
void
Managing Frame Update Event
- Signature
-
scheduleUpdate() -
unscheduleUpdate()
- Return Type
-
void -
void
Managing Timers
- Signature
-
scheduleTimer(callback, interval, [repeatTimes], [delay])Starts a timer.
-
unscheduleAllTimers() -
unscheduleTimer(callback)
- Return Type
-
void -
void -
void
Class Attributes
- Name
-
INVALID_TAG
- Type
-
int
- Writability
-
readonly
Attributes
anchor
Defines the anchor point of the node’s bounds rectangle.
Non-null readwrite Point anchorDiscussion
Defaults to (0.0, 0.0), the left-bottom of the bounds rectangle.
Notes: the default value of anchor may be different in the subclasses of Node, like the default anchor of Sprite class is (0.5, 0.5), the center of its bounds rectangle.
onframeupdate
Registers a JavaScript callback, which can be invoked when updating each frame.
readwrite function onframeupdateDiscussion
To make the registered callback be notified, scheduleUpdate() must be invoked to put the current node into the frame update event queue. unscheduleUpdate stops the node from receiving frame update.
The callback function will receive two parameters: target and deltaTime. target is the node which was registered with the callback; deltaTime is the interval from last frame measured in seconds.
node.onframeupdate = function(target, deltaTime) {
};
node.scheduleUpdate();
Set null to onframeupdate will also invoke unscheduleUpdate automatically.
See Also
scale
Non-null readwrite Vector2 scaleDiscussion
An int value is also valid; both scale.x and scale.y will be set to it.
Functions
addChild(child, [z], [tag])
Adds a node to the receiver’s list of subnodes.
Parameters
Non-nullNode childThe node to be added
OptionalintzThe
zOrderof the new subnode
Optionalinttag
Return Value: Node
The same receiver node of addChild is returned. It’s helpful for method chaining.
Discussion
The child node’s parent is set to the receiver.
Nodes can have only one parent node. If child already has a parent and that node is the receiver, reorderChild is invoked instead; if that parent node is not the receiver, this method makes child invoke removeFromParent(false) before adding it as a subnode.
Notes: adding a child which already has a parent node is usually not the expected behavior, OpenAphid displays a warning message about it in develop mode.
See Also
cleanup()
void cleanup()Discussion
The cleanup function does the following things:
- Stops all actions, same as calling
stopAllActionsmanually; - Prevents the node from receiving the frame update event, same as calling
unscheduleUpdatemanually; - Unschedule all timers, same as calling
unscheduleAllTimersmanually; - All children nodes will proceed with the three steps above recursively.
Notes: the values of onframeupdate and ontouchxxx will NOT be reset to null, they will keep their original values.
removeAllChildren([cleanup])
void removeAllChildren([bool cleanup])Parameters
OptionalboolcleanupSet to
truewill invokecleanupon each subnode after they’re removed from the parent. Default istrue.
See Also
removeChild(child, [cleanup])
Parameters
Non-nullNode child
OptionalboolcleanupSet to
truewill invokechild.cleanupafter it’s removed from the parent. Default istrue.
See Also
removeChildByTag(tag, [cleanup])
void removeChildByTag(int tag, [bool cleanup])Parameters
inttag
OptionalboolcleanupSet to
truewill invokechild.cleanupafter it’s removed from the parent. Default istrue.
See Also
scheduleTimer(callback, interval, [repeatTimes], [delay])
Starts a timer.
void scheduleTimer(function callback, float interval, [int repeatTimes], [float delay])Parameters
Non-nullfunctioncallback
floatintervalThe number of seconds between firings of the time.
OptionalintrepeatTimesThe maximum firing times. Default is -1, which means unlimited firings.
OptionalfloatdelayThe number of seconds before starting the timer. Default is 0.
Discussion
A timer is started after delay time interval has elapsed and then invokes callback periodically with respect to the specified interval. If repeatTimes is a positive value, the timers will stop after being fired repeatTimes.”
The callback will receive three parameters: target, callback and deltaTime. target is the node which the timer started with; callback is the original function object passed in scheduleTimer; deltaTime is the elapsed time between firings.
A scheduled timer can be manually stopped by using unscheduleTimer() or making callback return false:
//Starts a timer which fires every second.
node.scheduleTimer(
function(target, callback, deltaTime) {
if (should stop firing)
return false; //or: target.unscheduleTimer(callback);
},
1.0);