Event.BEGIN CONTACT

From GiderosMobile
Revision as of 15:26, 13 July 2023 by Hgy29 (talk | contribs) (Text replacement - "<source" to "<syntaxhighlight")

Available since: Gideros 2011.6
Value: beginContact
Defined by: b2.World

Description

This event is dispatched when contact between box2d bodies begin.

Parameters

contact: (b2.Contact) contains instance of b2.Contact holding information about this collision
fixtureA: (b2.Fixture) contains instance of b2.Fixture of first colliding body
fixtureB: (b2.Fixture) contains instance of b2.Fixture of second colliding body

Example

<syntaxhighlight lang="lua"> LevelX = Core.class(Sprite)

function LevelX:init() -- liquidfun self.world = b2.World.new(0, 9.8 * 3, true) -- gravity x, gravity y, allow sleeping? -- listeners self:addEventListener("enterBegin", self.onTransitionInBegin, self) self:addEventListener("enterEnd", self.onTransitionInEnd, self) self:addEventListener("exitBegin", self.onTransitionOutBegin, self) self:addEventListener("exitEnd", self.onTransitionOutEnd, self) self.world:addEventListener(Event.BEGIN_CONTACT, self.onBeginContact, self) self.world:addEventListener(Event.END_CONTACT, self.onEndContact, self) self.world:addEventListener(Event.PRE_SOLVE, self.onPreSolveContact, self) self.world:addEventListener(Event.POST_SOLVE, self.onPostSolveContact, self) end

-- GAME LOOP function LevelX:onEnterFrame(e) self.world:step(1/60, 8, 3) end

-- collisions handler function LevelX:onBeginContact(e) local fixtureA, fixtureB = e.fixtureA, e.fixtureB local bodyA = e.fixtureA:getBody() local bodyB = e.fixtureB:getBody() -- PLAYER if (bodyA.name == G_PLAYER and bodyB.name == G_GROUND) or (bodyA.name == G_GROUND and bodyB.name == G_PLAYER) then if bodyA.name == G_PLAYER then bodyA.numfloorcontacts += 1 else bodyB.numfloorcontacts += 1 end end end

function LevelX:onEndContact(e) local bodyA = e.fixtureA:getBody() local bodyB = e.fixtureB:getBody() -- PLAYER if (bodyA.name == G_PLAYER and bodyB.name == G_GROUND) or (bodyA.name == G_GROUND and bodyB.name == G_PLAYER) then if bodyA.name == G_PLAYER then bodyA.numfloorcontacts -= 1 else bodyB.numfloorcontacts -= 1 end end end

function LevelX:onPreSolveContact(e) local bodyA = e.fixtureA:getBody() local bodyB = e.fixtureB:getBody() local platform, player if bodyA.name == G_PTPLATFORM then platform = bodyA player = bodyB else platform = bodyB player = bodyA end if not platform then return end -- do stuff... end

function LevelX:onPostSolveContact(e) end

-- EVENT LISTENERS function LevelX:onTransitionInBegin() self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end function LevelX:onTransitionInEnd() self:myKeysPressed() end function LevelX:onTransitionOutBegin() if self.channel then self.channel:stop() end self:removeAllListeners() end function LevelX:onTransitionOutEnd() end

-- KEYS HANDLER function LevelX:myKeysPressed() self:addEventListener(Event.KEY_UP, function(e) if e.keyCode == KeyCode.BACK or e.keyCode == KeyCode.ESC then self:goMenu() end end) end function LevelX:goMenu() scenemanager:changeScene("menu", 1) end </source>





LiquidFun