main.lua 71 attemp to index global piruleta1 a nil value

Hey,

Why appear this error? I was finding a possible solution but I not found nothing, please can you help me to resolved the problem?

This is the code, in “theory” when the bullet collision with piruleta1, this would be remove, only one. Thanks in advance!

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local math, physics = require("math"), require('physics') physics.start() physics.setGravity( 0, 0 ) display.setStatusBar( display.HiddenStatusBar ) local screenW, screenH = display.contentWidth, display.contentHeight local playerX, playerY = (screenW / 2), (screenH / 1) local player = display.newRect( 0, 0, 30, 30 ) player.x = playerX player.y = playerY local function onScreenTouch( event ) if (event.phase == "began") then speed = 100 deltaX = event.x - playerX deltaY = event.y - playerY normDeltaX = deltaX / math.sqrt(math.pow(deltaX,2) + math.pow(deltaY,2)) normDeltaY = deltaY / math.sqrt(math.pow(deltaX,2) + math.pow(deltaY,2)) angle = math.atan2( deltaY, deltaX ) \* 180 / math.pi -- angleT.text = angle bullet = display.newRect( 0, 0, 6, 6 ) bullet.x = playerX bullet.y = playerY physics.addBody( bullet) bullet.myName = "bala" bullet:setLinearVelocity( normDeltaX \* speed, normDeltaY \* speed ) end end local function onCollision( event ) if ( event.phase == "ended" ) then print( "objeto1: " .. event.object1.myName .. " onjeto2 " .. event.object2.myName ) if event.object1.myName == "piruleta1" then if( piruleta1.removeSelf ~= nil ) then piruleta1:removeSelf() bullet:removeSelf() end end end end Runtime:addEventListener( "collision", onCollision ) Runtime:addEventListener( "touch", onScreenTouch ) local function spawnObject() piruleta1 = display.newRect( 0, 0, 30, 30 ) piruleta1.x = math.random(10,50) piruleta1.y = math.random(10,50) physics.addBody( piruleta1, "static" ) piruleta1.myName = "piruleta1" end timer.performWithDelay(1,spawnObject,3)

Hi @Kiritosuu,

In this code, “piruleta” is still a global variable, which should be avoided in 99.99% of cases. When spawning objects, you shouldn’t just re-assign new objects to the same global variable, because Lua will not know the reference of those previously created. This is likely why you’re getting the error.

Take care,

Brent

Hi @Brent ! Thank you very much for your reply, do you have any tutorial for learn like re-assign new object to the global variable? Or a example code for example. Thank you very much!! :slight_smile:

Cheers,

Kiritosuu

Here are a few tutorials that should help you understand scope and globals a bit better.

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

http://coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/

http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

This video is kind of long, but may be helpful:

https://www.youtube.com/watch?v=b12YEOIry60

Hi @Kiritosuu,

In this code, “piruleta” is still a global variable, which should be avoided in 99.99% of cases. When spawning objects, you shouldn’t just re-assign new objects to the same global variable, because Lua will not know the reference of those previously created. This is likely why you’re getting the error.

Take care,

Brent

Hi @Brent ! Thank you very much for your reply, do you have any tutorial for learn like re-assign new object to the global variable? Or a example code for example. Thank you very much!! :slight_smile:

Cheers,

Kiritosuu

Here are a few tutorials that should help you understand scope and globals a bit better.

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

http://coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/

http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

This video is kind of long, but may be helpful:

https://www.youtube.com/watch?v=b12YEOIry60