Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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 电晕:触摸时触发精灵_Lua_Touch_Coronasdk_Sprite - Fatal编程技术网

Lua 电晕:触摸时触发精灵

Lua 电晕:触摸时触发精灵,lua,touch,coronasdk,sprite,Lua,Touch,Coronasdk,Sprite,我想触发一个精灵动画,当它被触摸时,只让它循环一次 我有一个精灵动画,当前会在触摸屏上触发,但我不知道如何制作它,所以它只在精灵本身被触摸时才会产生动画 require "sprite" local sheet1 = sprite.newSpriteSheet( "greenman.png", 75, 105 ) local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 16) sprite.add( spriteSet1, "green", 1,

我想触发一个精灵动画,当它被触摸时,只让它循环一次

我有一个精灵动画,当前会在触摸屏上触发,但我不知道如何制作它,所以它只在精灵本身被触摸时才会产生动画

require "sprite"

local sheet1 = sprite.newSpriteSheet( "greenman.png", 75, 105 )

local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 16)

sprite.add( spriteSet1, "green", 1, 12, 700, 1 ) -- play 12 frames every 700 ms
local instance1 = sprite.newSprite( spriteSet1 )
instance1.x = display.contentWidth/2
instance1.y = display.contentHeight/2.8

function kick( event )
  if(event.phase == "ended") then
    instance1:prepare("green")
    instance1:play()
  end
end

Runtime:addEventListener("touch", kick)
请试一试

instance1:addEventListener( "touch" , kick )
甚至

instance1:addEventListener( "tap" , kick )

对一次性代码使用匿名函数
您只需编写一次代码,然后就可以忘记:

instance1:addEventListener("touch", function(event)
  if(event.phase == "ended") then
    instance1:prepare("green")
    instance1:play()
  end
end)

当您希望将函数绑定到对象时,请执行此操作,
它可能会在不同的情况下变形,
instance1
下的
kick
函数保存为其属性之一,
然后添加/删除它:

instance1.kick=function(event)
  if(event.phase == "ended") then
    instance1:prepare("green")
    instance1:play()
  end
end

instance1:addEventListener("touch",instance1.kick)

如果事件处理程序跨不同对象共享并广泛使用:

function kick( event )
  if(event.phase == "ended") then
    instance1:prepare("green")
    instance1:play()
  end
end

instance1:addEventListener("touch", kick)
只要写下:

instance1:addEventListener ("touch", kick)
而不是:

Runtime:addEventListener ("touch", kick)