【问题标题】:Attempt to compare nil with number on (i).x < 100 then尝试将 nil 与 (i).x < 100 上的数字进行比较然后
【发布时间】:2014-05-22 20:35:26
【问题描述】:

如果旧的 display.newRect

function hollSpawne(i)
    if (i).x < 100 then
         hollspawn()
    end
end
HollControll = timer.performWithDelay(  1400 , hollSpawne, 0 )

我看不到错误,谁能解释一下如何解决这个问题?

完整代码:

 function pluspoint(i)
 score = score + 1
 display.remove(i)
 end

 screenGroup = self.view
 holl = {}
 hollSpawn = function()

    i = display.newRect( 0, 0, math.random(10, 500), 53 )
    i.x = display.contentWidth + i.contentWidth + 10
    i.y = display.contentHeight - 53/2
    i:setFillColor( 1, 0, 0 )
    i.name = "hollgameover"
    physics.addBody(i, "static", {density=.1, bounce=0.5, friction=.2,filter=playerCollisionFilter } )      
    trans55 = transition.to(i,{time=2000, x=display.contentWidth - display.contentWidth - i.contentWidth/2 - 20, onComplete=pluspoint, transition=easing.OutExpo } )
    holl[#holl+1] = i
    screenGroup:insert(i)
end 
timereholl = timer.performWithDelay(  100 , hollSpawn, 1 )

function hollSpawne(i)
    if (i).x < 100 then
         hollspawn()
    end
end
HollControll = timer.performWithDelay(  1400 , hollSpawne, 0 )

-

-

新测试仍然无法正常工作

 screenGroup = self.view
 holl = {}
 hollSpawn = function()

    i = display.newRect( 0, 0, math.random(10, 500), 53 )
    i.x = display.contentWidth + i.contentWidth + 10
    i.y = display.contentHeight - 53/2
    i:setFillColor( 1, 0, 0 )
    i.name = "hollgameover"
    physics.addBody(i, "static", {density=.1, bounce=0.5, friction=.2,filter=playerCollisionFilter } )      
    trans55 = transition.to(i,{time=2000, x=display.contentWidth - display.contentWidth - i.contentWidth/2 - 20, onComplete=pluspoint, transition=easing.OutExpo } )
    holl[#holl+1] = i
    screenGroup:insert(i)

end 
timereholl = timer.performWithDelay(  100 , hollSpawn, 1 )

function hollSpawne(event)
    if (i).x < 100 then
         hollSpawn()
    end
end
HollControll = timer.performWithDelay( 1400 , hollSpawne, 0 )

【问题讨论】:

  • 我还能告诉你在哪里用 hollSpawne 中的“i”替换“(i)”?

标签: lua coronasdk


【解决方案1】:

你的计时器调用没有参数的 hollSpawne,但在函数中你使用了 'i' 参数。 试试这个:

local function hollSpawne(i)
    if i.x < 100 then
         hollspawn()
    end
end
HollControll = timer.performWithDelay(  1400 , function()
    hollSpawne(my_i_value)
end, 0 )

【讨论】:

  • ok,调用了“hollSpawn(my_i_value)”,但是这部分仍然没有工作“if (i).x
【解决方案2】:

计时器以事件作为参数调用侦听器,因此 i 是一个事件。由于 i 是全局变量,因此您可以将 arg 设为事件:

function hollSpawne(event)
    if (i).x < 100 then
         hollSpawn()
    end
end

请注意,我使用 hollSpawn 而不是 hollspawn,因为我认为这是一个会导致额外错误的错字。

风格旁注:不知道为什么你需要()i,un-Lua'ish。此外,您可能应该在您的模块中声明 i 本地:

local i

screenGroup = self.view
holl = {}
hollSpawn = function()
    i = display.newRect( 0, 0, math.random(10, 500), 53 )
    ...

【讨论】:

  • 错误不再出现,但它没有执行“hollspawn()”
  • 我在原始问题中添加了一些代码,但仍然无法正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 2017-03-19
  • 2021-10-13
  • 2018-07-31
  • 2016-04-22
相关资源
最近更新 更多