简体   繁体   中英

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'

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. 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:

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.

Sidenote on style: not sure why you need () around i , un-Lua'ish. Also you probably should declare i local to your module:

local i

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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