Physics, event.phase == nil

First of all: Hi Corona Community :wink:

For a class I have to build a game which was going pretty well until I made a collision function which doesn’t seem to receive the “event.phase” parameter.

I have a touch function which does register the “event.phase” so I’m quite baffled that it didn’t work and couldn’t find much on stackoverflow or here…

In the zip below you can find the entire project but the problem resides in gamemanager.lua with the function starting at line 93.

function gamemanager.newplayer(X,Y,Width,Height,Bounce,Friction, fillcolor, strokecolor,strokewidth,Density) gamemanager["player"] = display.newRect(X,Y,Width,Height); gamemanager["player"]:setReferencePoint(display.CenterReferencePoint) gamemanager["player"]:setFillColor(fillcolor[1],fillcolor[2],fillcolor[3],fillcolor[4]); gamemanager["player"]:setStrokeColor(strokecolor[1],strokecolor[2],strokecolor[3],strokecolor[4]); gamemanager["player"].type = "player"; gamemanager["player"].state = "active"; gamemanager["player"].mass = Density \* (Width+strokewidth) \* (Height+strokewidth); gamemanager["player"].strokeWidth = strokewidth; gamemanager["player"].density = Density; physics.addBody(gamemanager["player"], "dynamic", {density = Density, friction = Friction, bounce = Bounce, radius = Radius, isBullet = true}); gamemanager["player"].isBullet = true; --gamemanager["player"].angularDamping = 100000; --gamemanager["player"].linearDamping = 0.3; gamemanager["player"].isFixedRotation = true; gamemanager["player"].isSleepingAllowed = false; gamemanager["player"]:addEventListener("touch", gamemanager.ontouch); gamemanager["player"].collision = gamemanager.playerCollide; gamemanager["player"]:addEventListener("collision", gamemanager["player"]); end function gamemanager.playerCollide(event) print("player collide"); --this is printed print(event.phase) -- but this results in 'nil' local target = gamemanager["player"]; local breakArea = gamemanager["breakField"]; local currForce = gamemanager.ForcePerFrame; print2DVector(currForce,"ori"); if (event.phase == "began") then currForce = reverse2DVector(currForce); print2DVector(currForce,"rev") currForce = div2DVector(currForce,10); print2DVector(currForce,"div") gamemanager.ForcePerFrame = currForce; print("break began!!!"); elseif (event.phase == "ended") then print("break!!! ended"); end end function gamemanager.newBreakField() gamemanager["breakField"].type = "breakField"; gamemanager["breakField"].state = "active"; gamemanager["breakField"].radius = 50; gamemanager["breakField"].x = -100; gamemanager["breakField"].y = -100; gamemanager["breakField"].breakFactor = 2; gamemanager["breakField"] = display.newCircle(gamemanager["breakField"].x,gamemanager["breakField"].y,gamemanager["breakField"].radius); gamemanager["breakField"]:setFillColor(122,122,122,255); gamemanager["breakField"]:setStrokeColor(255,255,255,255); gamemanager["breakField"].strokeWidth = 5; physics.addBody(gamemanager["breakField"], "dynamic", {radius = gamemanager["breakField"].radius, isSensor = true}); --gamemanager["breakField"].collision = gamemanager.breakPlayer; --gamemanager["breakField"]:addEventListener("collision", gamemanager["breakField"]); end

Hopefully someone sees a newbie mistake and it’s easily fixed.

Cheers,

Johnny

source

It has to do with the way you are creating the function.  You should be using ‘:’ instead of the dot notation:

function gamemanager:playerCollide(event)

That worked perfect!

Thank you Jon, hopefully I can show the community what resulted from my code and play with it :slight_smile:

It has to do with the way you are creating the function.  You should be using ‘:’ instead of the dot notation:

function gamemanager:playerCollide(event)

That worked perfect!

Thank you Jon, hopefully I can show the community what resulted from my code and play with it :slight_smile: