简体   繁体   English

随机错误经常发生:尝试将nil与数字Corona SDK进行比较

[英]Random error occurs every so often: attempt to compare nil with number Corona SDK

Every so often my app will throw the following error: 我的应用程序每隔一段时间就会抛出以下错误:

gamePlay.lua:121: attempt to compare nil with number gamePlay.lua:121:尝试将nil与数字进行比较

The function moves an object from the right of the screen to the left. 该功能将对象从屏幕的右侧移至左侧。 When it goes 50 pixels offscreen then the object should remove. 当它离开屏幕50个像素时,该对​​象应移除。 Here is my code for the method: 这是我的方法代码:

function moveObject(self,event)

        if (self.x)<-50 then --this is line 121
            --remove object
            Runtime:removeEventListener("enterFrame", event.self)
            display.remove(event.target)

        else
            self.x = self.x-self.speed --move object left at value speed
        end
    end

The error pops up when this method is called and it goes to the restart screen: 调用此方法时,错误弹出,并进入重新启动屏幕:

------when ball hits another object go to restart scene------------ ------当球碰到另一个物体时,重新开始场景------------

    local function onCollision( self,event )

        if(event.object2.name == "bonus")then--if we hit a bonus ball
            event.object2:removeSelf()

            --set score
            local currentScore =game.returnScore()
            game.setScore(currentScore*2)
            scoreText.text = game.returnScore()

            Runtime:removeEventListener("enterFrame", event.object2)

        else                

            composer.gotoScene("restart")   
            Runtime:removeEventListener("touch", onObjectTouch)    
        end
    end
    ball.collision = onCollision
    Runtime:addEventListener( "collision", ball )

Any ideas what could be happening? 有什么想法会发生什么吗? It is quite rarely that the error occurs, maybe 1 in 15 goes. 很少会发生错误,可能有15分之一。

I think it has to do with your event listeners. 我认为这与您的事件监听器有关。 I sometimes find with Corona that event listeners still run even after a scene has changed. 有时,我在Corona中发现即使场景已更改,事件侦听器仍然可以运行。 I'm guessing that the ball is cleaned up (removed) before the event listener is stopped, and that every now and then the listener is still being run before the listener is cleaned up 我猜想在事件监听器停止之前清理(移除)了球,并且在清理监听器之前,时不时地监听器仍在运行

To fix it, you could try 要修复它,您可以尝试

  1. removing the listener first, ball second 首先移开听众,然后移开球
  2. simply adding a null check before the if check 只需在if检查之前添加null检查

     if (self ~= null) then if (self.x)<-50 then --this is line 121 --remove object Runtime:removeEventListener("enterFrame", event.self) display.remove(event.target) 

I've solved a similar problem by not just checking if "self" is nil, but checking if "self.x" is nil. 我不仅通过检查“ self”是否为nil,还通过检查“ self.x”是否为nil,解决了类似的问题。 So... 所以...

if self ~= nil and self.x ~= nil and self.x < -50 then 如果self〜= nil和self.x〜= nil和self.x <-50那么

I'm not entirely sure why this works but I'm guessing corona is removing the display object properties and not the object itself. 我不完全确定为什么可以这样做,但是我猜电晕正在去除显示对象的属性,而不是对象本身。

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

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