【问题标题】:Handling two functions with one event. Corona SDK用一个事件处理两个函数。电晕SDK
【发布时间】:2013-05-22 12:57:40
【问题描述】:

目前我疯狂地用谷歌搜索自己,只是为了得到一个非常简单(我希望)的方法来访问两个或多个功能,只需触摸一个 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'(一个函数值)”等等。

我做错了什么?

【问题讨论】:

  • 您的参数是函数,而不是字符串。 (为什么要显示函数?)不能连接函数。在连接之前将它们转换为字符串:"param1="..tostring(t.param1)..
  • 嗯..我不知道...我还能用一个事件监听器调用所有 3 个函数吗?
  • 要调用一个函数,你必须在函数名后面加上括号:functionname()
  • function loadFuntions(event) timestampWrite() timestampWrite2() end 类似的东西?

标签: events lua coronasdk


【解决方案1】:

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

local timestampWrite = function()
    --some code here
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)

更多信息:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2013-05-04
    • 2015-11-16
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多