I would like to expand my rect in width increasing by 1 and want it to stop when it reaches the screen width. However, It stops increasing in the middle of the screen in my code. Can you tell me please what I am missing?
W=display.contentWidth
H=display.contentHeight
local rect = display.newRect(0,0,0,100)
rect:setFillColor(0,255,0)
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)
Not tested but this should work.
W=display.contentWidth
H=display.contentHeight
local rect = display.newRect(0,0,0,100)
rect:setFillColor(0,255,0)
local function expand()
rect.width= rect.width+1
rect.x=0
print(rect.width)
if rect.width==W then
Runtime :removeEventListener("enterFrame", expand)
end
end
Runtime: addEventListener("enterFrame", expand)
All views in corona has topleft reference point as default, meaning that if you position them at (0,0,0,100) they will start at the top left corner with a height of 100pixels. The x value of the view(rect in this case) will be at its' left side.
Increasing the width of this rectangle will not change the position of the rectangle. Just make it wider. Therefore half of the increase in width is found beyond the screen, to the left in this case.