Lua collision nil value - lua

When I tried to execute the following piece of code it gives me this error:
Attempt to index field 'other' (a nil value)
but I don't know why.
The code:
function onCollision(event)
if event.phase == "began" then
if event.other.star == "star" then
score = score + 1
elseif event.other.mine1 == "mine1" then
if jet.collided == false then
timer.cancel(tmr)
jet.collided = true
jet.bodyType = "static"
explode()
end
end
end
end
Thanks in advance :)

As #lhf and #RBerteig said the problem is event.other is nil, so trying to access the star member fails attempting to index a nil value.
Assuming event.other can indeed be nil, the idiomatic way to solve your problem would be to add a nil check to the previous if if event.phase == "began" and event.other then, since both if and else conditions depend on event.other to be set.
function onCollision(event)
if event.phase == "began" and event.other then
if event.other.star == "star" then
score = score + 1
elseif event.other.mine1 == "mine1" then
if jet.collided == false then
timer.cancel(tmr)
jet.collided = true
jet.bodyType = "static"
explode()
end
end
end
end
In case you're wondering about the message of 'attempt to index field' you can also read more about lua index metamethod here

Related

Corona SDK - How to remove an item(Coins) when collide with an object(player)?

I started learning and developing games using Corona SDK recently and right now I am facing an issue to collect coins in the gameplay. When the player object collides with the coin object the coin should be removed/disappeared. I tried the below code which is not successful whenever the coin collides with the player it throws an error
Attempt to call method 'translate' (a nil value)
Below is the code I used,
------Create Coins------
function coin()
token = display.newImage(sceneContainer, "gold.png")
token.x = math.random(320, 720)
token.y = math.random(160, 260)
token.myName = "token"
physics.addBody( token, "dynamic", { bounce=0, friction=1, radius=20 })
local function muovi()
token:translate(-2, 0)
end
Runtime:addEventListener( "enterFrame", muovi )
end
tmr = timer.performWithDelay(5000, coin, 0)
------Collision Function------
function onCollision( event )
if ( event.phase == "began" ) then
if event.object1.myName == "player" and event.object2.myName == "token" then
event.object2:removeSelf()
print("hitting 1")
elseif event.object1.myName == "token" and event.object2.myName == "player" then
event.object1:removeSelf()
print("hitting 1")
end
end
end
Runtime:addEventListener( "collision", onCollision)
Look at the gotchas in the Corona Documentation.
As a best practice, you should set the associated variable(s) to nil
after calling object:removeSelf().
When an object is removed, the rendering-related resources of the
removed object are deleted immediately. What remains of the object is
simply a plain Lua table with all non-display object properties — the
metatable is set to nil and all properties relating to display object
are removed. Thus, if there are still references to object in Lua,
they will simply be references to a normal Lua table.
Try setting the object to nil after calling removeSelf()
if ( event.phase == "began" ) then
if event.object1.myName == "player" and event.object2.myName == "token" then
event.object2:removeSelf()
event.object2 = nil
print("hitting 1")
elseif event.object1.myName == "token" and event.object2.myName == "player" then
event.object1:removeSelf()
event.object1 = nil
print("hitting 1")
end
end
end
If you read the second point you'll notice that you'll still have a plain Lua table with all non-display object properties, so my guess is that it is calling removeSelf twice. The first time it is removing the object from display, but it's not removing the myName field. Therefore when it's called a second time, it's trying to removeSelf again.

Return an event.object to another function during collision - Corona SDK

In my collision, I am detecting which spawns collide with my castle object at the bottom of the screen using event.object1.name and event.object2.name. this works fine but I would like to return that specific spawn (that collided with that castle) and use its event.object within another function if possible. How can I do this?
Cheers
My collision is as follows:
local function onCollision(event)
if event.phase == "began" and gameIsActive == true then
if event.object1 ~= nil and event.object1.name == "Blue" and event.object2 ~= nil and event.object2.name == "arrow" or event.object1 ~= nil and event.object1.name == "arrow" and event.object2 ~= nil and event.object2.name == "Blue" then
if event.object2.name == "arrow" then display.remove(event.object2) event.object2 = nil; onHit()
elseif event.object1.name == "arrow" then display.remove(event.object1) event.object1 = nil; onHit()
end
end
you can simply use the object befor you delete it.
display.remove(event.object1) removes object1 from the display.
event.object.1 = nil deletes it
function doSomethingWithTheObject(obj)
-- do something fancy here
print(obj.name)
end
Then befor you delete the object call your function:
doSomethingWithTheObject(event.object1)

why eveyone do if event.phase == "began" then...?

