Lua中的addEventListener() [英] addEventListener() in Lua

查看:441
本文介绍了Lua中的addEventListener()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Corona时遇到问题,需要帮助。

I have a problem when work with Corona and need a help.

注册事件监听器时,例如 object:addEventListener( 触摸,听众)。但是侦听器函数具有许多这样的参数:

When I register an event listener, such as object:addEventListener("touch", listener). But listener function has many parameters like this:

function listener (event, param1, param2...)
    ...
end

我的问题是如何将所有参数传递给侦听器。

My question is that how to pass all of parameters to listener. All search only pass one para is event.

谢谢!

推荐答案

执行此操作的一种方法是仅将属性添加到附加了处理程序的对象上。在侦听器中,您可以通过 event.target 参数访问它们。

One way to do this is to just add properties to the object to which you attach the handler. In the listener, you can access them through the event.target parameter.

例如,添加新的 param1 param2 某些图像对象的属性:

e.g., adding new param1 and param2 properties to some image objects:

local touchHandler = function( event )
    if event.phase == "began" then
        local t = event.target
        print( "param1=" .. t.param1 .. ", param2=" .. t.param2 )
    end
end

local image1 = display.newImageRect( "myImage.png", 100, 100 )
image1.param1 = "Apple"
image1.param2 = "Zucchini"
image1:addEventListener( "touch", touchHandler )

local image2 = display.newImageRect( "myImage.png", 100, 100 )
image2.param1 = "AC/DC"
image2.param2 = "ZZ Top"
image2:addEventListener( "touch", touchHandler )

触摸image1时将打印 Apple和西葫芦,并打印 AC / DC 和 ZZ Top(每次触摸image2。

This will print "Apple" and "Zucchini" when you touch image1, and print "AC/DC" and "ZZ Top" each time you touch image2.

这篇关于Lua中的addEventListener()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