触摸功能不起作用。

local Gin2
local function Gin ( event )
    if ( event.phase == "began" ) then
      Gin2 = display.newImage("PNGs/Sprite/Gin")
        Gin2.x = _H
        Gin2.y = _W

    end
    return true
end
  Runtime:addEventListener("touch", Gin )

大家好,我一直在尝试弄清楚这个问题,但是一直没有成功。所以你可能猜到了,想法就是通过触摸来生成一张图片。我需要定义将要触摸的对象吗?

点赞
用户2848049
用户2848049

你正在使用“功能性监听器”表单,对于“触摸”事件,你不应该使用 Runtime。

在你的情况下,你需要将 Runtime 更改为“要触摸的对象”。我想你想做的是在图片被触摸后移动它。

因此,首先将

Gin2 = display.newImage("PNGs/Sprite/Gin")

移到函数外。然后将 Runtime 更改为 Gin2。

2015-08-13 14:21:54
用户3739502
用户3739502

我为您编写了一段代码。当您想通过触摸监听器(无论是运行时事件还是单个事件)生成一个对象时,您可以使用event.xevent.y来确定用户的触摸点。以下是代码。

注意:我将_gin_设为数组,以便以后使用您生成的对象。

local val = 1
local gin = {}

local spawnObject = function(event)

  if(event.phase == "ended") then

    gin[val] = display.newImage("PNGs/Sprite/Gin")
    gin[val].x = event.x
    gin[val].y = event.y

    val = val + 1

  end

end

 Runtime:addEventListener( "touch", spawnObject )
2015-08-14 02:19:40