【问题标题】:Corona: trigger sprite on touchCorona:触摸时触发精灵
【发布时间】:2012-01-25 23:11:58
【问题描述】:

我想在触摸时触发精灵动画,并且只循环一次。

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

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)

【问题讨论】:

    标签: lua touch coronasdk sprite


    【解决方案1】:

    对一次性代码使用匿名函数
    你会在哪里编码一次然后忘记:

    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)
    

    【讨论】:

      【解决方案2】:

      请尝试

      instance1:addEventListener( "touch" , kick )
      

      甚至

      instance1:addEventListener( "tap" , kick )
      

      【讨论】:

        【解决方案3】:

        只写:

        instance1:addEventListener ("touch", kick)
        

        代替:

        Runtime:addEventListener ("touch", kick)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多