OMG HELP collision detection

I add some code so the when player hits one of the obstacles(circles), it disappear and spawn again at the start position.

[code]local function onCollision(event)
if(event.object1.myName==“player” or event.object2.myName ==“circle1”) or
(event.object1.myName==“player” or event.object2.myName ==“circle2”) or
(event.object1.myName==“player” or event.object2.myName ==“circle3”) or
(event.object1.myName==“player” or event.object2.myName ==“circle4”) then

event.object1:removeSelf()

end
end

function weDied()
player.x = 70
player.y = 150
end

Runtime:addEventListener(“collision”, onCollision)
[/code]

When the player hits one of the obstacle, it disappear.
but it won’t spawn at the points I set.
These are the error msg said in terminal when collision occurs.

Runtime error
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:21: attempt to call method ‘setLinearVelocity’ (a nil value)
stack traceback:
[C]: in function ‘setLinearVelocity’
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:21: in function ‘move’
…iW11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/game.lua:154: in function <…iw11yah95oxk>
?: in function <?:215>
Runtime error
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:21: attempt to call method ‘setLinearVelocity’ (a nil value)
stack traceback:
[C]: in function ‘setLinearVelocity’
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:21: in function ‘move’
…iW11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/game.lua:154: in function <…iw11yah95oxk>
?: in function <?:215>
Runtime error
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:21: attempt to call method ‘setLinearVelocity’ (a nil value)
stack traceback:
[C]: in function ‘setLinearVelocity’
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:21: in function ‘move’
…iW11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/game.lua:154: in function <…iw11yah95oxk>
?: in function <?:215>
Runtime error
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:16: attempt to call method ‘setLinearVelocity’ (a nil value)
stack traceback:
[C]: in function ‘setLinearVelocity’
…11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/player.lua:16: in function ‘move’
…iW11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/game.lua:132: in function <…iw11yah95oxk>
?: in function <?:215>

[import]uid: 131469 topic_id: 23221 reply_id: 323221[/import] </…iw11yah95oxk></…iw11yah95oxk></…iw11yah95oxk></…iw11yah95oxk>

I see you have a player object and Circle objects. I take it when a circle hits your player you want to remove the player. This is how i would set up the function.

  1. use the event phase or you will get unwanted repeated tics.
  2. I used string.find this will search for the work circle if found it returns true.
  3. you never know what object1 or object2 is. object2 could be the player. You can see how i handle it.

[lua]local function weDied()
playerObj.hasDied=nil
playerObj.isSensor = false
playerObj.alpha = 1
end

local function onCollision(event)
if ( event.phase == “began” ) then
local playerObj, circleObj
if (event.object1.myName==“player” or string.find(tostring(event.object2.myName),“circle”) or
(event.object2.myName==“player” or string.find(tostring(event.object1.myName),“circle”)) then
if event.object1.myName==“player” then
playerObj = event.object1
circleObj = event.object2
else
playerObj = event.object2
circleObj = event.object1
end

if not playerObj.hasDied then
playerObj.hasDied = true
playerObj.alpha = 0
playerObj.x = 70; playerObj.y = 150;
playerObj.isSensor = true
weDied()
end

end
end
end[/lua]

Your ERROR:player.lua:21: attempt to call method ‘setLinearVelocity’ (a nil value)

You are trying to setLinearVelocity to an object that do not exist. From the code you deleted the player and can no longer move or change the x,y values of the player. As in my code i would set the player alpha to 0 with Boolean death check. And alpha back to 1 when you want to show the player again.

You can use .isVisible vs .alpha as well.

[import]uid: 7177 topic_id: 23221 reply_id: 92913[/import]

Thank you so much for reply.
but for me as a newbie, your code is hard to understand.
It will be great if you can highlight the part that I need to change.

I have variables as listed:

player
circle1
circle2
circle3
and circle4

[import]uid: 131469 topic_id: 23221 reply_id: 92916[/import]

I’m using very basic lua. You really need to learn and understand the basics lua. If you just start copying and pasting code without really understanding it your going to run into trouble very quickly. I would check out http://lua.gts-stolberg.de/en/Variablen.php

If your really new to scripting/programing learn all you can about variables and functions. There are 100s of learning lua sites out there and books. [import]uid: 7177 topic_id: 23221 reply_id: 92940[/import]

I’ve added this code:

  
local circle=display.newGroup ( )  
circle:insert( circle1 )  
circle:insert( circle2 )  
circle:insert( circle3 )  
circle:insert( circle4 )  
  
local function weDied()  
 playerObj.hasDied=nil  
 playerObj.isSensor = false  
 playerObj.alpha = 1  
end  
   
local function onCollision(event)  
 if ( event.phase == "began" ) then   
 local playerObj, circleObj  
 if (event.object1.myName=="player" or string.find(tostring(event.object2.myName),"circle") or   
 (event.object2.myName=="player" or string.find(tostring(event.object1.myName),"circle"))) then   
 if event.object1.myName=="player" then   
 playerObj = event.object1  
 circleObj = event.object2  
 else  
 playerObj = event.object2  
 circleObj = event.object1   
 end  
  
 if not playerObj.hasDied then   
 playerObj.hasDied = true   
 playerObj.alpha = 0  
 playerObj.x = 90; playerObj.y = 150;  
 playerObj.isSensor = true  
 weDied()   
 end  
  
 end  
 end  
end  
  
  

however it’s not working as I expected.

The error message:

ERROR: physics.start() has not been called.
ERROR: physics.start() has not been called.

Director ERROR: Failed to execute new( params ) function on ‘game’.

…iW11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/game.lua:128: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

line 2 is line 128 in the actual code.
Please help. [import]uid: 131469 topic_id: 23221 reply_id: 94007[/import]