Created
January 13, 2014 09:17
-
-
Save qiuyuzhou/84fab254b82dff9416f0 to your computer and use it in GitHub Desktop.
Helper functions for using cocos2dx actions in lua coroutine.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 使用CCSequence将多个action串起来 | |
function sequenceActions(...) | |
local actions = { ... } | |
if #actions == 1 then | |
return actions[1] | |
end | |
local pre_action = actions[1] | |
for i = 2, #actions do | |
pre_action = CCSequence:createWithTwoActions(pre_action, actions[i]) | |
end | |
return pre_action | |
end | |
function spwarnActions(...) | |
local actions = { ... } | |
if #actions == 1 then | |
return actions[1] | |
end | |
local pre_action = actions[1] | |
for i = 2, #actions do | |
pre_action = CCSpawn:createWithTwoActions(pre_action, actions[i]) | |
end | |
return pre_action | |
end | |
-- 在指定的node上同时执行一个或多个给定的action, | |
-- 当所有action执行完成后从函数返回. | |
-- ccPerformActions(node,actions,[node,actions,]...) | |
-- actions is a array like {action,action,action,...} | |
-- WARNING: 仅能在协作线程中使用 | |
function coPerformActions(...) | |
local co = coroutine.running() | |
assert(co, 'coPerformActions should only be used in a coroutine.') | |
local nodeActions = { ... } | |
assert(#nodeActions % 2 == 0 and #nodeActions > 0) | |
local max_duration = -1 | |
local max_duration_node | |
for ii = 1, #nodeActions, 2 do | |
local node = nodeActions[ii] | |
local actions = nodeActions[ii + 1] | |
if type(actions) ~= 'table' then | |
actions = { actions } | |
end | |
for _,action in ipairs(actions) do | |
local d = action:getDuration() | |
if d > max_duration then | |
max_duration = d | |
max_duration_node = node | |
end | |
node:runAction(action) | |
end | |
end | |
local delayAction = CCDelayTime:create(max_duration) | |
local callfuncAction = CCCallFunc:create(function() | |
coroutine.resume(co) | |
end) | |
local sequenceAction = CCSequence:createWithTwoActions(delayAction, callfuncAction) | |
max_duration_node:runAction(sequenceAction) | |
coroutine.yield() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment