更新嵌套在组内的精灵。

如何更新或操作嵌套在多个组中的精灵?

我最初尝试将“enterFrame”事件直接添加到带有精灵的所需类中(没有成功)

我的代码

local box = {}

-- 公共函数

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

然后我考虑在我的场景中添加

Runtime:addEventListener("enterFrame", enterFrame)

然后循环遍历添加到场景中(scene.view)的组,并在那里调用一个自定义的update函数,直到它到达我的类。 但是,我还没有找到一种检查组是否具有更新方法的方法。 现在,即使组没有更新方法,我也会调用更新。

function enterFrame (event)
    local group = scene.view
    for i=1,group.numChildren do
        group[i]:update()
    end
end
点赞
用户2186639
用户2186639
local box = {}

您可以尝试这个简单的解决方案

-- 公共函数

function box:new( )

local group = display.newGroup()

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

group:insert(image)
group.isThereUpdate = true
// 或者 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 ```

2013-04-04 17:00:45