[Resolved] Compare Nil to Number error

In my callGameOver = function, when I click on the menu, restart or next button I get an attempt to compare nil to number in the createLevel = function
at

  
 if claw.x \< 116 then  
 claw.x = 116  
 end  
  

Now it works fine with the callGameOver = function in the createLevel = function but one of my levels has an upvalues error in the createLevel = function, so I’m trying to make it smaller, and this is one of my attempts to do so.

my callGameOver = function is complete here, and I removed most everything in the createLevel = function.

[code]
local callGameOver = function( isWin )

local isWin = isWin
shadeRect = display.newRect( 0, 0, screenW, screenH )
shadeRect:setFillColor( 0, 0, 0, 255 )
shadeRect.alpha = 1
if isWin == “yes” then
gameOverDisplay = display.newImageRect( “images/youwin.png”, 390, 154 )
else
gameOverDisplay = display.newImageRect( “images/youlose.png”, 390, 154 )
end
gameOverDisplay.x = 240; gameOverDisplay.y = 165
gameOverDisplay.alpha = 0

gameIsActive = true
physics:pause()
– MENU BUTTON
local onMenuTouch = function( event )
if event.phase == “release” then

if audioset == 1 then
audio.play( tapSound )
end
director:changeScene( “menu” )
for i = game.numChildren,1,-1 do
local child = game[i]
child.parent:remove( child )
child = nil
end
for h = hudGroup.numChildren,1,-1 do
local child = hudGroup[h]
child.parent:remove( child )
child = nil
end

director:changeScene(“menu”,“fade”)
Runtime:removeEventListener( “enterFrame”, move )
Runtime:removeEventListener(“enterFrame”, wrap)
Runtime:removeEventListener( “enterFrame”, moveCamera )
Runtime:removeEventListener(“enterFrame”, offset)

end
end

local backbutton = ui.newButton{
defaultSrc = “images/menubtn.png”,
defaultX = 60,
defaultY = 60,
overSrc = “images/menubtn-over.png”,
overX = 60,
overY = 60,
onEvent = onMenuTouch,
id = “MenuButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}

backbutton.x = 240
backbutton.y = 186
backbutton.alpha = 0

– RESTART BUTTON
local onRestartTouch = function( event )
if event.phase == “release” then

if audioset == 1 then
audio.play( tapSound )
end
for i = game.numChildren,1,-1 do
local child = game[i]
child.parent:remove( child )
child = nil
end
for h = hudGroup.numChildren,1,-1 do
local child = hudGroup[h]
child.parent:remove( child )
child = nil
end

director:changeScene(“loadlevel2”,“fade”)
Runtime:removeEventListener( “enterFrame”, move )
Runtime:removeEventListener(“enterFrame”, wrap)
Runtime:removeEventListener( “enterFrame”, moveCamera )
Runtime:removeEventListener(“enterFrame”, offset)
end
end

local restartbutton = ui.newButton{
defaultSrc = “images/restartbtn.png”,
defaultX = 60,
defaultY = 60,
overSrc = “images/restartbtn-over.png”,
overX = 60,
overY = 60,
onEvent = onRestartTouch,
id = “RestartButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}

restartbutton.x = backbutton.x + 72; restartbutton.y = 186
restartbutton.alpha = 0

– NEXT BUTTON
local onNextTouch = function( event )
if event.phase == “release” then
if audioset == 1 then
audio.play( tapSound )
end
for i = game.numChildren,1,-1 do
local child = game[i]
child.parent:remove( child )
child = nil
end
for h = hudGroup.numChildren,1,-1 do
local child = hudGroup[h]
child.parent:remove( child )
child = nil
end
local theModule = “load” … nextModule
director:changeScene( theModule )
Runtime:removeEventListener( “enterFrame”, move )
Runtime:removeEventListener(“enterFrame”, wrap)
Runtime:removeEventListener( “enterFrame”, moveCamera )
Runtime:removeEventListener(“enterFrame”, offset)
end
end

