我想用拖动事件移动精灵表的帧。精灵实例必须只在用户拖动时播放。如果他举起手,它必须暂停。有什么方法可以这样做吗?
答案 0 :(得分:0)
require "sprite"
-- A sprite sheet with a green dude
local sheet = sprite.newSpriteSheet( "greenman.png", 128, 128 )
local spriteSet = sprite.newSpriteSet(sheet, 1, 15)
sprite.add( spriteSet, "man", 1, 15, 200, 0 ) -- play 15 frames every 200 ms
local instance = sprite.newSprite( spriteSet )
instance.x = display.contentWidth / 2
instance.y = display.contentHeight / 2
instance:prepare("man")
local function startDrag( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
t:play()
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
t:pause()
end
end
-- Stop further propagation of touch event!
return true
end
instance:addEventListener( "touch", startDrag )