节点接口(Node)
节点脚本可用于渲染形状、图像、文本、画板等。
脚本化节点可附加在任意 Node 上,并在宿主节点本地变换空间(local transform space)渲染。
更多信息见:Node Scripts
方法(Methods)
init
节点创建时调用一次。初始化成功返回 true。
-- Define the script's data and inputs.
type MyNode = {}
-- Called once when the script initializes.
function init(self: MyNode, context: Context): boolean
return true
end
-- Return a factory function that Rive uses to build the Node instance.
return function(): Node<MyNode>
return {
init = init
}
end
advance
可选逐帧更新。返回 true 则继续接收 advance。
-- Define the script's data and inputs.
type MyNode = {}
-- Called every frame to advance the simulation.
-- 'seconds' is the elapsed time since the previous frame.
function advance(self: MyNode, seconds: number): boolean
return false
end
-- Return a factory function that Rive uses to build the Node instance.
return function(): Node<MyNode>
return {
advance = advance,
}
end
update
输入值变化时调用。
-- Define the script's data and inputs.
type MyNode = {}
-- Called when any input value changes.
function update(self: MyNode)
print('An script input value has changed.')
end
-- Return a factory function that Rive uses to build the Node instance.
return function(): Node<MyNode>
return {
update = update,
}
end
draw
使用 渲染器(Renderer) 绘制内容。
-- Define the script's data and inputs.
type MyNode = {}
-- Called every frame (after advance) to render the content.
function draw(self: MyNode, renderer: Renderer) end
-- Return a factory function that Rive uses to build the Node instance.
return function(): Node<MyNode>
return {
draw = draw,
}
end
pointerDown
指针按下处理。
function handlePointerDown(self: MyGame, event: PointerEvent)
print('Pointer Position: ', event.position.x, event.position.y)
event:hit()
end
return function(): Node<MyGame>
return {
init = init,
advance = advance,
draw = draw,
pointerDown = handlePointerDown,
}
end
pointerMove
指针移动处理。
function handlePointerMove(self: MyGame, event: PointerEvent)
print('Pointer Position: ', event.position.x, event.position.y)
event:hit()
end
return function(): Node<MyGame>
return {
init = init,
advance = advance,
draw = draw,
pointerMove = handlePointerMove,
}
end
pointerUp
指针抬起处理。
function handlePointerUp(self: MyGame, event: PointerEvent)
print('Pointer Position: ', event.position.x, event.position.y)
event:hit()
end
return function(): Node<MyGame>
return {
init = init,
advance = advance,
draw = draw,
pointerUp = handlePointerUp,
}
end
pointerExit
指针离开处理。
function handlePointerExit(self: MyGame, event: PointerEvent)
print('Pointer Position: ', event.position.x, event.position.y)
event:hit()
end
return function(): Node<MyGame>
return {
init = init,
advance = advance,
draw = draw,
pointerExit = handlePointerExit,
}
end