Lua EnterFrame事件在Corona早期停止

Lua EnterFrame事件在Corona早期停止,lua,coronasdk,enterframeevent,Lua,Coronasdk,Enterframeevent,我想扩大我的矩形宽度增加1,并希望它停止时,它达到屏幕宽度。但是,它在我的代码中停止在屏幕中间。你能告诉我我错过了什么吗 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)

我想扩大我的矩形宽度增加1,并希望它停止时,它达到屏幕宽度。但是,它在我的代码中停止在屏幕中间。你能告诉我我错过了什么吗

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)

没有测试,但这应该是可行的

    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)
corona中的所有视图都默认为左上角参照点,这意味着如果将它们定位在(0,0,0100)处,它们将从左上角开始,高度为100像素。视图的x值(本例中为rect)将位于其“左侧”


增加此矩形的宽度不会更改矩形的位置。把它弄宽一点。因此,在这种情况下,宽度增加的一半超出屏幕左侧。

未测试,但这应该可以工作

    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)
corona中的所有视图都默认为左上角参照点,这意味着如果将它们定位在(0,0,0100)处,它们将从左上角开始,高度为100像素。视图的x值(本例中为rect)将位于其“左侧”


增加此矩形的宽度不会更改矩形的位置。把它弄宽一点。因此,在这种情况下,宽度增加的一半会出现在屏幕之外的左侧。

您可以通过将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
的变量,用于理解代码中rect大小和位置的关系):


继续编码………..)

您可以通过在代码请求处放置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
的变量,用于理解代码中rect大小和位置的关系):


继续编码………..)

现在,这完全有道理。谢谢你的启示现在,这完全有道理。谢谢你的启示