Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Getting Started - Global Index question — Gideros Forum

Getting Started - Global Index question

rdominellirdominelli Member
edited January 2013 in General questions
I apologize for the newbie nature of this question, I am coming to LUA and Gideros from objective c and java and I expect that's causing me more grief then helping.

I am trying to adopt the basic box2d tutorial (http://appcodingeasy.com/Gideros-Mobile/Gideros-Box2D-basics) from appcodingeasy to do the following
1. Have 2 balls falling
2. When the balls collide, weld them together and stop movement.

This tutorial was a great starting point but now that I am trying to expand it I have hit a problem

Looking at the code, it seems that the main.lua is an derived class from sprite.
scene = gideros.class(Sprite)
What seems to be tripping me up is the onBeginContact event does not seem to be firing a class method
	self.world:addEventListener(Event.BEGIN_CONTACT, onBeginContact)
So when I attempt to do
	 	local jd = b2.createWeldJointDef(bodyA, bodyB, bodyA:getPosition(), bodyB:getPosition())
		weldJoint = world.createJoint(jd)
I receive "attempt to index global 'world' (a nil value)" this is also true if I try to specify self.world.

So my question is how do I access the world object within onBeginContact if its a member variable of scene.

Thanks for your help.

Rich in NY.
Tagged:

Comments

  • You can pass additional information to the event listener by adding a 3rd parameter.

    so you can do:
    local function onBeginContact(world, event)
      local jd = b2.createWeldJointDef(bodyA, bodyB, bodyA:getPosition(), bodyB:getPosition())
      weldJoint = world:createJoint(jd)
    end
     
    self.world:addEventListener(Event.BEGIN_CONTACT, onBeginContact, self.world)
  • ar2rsawseenar2rsawseen Maintainer
    edited January 2013
    @rdominelli in your case I'd define onBeginContact method as class method
    function scene:onBeginContact(e)
     ....
    end
    and then add event as
    self.world:addEventListener(Event.BEGIN_CONTACT, self.onBeginContact, self)
    thus you will have all properties and method available from same scene class instance including self.world
  • Thank you this resolved my issues, can you pass multiple params to events. For instance if I want to use a timer event, can I send multiple params to the onTimer function?
  • ScouserScouser Guru
    edited January 2013
    @rdominelli: The easy way to send multiple parameters would be to add your extra parameters to the event table.
    local event = Event.new("mytimerevent")
    event.data1 = "12345"
    event.data2 = "abcde"
     
    mydispatcher:dispatchEvent(event)
    See here for more.
  • Thank you for this
Sign In or Register to comment.