Difference between revisions of "Event.BEGIN CONTACT"

From GiderosMobile
(removed language stuff + added example)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Value:''' beginContact<br/>
'''<translate>Value</translate>:''' beginContact<br/>
+
'''Defined by:''' [[b2.World]]<br/>
'''<translate>Defined by</translate>:''' [[Special:MyLanguage/b2.World|b2.World]]<br/>
 
  
=== <translate>Descriptio</translate>n ===
+
=== Description ===
 
This event is dispatched when contact between box2d bodies begin.
 
This event is dispatched when contact between box2d bodies begin.
  
=== <translate>Parameters</translate> ===
+
=== Parameters ===
'''contact''': (b2.Contact) <translate>Contains instance of [[Special:MyLanguage/b2.Contact|b2.Contact]] holding information about this collision</translate><br/>
+
'''contact''': (b2.Contact) contains instance of [[b2.Contact]] holding information about this collision<br/>
'''fixtureA''': (b2.Fixture) <translate>Contains instance of [[Special:MyLanguage/b2.Fixture|b2.Fixture]] of first colliding body</translate><br/>
+
'''fixtureA''': (b2.Fixture) contains instance of [[b2.Fixture]] of first colliding body<br/>
'''fixtureB''': (b2.Fixture) <translate>Contains instance of [[Special:MyLanguage/b2.Fixture|b2.Fixture]] of second colliding body</translate><br/>
+
'''fixtureB''': (b2.Fixture) contains instance of [[b2.Fixture]] of second colliding body<br/>
 +
 
 +
=== Example ===
 +
<source 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>
 +
 
  
 
{{B2.World}}
 
{{B2.World}}

Revision as of 09:26, 11 April 2021

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

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





LiquidFun