2013-04-04 60 views
0

如何更新或操作嵌套在多個組中的精靈?更新組中嵌套的精靈

我第一次嘗試直接與精靈(沒有成功)

我的代碼

local box = {} 

-- PUBLIC FUNCTIONS 

function box:new() 

    local group = display.newGroup() 

    local image = display.newImage("crate.png") 

    group:insert(image) 

    function group:update() 
     image.rotation = image.rotation + 1 
    end 

    return group 
end 

return box 

其次,我添加了「enterFrame事件」事件所需的類,但是加入了

Runtime:addEventListener("enterFrame", enterFrame) 

,然後遍歷添加到場景中的組(scene.view),然後調用一個自定義更新函數,向前發送更新,直到它更新他是我的班級。但是,我還沒有找到一種方法來檢查組是否有更新方法。現在我只是致電更新,即使該組沒有它。

function enterFrame (event) 
    local group = scene.view 
    for i=1,group.numChildren do 
     group[i]:update() 
    end 
end 

回答

1

本地箱= {}

你可以試試這個簡單的解決方案

-- PUBLIC FUNCTIONS 

function box:new() 

    local group = display.newGroup() 

    local image = display.newImage("crate.png") 

    group:insert(image) 
    group.isThereUpdate = true 
    // or group.isThereUpdate = false 

    function group:update() 
     image.rotation = image.rotation + 1 
    end 

    return group 
end 

return box 


function enterFrame (event) 
    local group = scene.view 
    for i=1,group.numChildren do 
     if group[i].isThereUpdate then 
      group[i]:update() 
     end 
    end 
end 
+0

效果很好,你不知道如何使用'運行:的addEventListener( 「enterFrame事件」,enterFrame事件)'將偵聽器直接設置爲更新函數'group:update'? – Sunkas 2013-04-05 07:35:06

+0

你的意思是,類似於運行時:addEventListener(「enterFrame」,group:update)? – 2013-04-05 07:56:00

+0

是「類似的東西」。它給了我一個函數參數,期望在'附近')'' - 我想我的新方法 – Sunkas 2013-04-05 08:11:04