0

How can I save a DisplayObjects to be reused between scenes?

Example:

scene1 contains the DisplayObject drawing.

In scene:exitScene I save it to storyboard.state.scene1.drawing

Then when I get back to my scene in scene:enterScene I do:

drawing = storyboard.state.scene1.drawing
self.view:insert(drawing)

But I get an error as if drawing was invalid.

4

2 回答 2

0

鉴于您将对显示对象的引用存储在变量中,我会这样做

没有经过测试,但这个想法应该可行。

FromScene:

local displayObj=yourDisplayObject

-- when it is time to change scene, do it like this:

local options={
      local params ={
      dispObj = displayObj,
    },
}
storyboard.gotoscene(targetScene, options)


---
targetScene:

scene:createScene(event)
    local params=event.params
    local displayObj=params.dispObj
    --and then do whatever you want with displayObj
...
...
end

以这种方式执行此操作要求您不要取消或删除第一个场景的destroyscene 中的显示对象。

于 2013-07-21T22:02:46.830 回答
0

如果你想在不同的场景中重用对象,你可以通过创建一个对象 lua 文件来做到这一点,例如我将创建一个矩形并将其保存为 RectObject.lua

local H = display.contentHeight
local W = display.contentWidth
local myRect

myObject = {}

--the ScreenGroup parameter is the group from the scene where this object calls
function myObject:drawRect(ScreenGroup)
myRect = display.newRect(W/2 + 50, H/2, 50,50)
myRect:setFillColor(255,255,128)
ScreenGroup:insert(myRect)

end

所以当我去我的场景时,只要我需要它,我就会像这样调用它

require("RectObject.lua")

function scene:enterScene( event )
    local group = self.view
    --calling Object from RectObject.lua
    myObject:drawRect(group) 

end

注意:您需要将组传递给对象,以便在退出时将其移除到场景中。

于 2013-07-22T02:12:32.350 回答