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:

  1. The [#Node.userInteractionEnabled] attributes of the node and all its parent nodes are true;
  2. The touchEnabled attribute is true;
  3. The visible attribute is true;
  4. 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 Constructors

Overview of Tasks

Attributes

  • 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

  • Return Type
  • Node
  • void
  • Node
  • void
  • void
  • void
  • void
  • void

Converting Between Node Coordinate Systems

  • Return Type
  • Point
  • Point
  • Point
  • Point
  • Point
  • Point
  • Point

Managing Actions

  • Return Type
  • void
  • void

Managing Frame Update Event

  • Return Type
  • void
  • void

Managing Timers

  • Return Type
  • void
  • void
  • void

Class Attributes

  • Type
  • int
  • Writability
  • readonly

Constructors

Node()

new Node()

Attributes

anchor

Defines the anchor point of the node’s bounds rectangle.

Non-null readwrite Point anchor

Discussion

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.

anchorInPixels

Non-null readonly Point anchorInPixels

boundingBox

Non-null readonly Rect boundingBox

boundingBoxInPixels

Non-null readonly Rect boundingBoxInPixels

camera

readonly Camera camera

children

readonly NodeList children

contentSize

Non-null readwrite Size contentSize

contentSizeInPixels

Non-null readwrite Size contentSizeInPixels

isRelativeAnchor

readwrite bool isRelativeAnchor

isRunning

readonly bool isRunning

onframeupdate

Registers a JavaScript callback, which can be invoked when updating each frame.

readwrite function onframeupdate

Discussion

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.

ontouchcancel

readwrite function ontouchcancel

ontouchend

readwrite function ontouchend

ontouchmove

readwrite function ontouchmove

ontouchstart

readwrite function ontouchstart

parent

readonly Node parent

position

Non-null readwrite Point position

positionInPixels

Non-null readwrite Point positionInPixels

rotation

readwrite float rotation

scale

Non-null readwrite Vector2 scale

Discussion

An int value is also valid; both scale.x and scale.y will be set to it.

skew

Non-null readwrite Vector2 skew

tag

readwrite int tag

Discussion

Notes: this is a subject to change in the future release, it’s planned to support string tags in OpenAphid as DOM does.

touchEnabled

readwrite bool touchEnabled

userInteractionEnabled

readwrite bool userInteractionEnabled

visible

readwrite bool visible

zOrder

Determines the display ordering of the node

readonly int zOrder

Discussion

A node with smaller zOrder is displayed in front of other nodes.

Functions

addChild(child, [z], [tag])

Adds a node to the receiver’s list of subnodes.

Node addChild(Node child, [int z], [int tag])

Parameters

Non-null Node child

The node to be added

Optional int z

The zOrder of the new subnode

Optional int tag

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:

  1. Stops all actions, same as calling stopAllActions manually;
  2. Prevents the node from receiving the frame update event, same as calling unscheduleUpdate manually;
  3. Unschedule all timers, same as calling unscheduleAllTimers manually;
  4. 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.

convertParentToNodeSpace(p)

Point convertParentToNodeSpace(Point p)

Parameters

Non-null Point p

Return Value: Point

convertToNodeSpace(p)

Point convertToNodeSpace(Point p)

Parameters

Non-null Point p

Return Value: Point

convertToNodeSpaceAR(p)

Point convertToNodeSpaceAR(Point p)

Parameters

Non-null Point p

Return Value: Point

convertToWorldSpace(p)

Point convertToWorldSpace(Point p)

Parameters

Non-null Point p

Return Value: Point

convertToWorldSpaceAR(p)

Point convertToWorldSpaceAR(Point p)

Parameters

Non-null Point p

Return Value: Point

getChildByTag(tag)

Node getChildByTag(int tag)

Parameters

int tag

Return Value: Node

locationAROfTouch(touch)

Point locationAROfTouch(Touch touch)

Parameters

Non-null Touch touch

Return Value: Point

locationOfTouch(touch)

Point locationOfTouch(Touch touch)

Parameters

Non-null Touch touch

Return Value: Point

removeAllChildren([cleanup])

void removeAllChildren([bool cleanup])

Parameters

Optional bool cleanup

Set to true will invoke cleanup on each subnode after they’re removed from the parent. Default is true.

See Also

removeChild(child, [cleanup])

void removeChild(Node child, [bool cleanup])

Parameters

Non-null Node child
Optional bool cleanup

Set to true will invoke child.cleanup after it’s removed from the parent. Default is true.

See Also

removeChildByTag(tag, [cleanup])

void removeChildByTag(int tag, [bool cleanup])

Parameters

int tag
Optional bool cleanup

Set to true will invoke child.cleanup after it’s removed from the parent. Default is true.

See Also

removeFromParent([cleanup])

void removeFromParent([bool cleanup])

Parameters

Optional bool cleanup

reorderChild(child, z)

void reorderChild(Node child, int z)

Parameters

Node child
int z

runAction(action)

void runAction(Action action)

Parameters

Action action

scheduleTimer(callback, interval, [repeatTimes], [delay])

Starts a timer.

void scheduleTimer(function callback, float interval, [int repeatTimes], [float delay])

Parameters

Non-null function callback
float interval

The number of seconds between firings of the time.

Optional int repeatTimes

The maximum firing times. Default is -1, which means unlimited firings.

Optional float delay

The 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);

scheduleUpdate()

void scheduleUpdate()

stopAllActions()

void stopAllActions()

unscheduleAllTimers()

void unscheduleAllTimers()

unscheduleTimer(callback)

void unscheduleTimer(function callback)

Parameters

Non-null function callback

See Also

unscheduleUpdate()

void unscheduleUpdate()

Class Attributes

Node.INVALID_TAG

readonly int INVALID_TAG

Discussion

Constant value: -1. Notes: this is a subject to change in the future release.