local nextBtn = ui.newButton{
defaultSrc = “images/nextlevelbtn.png”,
defaultX = 60,
defaultY = 60,
overSrc = “images/nextlevelbtn-over.png”,
overX = 60,
overY = 60,
onEvent = onNextTouch,
id = “NextButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}

nextBtn.x = restartbutton.x + 72; nextBtn.y = 186
nextBtn.alpha = 0
if isWin ~= “yes” then nextBtn.isVisible = false; end

hudGroup:insert( shadeRect )
hudGroup:insert( fbBtn )
hudGroup:insert( gameOverDisplay )
hudGroup:insert( backbutton )
hudGroup:insert( restartbutton )
if isWin == “yes” then hudGroup:insert( nextBtn ); end
transition.to( shadeRect, { time=200, alpha=0.65 } )
transition.to( gameOverDisplay, { time=500, alpha=1 } )
transition.to( backbutton, { time=500, alpha=1 } )
transition.to( restartbutton, { time=500, alpha=1 } )
transition.to( nextBtn, { time=500, alpha=1 } )
if isWin == “yes” then transition.to( nextBtn, { time=500, alpha=1 } ); end

if isWin == “yes” then
local baseScore = score

local orbBonus = gameLives * 2000
local temp
local temp = score
score = temp + orbBonus

if bestScore < score then
bestScore = score
–print(“new highscore”…score )

local sql = “UPDATE gamestats SET content2=” …bestScore … " WHERE content=‘level2score’;"
db:exec(sql)

end

local oldScoreText = baseScore
tscore = display.newText( “0”, 10, 300, native.systemFont, 32 )
tscore.text = “Score: " … oldScoreText…” + Bonus: “…orbBonus…”\nFinal Score: "…score
tscore.xScale = 0.5; tscore.yScale = 0.5 --> for clear retina display text
tscore.x = (480 - (tscore.contentWidth * 0.5)) - 30
tscore.y = 30
tscore:toFront()
timer.performWithDelay( 1000, function() tscore.isVisible = true; end, 1 )
hudGroup:insert( tscore )
else
tscore:removeSelf()
tscore = nil
end

– MAKE SURE BEST SCORE TEXT IS VISIBLE
bestScoreText = display.newText( “0”, 10, 300, native.systemFont, 32 )
bestScoreText:setTextColor( 228, 228, 228, 255 ) --> white
bestScoreText.text = "Best Score For This Level: " … bestScore
bestScoreText.xScale = 0.5; bestScoreText.yScale = 0.5 --> for clear retina display text
bestScoreText.x = (bestScoreText.contentWidth * 0.5) + 15
bestScoreText.y = 304

hudGroup:insert( bestScoreText )
end

local createLevel = function()


--------- Move claw

– CREATE ANALOG STICK
MyStick = StickLib.NewStick(
{
borderImage = “images/stick_border.png”, – IF NOT PROVIDED, A CIRCLE SHAPE WILL BE USED
thumbImage = “images/stick_thumb.png”, – IF NOT PROVIDED, A CIRCLE SHAPE WILL BE USED
x = screenW*.5,
y = screenH*.9,
thumbSize = 11,
borderSize = 24,
snapBackSpeed = .09,
R = 55,
G = 255,
B = 255
} )
game:insert( MyStick )


--------- claw

local claw = display.newImage( “images/claw2.png” )
game:insert( claw )
physics.addBody( claw, “static”, { density=3.0,friction=0.5, bounce=3.2, shape=clawshape })
claw.x = 170
claw.y = 10
claw.name = “claw”

local function move(event )
if ( not gameIsActive ) then
MyStick:move(claw, 7.0, false)
end
end
Runtime:addEventListener( “enterFrame”, move )

local function wrap (event) – Keep claw Confined

if claw.x < 116 then
claw.x = 116
end

if claw.x > 220 then
claw.x = 220
end

if claw.y < -74 then
claw.y = -74
end

if claw.y > 60 then
claw.y = 60
end

end
Runtime:addEventListener(“enterFrame”, wrap)
end
[/code] [import]uid: 78446 topic_id: 15584 reply_id: 315584[/import]

Try adding a nil check to your logic statement:

