在 Corona SDK 中处理两个功能的一个事件。

最近我疯狂地谷歌自己,只是为了得到一个相当简单(我希望)的方法,通过单个“EventListener”触摸来访问两个或更多个函数。

找到了这个:

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

  local loadServerButton = display.newRect(0, 0, 50, 50)
  loadServerButton:setFillColor(0, 0, 0)
  loadServerButton.x=  _W/2
  loadServerButton.y=  _H/1.35
  loadServerButton.param1 = timestampWrite
  loadServerButton.param2 = downloadServerAPI
  loadServerButton.param3 = downloadUserAPI
  loadServerButton:addEventListener("touch", touchHandler)

但是我遇到了困难,无法让它运行,显示出现错误:“尝试连接“param3”字段(一个函数值)”,等等。

我做错了什么?

点赞
用户1502079
用户1502079

我假设 timestampWrite 函数是这样定义的:

local timestampWrite = function()
    --此处为代码
end

以下是代码:

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

  local loadServerButton = display.newRect(0, 0, 50, 50)
  loadServerButton:setFillColor(0, 0, 0)
  loadServerButton.x=  _W/2
  loadServerButton.y=  _H/1.35
  loadServerButton.param1 = timestampWrite
  loadServerButton.param2 = downloadServerAPI
  loadServerButton.param3 = downloadUserAPI
  loadServerButton:addEventListener("touch", touchHandler)

更多信息:

2013-05-27 21:01:31