you can find out what is happening in your code by putting rect.x=W/2 at the begging of your code as:
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)
Now, you can solve this by the following code(I have used a variable named: incrementVal
just for your convenience, for understanding the relation about rect size and position in your code):
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)
Keep coding.............. :)