Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 附加到事件侦听器的图像对象为nil_Lua_Coronasdk_Addeventlistener - Fatal编程技术网

Lua 附加到事件侦听器的图像对象为nil

Lua 附加到事件侦听器的图像对象为nil,lua,coronasdk,addeventlistener,Lua,Coronasdk,Addeventlistener,删除target:touch中的“local”:它使用变量local to target:touch()隐藏模块local。此外,如果您希望图像在触摸完成后消失,请使用触摸事件的“结束”和“取消”阶段。最后,我假设您已将目标初始化为某个对象,但如果没有,您也必须添加该对象,否则如何定义touch:event(感谢Rob注意到这一点): 您尚未创建目标。无法将触摸处理程序指定给尚不存在的对象 听起来你需要做的是在舞台上添加一个触摸处理器。我会预先创建图像,然后使用.isVisible=true隐藏

删除target:touch中的“local”:它使用变量local to target:touch()隐藏模块local。此外,如果您希望图像在触摸完成后消失,请使用触摸事件的“结束”和“取消”阶段。最后,我假设您已将目标初始化为某个对象,但如果没有,您也必须添加该对象,否则如何定义touch:event(感谢Rob注意到这一点):

您尚未创建目标。无法将触摸处理程序指定给尚不存在的对象


听起来你需要做的是在舞台上添加一个触摸处理器。我会预先创建图像,然后使用.isVisible=true隐藏它。然后在触摸处理程序中,显示和隐藏对象。但无论如何,您必须将触摸处理程序放在整个屏幕上,而不是一个单独的小图像。

问题是,我想在用户在x和y位置触摸屏幕时创建图像,并在用户提起触摸时将其移除。@SyedZainulAbedin ok extended answer;请尽可能具体地回答您遇到的问题,出于尊重,请足够具体地让人们提供帮助:Lua说这发生在哪一行?+1注意到函数是尚未创建的对象的一部分。我延长了我的回答。
--up in the level1.lua
local target
--in the enter frame function of scene
function target:touch(event)
  if event.phase=="began" then
    local target=display.newImage("target.png",event.x,event.y)
    return true
  end
end
-- first create the target, but don't show it:
local target = display.newImage("target.png", 0, 0)
target.isVisible = false

--in the enter frame function of scene
function target:touch(event)
  if event.phase=="began" then
    target.x = event.x
    target.y = event.y
    return true
  else if event.phase == "ended" or event.phase == "cancelled" then
    if target ~= nil then 
        target:removeSelf()
        target = nil
    end
    return true
  end
end
function target:touch(event)