跳到主要内容

触发器属性(PropertyTrigger)

表示一个触发器属性(trigger property),可触发事件并通知监听器。

与其他 属性(Property) 不同,Trigger 不保存持久值;调用 fire() 时仅发出事件。

字段(Fields)

addListener

注册监听器(listener),在触发器触发时调用。

local vmi = context:viewModel()
if vmi then
local cannon = vmi:getTrigger('cannon')
if cannon then
cannon:addListener(function()
print("cannon fired!")
end)

cannon:fire()
end
end

removeListener

移除已注册监听器。无需时请移除,避免泄漏(leaks)。

function init(self: MyNode, context: Context): boolean
local vmi = context:viewModel()
if vmi then
local cannon = vmi:getTrigger('cannon')
if cannon then
self.cannon = cannon
self.onCannonFired = onCannonFired
cannon:addListener(onCannonFired)
end
end

return true
end

function removeCannonListener(self: MyNode)
if self.cannon then
self.cannon:removeListener(self.onCannonFired)
end
end

方法(Methods)

fire

触发该 Trigger,并通知所有已注册监听器。

local vmi = context:viewModel()
if vmi then
local cannon = vmi:getTrigger('cannon')
if cannon then
cannon:addListener(function()
print("cannon fired!")
end)

cannon:fire()
end
end