i see allways that people write in the collusion function (example):
local onCollision = function(event)
if event.phase == "began" then
event.object2:removeSelf();
event.object2 = nil;
end
end
Runtime:addEventListener("collision",onCollision);
why you dont just write:
local onCollision = function(event)
event.object2:removeSelf();
event.object2 = nil;
end
Runtime:addEventListener("collision",onCollision);
I dont understand what is the point?
consider this example,
local object = display.newImage( "ball.png" )
function object:touch( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( self )
self.isFocus = true
elseif event.phase == "moved" then
print( "moved phase" )
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus( nil )
self.isFocus = false
end
end
return true
end
object:addEventListener( "touch", object )
If you doesn't add the phase your touch will be detected in all the three phase,
thus it executes all the statements with in function three times.
To avoid this we are using the phase.
In your case ,
local onCollision = function(event)
event.object2:removeSelf();
event.object2 = nil;
end
Runtime:addEventListener("collision",onCollision);
The code inside this will be called three times, this results in error. Since in began phase itself your object will be removed, when it comes to moved phase it will give you error, since object is already removed.
Please refer this, http://docs.coronalabs.com/api/event/touch/phase.html

Attempt to call global 'gameover' (a table value)

So i'm working on a little game using onCollision event's :
local function onCollision(event)
if event.phase == "began" and gameIsActive == true then
local obj1 = event.object1;
local obj2 = event.object2;
if obj1.name == "bill" then
if obj2.name == "rocks" then gameover()
elseif obj2.name == "" then
end
end
end
end
Runtime:addEventListener( "collision", onCollision )
But i have a bit of a problem the code works as expected the first time it runs but is you restart the game i'm getting this error :
File: game.lua
Line: 649
Attempt to call global 'gameover' (a table value)
stack traceback: game.lua:649: in function <game.lua:643> ?: in function <?:221>
649 = if obj2.name == "rocks" then gameover()
643 = local function onCollision(event)
Any idea what this could be?
gameover function :
function gameover()
rightTAP:removeEventListener("tap", movePLAYERr)
leftTAP:removeEventListener("tap", movePLAYERl)
timer.pause(spawnBits)
timer.pause(Rockspawner1)
timer.pause(tmrscore)
timer.pause(updateScoretimer)
timer.pause(spawnDinosControll)
audio.pause( drillingChannel )
drillbg.isVisible = false
scoreText.isVisible = false
Restartg = display.newText( "Restart", 0, 0,nil, 20)
Restartg:setFillColor(0, 0, 0)
screenGroup:insert(Restartg)
Restartg.x= display.contentWidth/2
Restartg.y= display.contentHeight/2 + 160
Restartg:addEventListener("tap", RestartGame1)
end
The error message is clear: you are trying to call gameover and it has a table value instead of a function value. You get the same error is you run the following: gameover = {}; gameover().
This means that either you are not correctly defining gameover function or overwriting gameover value somewhere in your script.
if you have an object of functions you can call whit the key that you use to declare.
example
el = {}
print(el)
table: 0x7f8fe9e003f0
el.f1 = function()
print('hola')
end
el.f1()
hola
On restart you have to remove all Runtime listeners. Otherwise they will be still listening and it may cause errors, because they lead to already removed methods.
Place it in RestartGame1:
Runtime:removeEventListener( "collision", onCollision )
fixed by adding "transition.pause( thisRock )" to the gameover function

Moving a character with Lua

I am new to Lua and am attempting to simulate a character moving.
I have the character moving left and right at the moment. I would like the character to move 16 pixels at a time. That works fine given that the user doesn't touch the phone rapidly. In that case, the character moves a random number of pixels.
my question is, how can i get the touch event to only register once at a time.
my code:
-- move character
function moveCharacter(event)
if event.phase == 'began' then
if event.x > character.x+8 then
transition.to(background, {time=800, x=background.x-16})
end
if event.x < character.x-8 then
transition.to(background, {time=800, x=background.x+16})
end
end
end
function touchScreen(event)
Runtime:removeEventListener('touch', moveCharacter)
if event.phase == 'began' then
Runtime:addEventListener('touch', moveCharacter)
end
end
Runtime:addEventListener('touch', touchScreen)
You can try this:
function moveCharEF()
if event.x > character.x+8 then
background.x = background - 16
end
if event.x < character.x-8 then
background.x = background + 16
end
end
function moveCharacter(event)
if event.phase == 'began' then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", moveCharEF )
elseif event.target.isFocus then
if event.phase == "ended" then
Runtime:removeEventListener( "enterFrame", moveCharEF )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
end
function touchScreen(event)
Runtime:removeEventListener('touch', moveCharacter)
if event.phase == 'began' then
Runtime:addEventListener('touch', moveCharacter)
end
end
Runtime:addEventListener('touch', touchScreen)
By the way, I dont know about your application. So character may move too fast or too slow. Just change moveCharEF() function's related lines
Is it is what you looking for..?
local isTransitionInProgress = false
local function resetFlag()
isTransitionInProgress = false
end
function moveCharacter(event)
if(event.x > character.x+8 and isTransitionInProgress==false) then
isTransitionInProgress = true
transition.to(background, {time=800, x=background.x-16,onComplete=resetFlag()})
end
if(event.x < character.x-8 and isTransitionInProgress==false) then
isTransitionInProgress = true
transition.to(background, {time=800, x=background.x+16,onComplete=resetFlag()})
end
end
background:addEventListener("tap", moveCharacter)
Keep coding... :)
You can try using the "tap" listener instead of "touch". It only registers one touch at a time.
Runtime:addEventListener('tap', touchScreen)

Resources