lua transition.to onComplete function isnt working

时间:2018-05-09 23:51:11

标签: lua corona

local link = require("link")
local walkingData = require("Animation")

local walk_right
local walk_down
local walk_left
local walk_up

--loading in the sprite sheet and animation data
local options = link.getSpriteSheetData()
local walking = walkingData.getAnimationSequences()

-- creating the image sheet and sprite
local sheet1 = graphics.newImageSheet("link.png", options)

local linkWalk = display.newSprite(sheet1, walking)

linkWalk.x = 40
linkWalk.y= display.contentHeight - 60

--start the loop by transition
--linkWalk:setSequence("Walking up")
--linkWalk:play()
--transition.to(linkWalk, {y= 60, time= 8000, onComplete = walk_right})

local function walk_right ()

    linkWalk:setSequence("Walking right")
    linkWalk:play()
    transition.to(linkWalk, {x=display.contentWidth - 60, time= 3200, 
onComplete=walk_down})

end

local function walk_left ()

    linkWalk:setSequence("Walking left")
    linkWalk:play()
    transition.to(linkWalk, {x= 60, time= 3200, onComplete = walk_up})
end

local function walk_up ()

    linkWalk:setSequence("Walking up")
    linkWalk:play()
    transition.to(linkWalk, {y= 60, time= 8000, onComplete = walk_right})

end

local function walk_down ()

    linkWalk:setSequence("Walking down")
    linkWalk:play()
    transition.to(linkWalk, {y=display.contentHeight - 60, time= 8000, 
onComplete = walk_left})

end

--starting by making the sprite move upward
walk_up()'

我正在上一个日冕课,我在完成任务时遇到麻烦我试图让精灵图像绕过设备的边界但是精灵向上移动然后向右移动然后停止。为什么transition.to onComplete函数在被调用的第一个函数中正常工作,而不是由onComplete函数调用的函数。我已经尝试了多种方式来启动它,但只是向右移动,是否有更好的方法从过渡到过渡?

1 个答案:

答案 0 :(得分:0)

尝试

local composer   = require 'composer' 

local scene = composer.newScene()
local sprite


function scene:create( event )

    local sceneGroup = self.view

    local _T = display.screenOriginY
    local _B = display.viewableContentHeight - display.screenOriginY
    local _L = display.screenOriginX
    local _R = display.viewableContentWidth - display.screenOriginX

    sprite = display.newImage( sceneGroup, 'bubble.png', 0, 0 )
    sprite.index = 1

    local data = { 
        { x=sprite.width * 0.5,      y=_B - sprite.height * 0.5, time=3200 },
        { x=sprite.width * 0.5,      y=_T + sprite.height * 0.5, time=8000 },
        { x=_R - sprite.width * 0.5, y=_T + sprite.height * 0.5, time=3200 },
        { x=_R - sprite.width * 0.5, y=_B - sprite.height * 0.5, time=8000 },
        { x=sprite.width * 0.5,      y=_B - sprite.height * 0.5, time=3200 }
    }

    function sprite.onComplete( self )

        if self.index < 5 then 

            local prevTrans = data[self.index]
            local nextTrans = data[self.index + 1]
            nextTrans.onComplete = self.onComplete
            self.index = self.index + 1
            self.x, self.y = prevTrans.x, prevTrans.y
            transition.to( self, nextTrans )

        end

    end 



end

function scene:show( event )

    local sceneGroup = self.view
    local phase     = event.phase

    if phase == 'will' then

    elseif phase == 'did' then

        sprite.onComplete( sprite )

    end
end

function scene:hide( event )

    local sceneGroup = self.view
    local phase     = event.phase

    if phase == 'will' then

    elseif phase == 'did' then

    end
end

function scene:destroy( event )

end

scene:addEventListener( 'create', scene )
scene:addEventListener( 'show', scene )
scene:addEventListener( 'hide', scene )
scene:addEventListener( 'destroy', scene )

return scene

我使用自定义图像和编辑器模块。