简体   繁体   English

尝试将nil与(i)上的数字进行比较.x <100然后

[英]Attempt to compare nil with number on (i).x < 100 then

I am trying to spawn a new display.newRect if to old one is < 100 but I'm getting a 'Attempt to compare nil with number' 我正在尝试生成一个新的display.newRect如果到旧的显示是<100,但是我得到一个“试图将nil与数字进行比较”

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

I cant see the error, could someone please explain how to fix this ? 我看不到错误,有人可以解释一下解决方法吗?

Fullcode : 完整代码:

 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 )

- -

- -

new test still not working 新测试仍然无法正常工作

 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 )

Your timer calls hollSpawne with no arguments, but in function you use 'i' parameter. 您的计时器不带任何参数调用hollSpawne,但在函数中使用“ i”参数。 Try this one: 试试这个:

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

The timer calls the listener with event as argument so i is an event. 计时器使用事件作为参数调用侦听器,因此我是一个事件。 Since i is a global you can just have the arg be event: 由于我是一个全球性人士,因此您可以使用arg be事件:

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

Note that I use hollSpawn not hollspawn as I think this is a typo that will cause additional bug. 请注意,我使用的不是hollspawn ,而是我使用的hollSpawn因为我认为这是一个错字,会导致其他错误。

Sidenote on style: not sure why you need () around i , un-Lua'ish. 风格的旁注:不确定为什么需要在() i ()地方使用() Also you probably should declare i local to your module: 另外,您可能应该在模块本地声明i

local i

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM