您可以通过将 rect.x=W/2 放在代码的请求处来了解代码中发生了什么:
W=display.contentWidth
H=display.contentHeight
local rect = display.newRect(0,0,0,100)
rect:setFillColor(0,255,0)
rect.x = W/2 -- just put this in your code and see what actually happening
local function expand()
rect.width= rect.width+1
print(rect.width)
if rect.width==W then
Runtime :removeEventListener("enterFrame", expand)
end
end
Runtime: addEventListener("enterFrame", expand)
现在,您可以通过以下代码解决此问题(我使用了一个名为:的变量incrementVal
,只是为了您的方便,以了解代码中矩形大小和位置的关系):
W=display.contentWidth
H=display.contentHeight
local rect = display.newRect(0,0,0,100)
rect:setFillColor(0,255,0)
local incrementVal = 1
local function expand()
rect.width= rect.width+incrementVal
rect.x = rect.x + (incrementVal/2) -- additional line, added for proper working
if rect.width==W then
Runtime :removeEventListener("enterFrame", expand)
end
end
Runtime: addEventListener("enterFrame", expand)
继续编码........ :)