Hi,
I make my “objects” like this in Lua:
local graphics = require ("graphics") local M = {} -- table needed to return the file ---------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------- M.playSpinAnimation = function() -- set and playback a spinning aniation for the coin end ---------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------- local function onCollision(self, event) if (event.other.myType == "Purple") then Runtime:dispatchEvent( {name="GotNetItem"} ) -- Disposing of this food as we don't need it anymore. removeThisFoodObjectFromTheWholeGameProperly(self) elseif (event.other.myType == "Yellow") then Runtime:dispatchEvent( {name="GotNetItem"} ) -- Disposing of this food as we don't need it anymore. removeThisFoodObjectFromTheWholeGameProperly(self) elseif (event.other.myType == "TopWall") then local vx local vy vx, vy = self:getLinearVelocity() if vy \< 0 then removeThisFoodObjectFromTheWholeGameProperly(self) end elseif (event.other.myType == "BottomWall") then removeThisFoodObjectFromTheWholeGameProperly(self) end end ---------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------- M.new = function() local newNetItem local netSheetData = { width = 125, height = 125, numFrames = 6, -- The params below are optional; used for dynamic resolution support sheetContentWidth = 750, -- width of original 1x size of entire sheet sheetContentHeight = 125 -- height of original 1x size of entire sheet } local coinAnimationSequenceData = { { name = "spin", frames = { 1, 2, 4, 5, 6 }, time = 500, loopCount = 0 }, } local netSheet = graphics.newImageSheet("item\_net.png", netSheetData) newNetItem = display.newSprite(netSheet, coinAnimationSequenceData) return newNetItem end return M
and when I make an instance of this, I use:
foodObject = itemNetClass.new() foodObject.myType = "ItemNet" foodObject.collision = onCoinItemCollision foodObject:addEventListener("collision", foodObject) physics.addBody( foodObject, "dynamic", coinPhysicSettings) currentSceneGroup:insert(foodObject) foodObject:setSequence( "spin" ) foodObject:play()
But in order to make my main chunk of file more clean, I wanted to move the collision detection into the table’s definition, so I want to move this line to last part of the table definition:
newNetItem.collision = onCollision
So it if it gets “collision” event, it has a handler, but it won’t work, logical right? But it won’t work and collision won’t get detected.
Only way I can get it to work is the way I wrote here; to define the .collision outside where I create it, like I posted above.
So what’s the difference between defining a collision member inside table definition or outside after it’s made?
Thanks.