if claw.x ~= nil and claw.x < 116 then
claw.x = 116
end [import]uid: 10903 topic_id: 15584 reply_id: 57533[/import]

Can you try a print statement right after you remove the wrap listener to ensure it is in fact being removed? A glance over your code suggests it might not be. Let me know; if that is the case it’s going to be a fairly easy fix :slight_smile: [import]uid: 52491 topic_id: 15584 reply_id: 57586[/import]

In my print I’m getting an attempt to to index upvalue ‘claw’ a nil value in function ‘onEvent’ [import]uid: 78446 topic_id: 15584 reply_id: 57633[/import]

Thanks crssmn,
Using this worked.

if claw.x ~= nil and claw.x \< 116 then  
claw.x = 116  
end  

Dan [import]uid: 78446 topic_id: 15584 reply_id: 58119[/import]

Okay, I am sorry for bring this issue back again, but I having a similar problem with Runtime:removeEventListener("enterFrame", myFunction) . I have a slingshot example I am working off of (Fixedit’s example of slingshot). It’s working fine, but the problem is that when I want to change scenes, using director, doesn’t matter what I do, I am always getting attempt to compare number with nil .
Here’s the function that is attached to Runtime

 --Function to see when the ball touch the screen limit, if yes, stop any angular and linear velocity  
local function toucheLimite( event )  
  
 if ball.x \> display.contentWidth+200 or ball.x \< -200 or ball.y \< -200 or ball.y \> display.contentHeight+200 then  
 ball:setLinearVelocity(0,0)  
 ball.angularVelocity = 0  
 ball.x = \_x+ ballXValue  
 ball.y = \_y + ballYValue  
  
end   
  
end  
  
--Event listeners  
 Runtime:addEventListener("enterFrame", toucheLimite)  
  

I am trying to terminated toucheLimite(event) function when hit a button as follows:

[code]
local function onMenuButton(event)

if (event.phase == “release” and menu_button.isActive) then

timer.performWithDelay(2990, function() director:changeScene(“mainMenu”, “crossfade”) end, 1)

end
end
—BUTTON—
menu_button = ui.newButton{
defaultSrc = “menu_button.png”,
defaultX = 90,
defaultY = 40,
overSrc = “menu_button.png”,
overX = 88,
overY = 39,
onEvent = onMenuButton,
id = “menu_button”,
text = “”,
font = “Verdana”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false

}

[/code].

Why isn’t this working as expected? Everytime I try to remove the event listener, I get Runtime errors.

Any help will be highly appreciated, please.

Thanks [import]uid: 75258 topic_id: 15584 reply_id: 97274[/import]

You do need to remove the listener.

Where were you trying to remove it when you got errors? Immediately before director:changScene() should do the trick. [import]uid: 52491 topic_id: 15584 reply_id: 97286[/import]

Hello Peach!

Yes, I remove it right before director:ChangeScene(), but I still get the same error. (Sorry I forgot to include it in the code I posted) but it’s like the following:

  
 local function onMenuButton(event)  
  
 if (event.phase == "release" and menu\_button.isActive) then  
  
 Runtime:removeEventListener("enterFrame", toucheLimite)   
 timer.performWithDelay(2990, function() director:changeScene("mainMenu", "crossfade") end, 1)  
  
 end   
 end  

I have switched things around hoping it would work, but it didn’t. I ma stuck. [import]uid: 75258 topic_id: 15584 reply_id: 97366[/import]

@pdichone : is your “toucheLimite” function declared above your button function? If not there is the problem [import]uid: 84637 topic_id: 15584 reply_id: 97383[/import]

Oh my goodness @Danny!! You are completely right! It did the trick! How can one be so oblivious of something so obvious!!! Grrr…!

Thank you very much Danny. Highly appreciated. Thank you. You saved me from a lot of unnecessary grief caused by stupidity on my part.Ugh!! [import]uid: 75258 topic_id: 15584 reply_id: 97408[/import]

Very welcome :slight_smile: [import]uid: 84637 topic_id: 15584 reply_id: 97415[/import]