使用循环创建具有不同偏移量的对象
Use loop to create objects with different offsets
这是创建 4 个对象的 for 循环:
for i=1,4 do
local obj = display.newRect(sceneGroup, 40, 60)
obj.y = (obj.height + 80) * i
end
此代码创建四个对象,起点为 60(obj.height),每个对象之间的间距为 80。
我想做的是改变起点但保持它们之间相同的差距(80),这意味着我希望起点为 250 而不是 60。我尝试了不同的方法但无法获得对的。
来自 Corona documentation 关于 display.newRect()
Overview
Creates a rectangle object. The local origin is at the center of the
rectangle and the anchor point is initialized to this local origin.
Syntax
display.newRect( [parent,] x, y, width, height )
尝试
local x, y = display.contentWidth * 0.5, 250-- decide where to put first rectangle
local obj = {} -- reference for futher use
for i=1, 4 do
obj[i] = display.newRect(x, y, 40, 60)
y = y + obj[i].height + 80
end
这是创建 4 个对象的 for 循环:
for i=1,4 do
local obj = display.newRect(sceneGroup, 40, 60)
obj.y = (obj.height + 80) * i
end
此代码创建四个对象,起点为 60(obj.height),每个对象之间的间距为 80。
我想做的是改变起点但保持它们之间相同的差距(80),这意味着我希望起点为 250 而不是 60。我尝试了不同的方法但无法获得对的。
来自 Corona documentation 关于 display.newRect()
Overview
Creates a rectangle object. The local origin is at the center of the rectangle and the anchor point is initialized to this local origin.
Syntax
display.newRect( [parent,] x, y, width, height )
尝试
local x, y = display.contentWidth * 0.5, 250-- decide where to put first rectangle
local obj = {} -- reference for futher use
for i=1, 4 do
obj[i] = display.newRect(x, y, 40, 60)
y = y + obj[i].height + 80
end