Runtime error.. have no idea why.?

@cyberparkstudios,

  1. The nomenclature is clearer.
  2. You can trade out objects as the ‘receiver’ i.e. You can have a proxy object as ‘self’ and the collided object as “event.target”.  This allows for unified  processing of collisions when you need it.
  3. self passed as an argument is slightly faster than “local target = event.target” creation on each call.  (Not really a motivator, but still better.)
  4. You can dynamically re-assign the listener w/o needing to remove and add, simply change the function ‘.collision’ is pointing to and the new function is called.  This can be done any time, even mid-touch.  I use this for easy creation of editors where touch, drag, drop functionality, and/or context changes.  It keeps the individual touch listeners simple and short.
  5. More…

The point is, function listeners are simple, but not really flexible.

However, we should each code the way we are comfortable.

-Ed

Oh, and because SSK uses these extensively.  For example: 

local function onCollision( self, event ) ... code here end ssk.display.newImageRect( group, centerX, centerY, "images/smiley.png", -- add to group, sel image, place { size = 20, fill = \_RED\_, rotation = 25 }, -- Visual attributes { radius = 10, bounce = 1, collision = onCollision } ) -- Physics attributes

i did this and it worked and the error doesnt  come up any more but the onCollision function doesn’t heres some more code…

local box = display.newImageRect("box.png", 30, 30 ) box.x = display.contentWidth \* 0.5 box.y = 420 physics.addBody( box, "static", {bounce = 0 } ) box.isVisible = false box.value = 1 local onCollision = function(event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then print("hey") --display.remove( circle[hit] ) --circle[hit] = nil end end end box:addEventListener("collision", onCollision) local spawnCircles = function () local FallDown = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8 ) circle[hit] = display.newImageRect("circle.png", 31, 31 ) circle[hit].x = FallDown circle[hit].y = -70 physics.addBody( circle[hit], "dynamic", {density = .01, friction = 0.5, bounce = 0.1 }) circle[hit].gravityScale = 0.1 physics.setGravity( 0, 10 ) circle[hit].collision = onCollision circle[hit]:addEventListener( "collision", circle[hit] ) circle[hit].value = hit hit = hit + 1 end box.collision = onCollision myTimer = timer.performWithDelay( 1000, spawnCircles, -1) --1000 is the time 

maybe i have values messed up? thanks again

@roaminggamer,

#2, #4 are excellent points.  I do remember those ‘advantages’ now, as you mention them, I just have got into the habit of using the function listener and have not used the table listener for so long, I forgot the advantages over the function listener.  Thanks for reminding me, I figured there was some stronger reason.

@sonic

I think you have now both the ways to setup collision events, setup, as roaming gamer speaks about in his first response.  The ‘table listener’ is setup after that the ‘function listener’.  Remark out the last one :

box.collision = onCollision

and it should work.

Roaming Gamer, much more experienced then I, makes some good points on using the ‘table listener’  instead of the ‘function listener’. …  if you use the ‘table listener’, then you need to put the ‘self’ argument back into the function definition

local function onCollision( self, event )

either way should work, but having both for the same event will be an issue.

I just use the ‘function listener’ as I do very little (presently) with physics and find it easier for ‘me’ to use the ‘function listeners’. They work well for what I am doing. 

I was in a hurry earlier, when I posted my first response, and felt you likely intended to use a ‘function listener’ and just made a typo when you had the ‘self’ argument in the event function, in your original post.  So I posted that short simple fix.

Anyway, read over what roaming gamer posted… I do not claim to have the knowledge and experience he has, so you can learn a lot more from his very informative posts.

I just try to help where I can, with what little I know.  I am trying to get 5 apps finished up and out to the app store this week, so I am super busy.  I just wanted to give you the quick fix, and not try to tackle the difference between the ‘function listener’ and ‘table listener’ … which I don’t have full grasp of anyway. Thanks again to roaming gamer for explaining that so well.

Good luck!

well i tried both and see when the circles spawn and start falling they just call the collision weather or not they collide with the object…

I added print(“hello”) adn its kept calling it like i said 

sonic,

Roaming Gamer is the guy to ask about that, as you just did.  I really never use physics collision, so I cannot speak to it.  I thought I would respond with a quick comment if it helps, as I figure roaming gamer is likely busy helping many others.

I did notice from you previous post example,  since you have (in that example)  used the ‘function listener’ , the :

local hit = self.value

is going to throw an error. I am guessing you are trying to pull that ‘value’ from box object, so try using event.target.value  … that is if you stay with the ‘function listener’.

I do think your example code as you have it listed 2 posts ago, should work, if you change that one line I mention.  Then according to your code you posted 2 posts ago,  if circle ‘1’ collides with box ‘hey’ should print, otherwise not.

anyway, I am wrapping up for the night.  I wish you luck.  I just did a quick look over the code, so I may still be missing something or mis read something, but hopefully not.

The circles are probably hitting each other since you didn’t specify filters when you created them.

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#filtering

Filtering basically allows you to make rules like:

  • These circles don’t hit each other.
  • These circles do hit the cube.

Its a rather advanced topic and I’m not really willing to cover it as it is well covered in the link above.

Also, I use my own library SSK to simplify this whole bit.  I’ll post a code and some link in a minute showing how I would merge your code and SSK to make collision filtering simple.

Please wait…

** Update Fixed Link **

  1. Download this file: http://github.com/roaminggamer/SSKCorona/blob/master/ssk/RGCC.lua

(or copy paste the contents into a file called RGCC.lua)
 
2. Try this code instead of yours (warning may contain typos; I didn’t run it):

-- Set up physics ONCE -- local physics = require "physics" physics.start() physics.setGravity( 0, 10 ) local myTimer -- Create a Collision Calculator to do the work for you -- local ccmgr = require "RGCC" local myCC = ccmgr:newCalculator() -- Configure collisions such that you have 'circle' and 'floor' collider names. -- -- 'circles' should only collide with floor. All other collisions are ignored. -- myCC:addNames( "circle", "floor" ) myCC:collidesWith( "circle", "floor" ) --Note: If you want circles to hit circles, change the line above to this: --myCC:collidesWith( "circle", "floor", "circle" ) -- Create a table to hold the spawned circles local circles = {} -- Create a box (the ground?) -- local box = display.newImageRect( "box.png", 30, 30 ) box.x = display.contentWidth \* 0.5 box.y = 420 -- Add a body to the box and use the "ground" filter generated by the calculator physics.addBody( box, "static", { bounce = 0, filter = myCC:getCollisionFilter( "ground" ) } ) -- Add a table 'collision' listener to the box. -- It removes circles when they collide with the box and takes them out of the circles table. -- function box.onCollision( self, event ) if( event.phase == "began" ) then circles[self] = nil display.remove(self) end return true end box:addEventListener( "collision" ) -- Modified circle spawner -- local function spawnCircles() -- Create a circle local circle = display.newImageRect("circle.png", 31, 31 ) -- Store a reference to it in the table. Use the circle as the table index circles[circle] = circle -- position the circle circle.x = math.random( display.contentWidth \* 0.2, display.contentWidth \* 0.8 ) circle.y = -70 -- Add a body to the circle and use the "circle" filter generated by the calculator physics.addBody( circle, "dynamic", { density = .01, friction = 0.5, bounce = 0.1, filter = myCC:getCollisionFilter( "circle" ) } ) -- Modify gravity scale so gravity affects this object 1/10th as much circle.gravityScale = 0.1 end -- Start Spawning circles (forever) myTimer = timer.performWithDelay( 1000, spawnCircles, -1)
  1. You don’t need a collision listener on the circles and on the box.  Just on the box will do. 
     
     
     
    Note:
     
    Please post cleaner code in the future, you’re posting code with extra lines that are not needed and your indentation sucks, making it hard to read the code and help you.  You need to read your post after you submit it, then re-edit it till it is neat and nice.  
     
    I should not have to work so hard all the time to answer your questions.  In fact, I am tiring of this quickly and will soon find myself unwilling to read and answer your posts.  
     
    Compare my code (cleaned, modified, and re-posted) with yours.  Make sure your future posts are as neat as mine.

The link doesnt work and yes ill try my best to clean my code for the future…

https links often get corrupted, and I frequently forget to check them (bad me).

 If you run into this in the future, you can copy and paste the text of the link.  For now let me repost it here:

http://github.com/roaminggamer/SSKCorona/blob/master/ssk/RGCC.lua 

@cyberparkstudios,

Good luck on those games/apps. 

Also, I really meant it.  Use what works best for you, but I will continue to try to convert folks to using ‘table listener’ :smiley:

You should know, Rob Miracle (if I recall correctly) prefers function listeners. 

@roaminggamer

thanks! I am always open to learning new, better or different ways to do things. 

I know there has been a lot of back and forth on this, but I would encourage you to take time to learn what the error message is actually telling you.  Here is the error:

Runtime error c:\users\r\documents\main game\faller\game.lua:50: attempt to index local 'event' (a nil value) stack traceback: c:\users\r\documents\main game\faller\game.lua:50: in function \<c:\u sers\r\documents\main game\faller\game.lua:48\> ?: in function \<?:221\> 

Lets break this down:

*c:\users\r\documents\main game\faller* :  This is just the path to your app.

game.lua:50 : Your error is happening at line 50 of game.lua.  Sometimes the real problem could be the line above, but in Lua it seems to be pretty good about getting that right.

attempt to index local ‘event’ (a nil value): You are trying to use a variable named “event”.  It is “nil”.

in function <c:\users\r\documents\main game\faller\game.lua:48 >: The function that has the error started at line 48.

So you should look at line 50 in your code.  This is why its important to use a text editor that shows line numbers!  See where you are using a variable named “event”.  Then figure out why it isn’t what you expect it to be.

Rob

well i was working out the problem and figured it ou ton my own… but did use the help and knowledge you guys provided me with so thanks to all!