试图将 nil 与数字进行比较,即 (i).x < 100。

我正在尝试在旧的 display.newRect 小于 100 时生成一个新的,但是我得到了一个“试图比较空值和数字”的错误。

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 )
点赞
用户1925928
用户1925928

你的计时器调用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 )
2014-04-09 14:45:04
用户869951
用户869951

计时器调用侦听器并将事件作为参数传递,因此 i 是一个事件。由于 i 是全局变量,所以参数可以直接为事件 event

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

请注意,我使用 hollSpawn 而不是 hollspawn,因为我认为这可能是一个导致其他错误的拼写错误。

关于风格的注释:不确定为什么需要在 i 周围加上 (),这不符合 Lua 风格。此外,你可能应该将 i 声明为模块本地变量:

local i

screenGroup = self.view
holl = {}
hollSpawn = function()
    i = display.newRect( 0, 0, math.random(10, 500), 53 )
    ...
2014-04-09 18:54:59