在函数中添加到场景组?

Adding to sceneGroup within a function?

这是我一次又一次遇到的问题。我只是无法理解它。这段代码在创建场景下,现在我想把它放在一个函数中,其想法是通过循环自动在不同位置生成这些块。我得到的错误是: newRect 中的错误参数 #1(预期数量)- 第一行。 sceneGroup 是一个 nil 值。

我尝试过的解决方案是: 1) 在脚本顶部定义场景组。但是后来我得到错误,upvalue 是一个 nil 值。 2)之前定义它。 - 零值。

如果有人能向我解释一下,我将不胜感激。我一直遇到这样的问题。

local Backgroundrectangle = display.newRect(sceneGroup, 75, 75, display.contentWidth-150, display.contentHeight/2 )
Backgroundrectangle.isVisible = false
Backgroundrectangle.anchorX = 0
Backgroundrectangle.anchorY = 0

 aAbackground = display.newRoundedRect(sceneGroup, Backgroundrectangle.x, Backgroundrectangle.y, 100, 125, 10 )
sceneGroup:insert(aAbackground)
aAbackground.id = "a"
aAbackground.strokeWidth = 2
aAbackground:setFillColor( gradient )
aAbackground:setStrokeColor( 0.2 )
aAmenutext = display.newText( "Aa", 100, 200, "Comic Sans MS", 50)
aAmenutext.x = aAbackground.x
aAmenutext.y = aAbackground.y - aAbackground.height/6
aAmenutext:setFillColor( 0.2 )
sceneGroup:insert(aAmenutext)

"Upvalue is nil" 意味着运行时期望 sceneGroup 是在函数范围之外定义的局部变量,但这不是 Corona 中通常的做法。

如果你有这个

local composer = require( "composer" )
local scene = composer.newScene()

在场景的 Lua 文件的顶部,每当您想将 DisplayObject 添加到场景的 GroupObject 时(可能在您的 scene:create() 方法中),您可以声明

local sceneGroup = scene.view

然后像以前一样使用 sceneGroupscene 将被定义(它具有文件范围)并且 view 属性 为您提供场景的 GroupObject。