Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
OnBeginContact - Object activated (local variable) — Gideros Forum

OnBeginContact - Object activated (local variable)

peach303peach303 Member
edited January 2013 in General questions
Hi,

when a collision occures i want to check if one of the bodys is "activated". Activated is a local variable in the Object class:
 self:body.activated = true -- for example
Here is the OnBeginContact function:
function onBeginContact(event)
	local fixtureA = event.fixtureA
	local fixtureB = event.fixtureB
	local bodyA = fixtureA:getBody()
	local bodyB = fixtureB:getBody()
 
	print("begin contact: " .. bodyA.type .. " & " .. bodyB.type)
 
	if bodyA.type and bodyA.type == "Trigger" and bodyB.aktiviert == true then 
		bodyA.aktiviert = true 
	end
	if bodyB.type and bodyB.type == "Trigger" and bodyA.aktiviert == true then 
		bodyB.aktiviert = true 
	end
 
end
bodyA.aktiviert (or BodyB.aktiviert) is nil because its a local in the object class. But how to solve this??

An object shall only be activated if the touching object is already activated...


daniel303

Comments

  • Well, you can either move that variable to the body OR add a reference to the containing object in the body.
    -- pseudo code
    SomeClass = Core.class(Sprite)
     
    function SomeClass:init(world)
              local body = world:createBody()
              body.parent = self
    end
  • Thx. Hm, i dont get it. In your code body.parent is a local variable of SomeClass right? So i cant use it outside of SomeClass. The same problem like with body.aktiviert.

  • @peach303 can you show your Object's class?

    Dislikes: BigWinston

    +1 -1 (+0 / -1 )Share on Facebook
  • Sure!

    This is the init of the PuckClass in puck.lua:
    function Puck:init(xp, yp, world)
    	self.puckArrow = Bitmap.new(Texture.new("puckArrow.png"))
    	self.puckHalo = Bitmap.new(Texture.new("puckHalo.png"))
    	self.puckBody = Bitmap.new(Texture.new("puck.png"))
    	self.puckLight = Bitmap.new(Texture.new("puckLight.png"))
     
    	self.dir = 0
    	self.pow = 0
    	self.illumination = 0
     
    	self.puckArrow:setAnchorPoint(0.5, 1)
    	self.puckArrow:setVisible(false)
     
    	self.puckHalo:setAlpha(0.3)
     
    	self.puckLight:setAlpha(0)
     
    	self:addChild(self.puckArrow)
    	self:addChild(self.puckBody)
    	self:addChild(self.puckHalo)
    	self:addChild(self.puckLight)
     
    	self.puckBody:setPosition(-50,-50)
    	self.puckHalo:setPosition(-100,-100)
    	self.puckLight:setPosition(-50,-50)
     
    	self:setPosition(xp,yp)
     
    	local body = world:createBody{type = b2.DYNAMIC_BODY}
        body:setPosition(xp, yp)
        body:setAngle(self:getRotation() * math.pi/180)
        local circle = b2.CircleShape.new(0, 0, 50)
        local fixture = body:createFixture{shape = circle, density = 1.0, friction = 0, restitution = 1}
        self.body = body
        self.body.type = "Puck"
    	self.parent = self
     
    	self.body:setLinearDamping(damping)
     
    	self.body.aktivert = true
     
    	self:addEventListener(Event.MOUSE_DOWN, self.onTouchesBegin, self)
    	self:addEventListener(Event.MOUSE_MOVE, self.onTouchesMove, self)
    	self:addEventListener(Event.MOUSE_UP, self.onTouchesEnd, self)
    	--self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)
     
    	self:addEventListener(Event.ENTER_FRAME, updateLight, self)
    end
    And the onBeginContact function (see above) is in scene3.lua
  • My apologies, I think I read your first post too fast and saw you needed to access something from your class, thus my first example.

    However, looking at it again it looks like your problem is trying to compare aktiviert to true since it might be nil maybe? The class you showed, shows aktiviert always being initialized and the type is always "Puck" for the body. Does its type get changed to "Trigger" at some point or is there a different class/body?

    If aktiviert being nil is what throws the error then I suggest changing any check on aktiviert, to the following:
    -- by removing the == true on the aktiviert check, nil will count as false and not throw an error
     
    if bodyB.type and bodyB.type == "Trigger" and bodyA.aktiviert then 
       bodyB.aktiviert = true 
    end
    If this isn't what's going on, then we may need to see more as I said above, we can't see where a body becomes a "Trigger" etc so I suspect there's another class.
  • Ah, i'm sorry. I should explain a little more.. There are 2 objects. "Puck" is allways aktiviert = true. And "Trigger" is the secound object (initialised as aktiviert = false. When hit by Puck or another activated (aktiviert) object, it should get activated too.

    Yes, the problem is that aktiviert is nil. But it should be true in case Puck is the collision partner.

    Should i post more of my code?
  • Are there walls it can hit? I still suspect the error happens when you hit something else or there's a small mistake initializing the aktiviert property. If it's nil then it still seems like it shouldn't be either of those classes causing the problem if you initialize the value.

    If you change the code similar to like what I said in my last post your code should function even if the value IS nil however because nil will be treated as a false if you take away the "== true".
  • atilimatilim Maintainer
    @peach303 your code seems ok. Maybe printing the values as:
    print(bodyA.type, bodyA.aktiviert, bodyB.type, bodyB.aktiviert)
    can help understanding the problem?
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @peach303 the problem is that in your class you have aktivert while on contact you are checking for aktiviert (mistyped?)

    But all in all you should be able to access it like that, there should be no problem ;)
  • If you change the code similar to like what I said in my last post your code should function even if the value IS nil however because nil will be treated as a false if you take away the "== true".

    @ zvardin: thx a lot for this advise! I forgot the walls they are there too.

    @atilim: THX! This brought the answer! It was the small mistake initializing the aktiviert property.. i wrote "aktivert" 8-|

    I still dont understand why i can work with aktiviert here. Isn't it a local in the object class? But it is working. Thx a lot!!

  • thx ar2rsawseen!
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @peach303
    local var = "somevalue" -- this is local
     
    function MyClass:init()
        self.somevalue -- this is a property and it is public (thus can be accessed anywhere where instance is)
    end
  • zvardinzvardin Member
    Accepted Answer
    Anything defined locally in ar2sawseen's example essentially works as a static/constant variable. Anything defined in the case of self.somevalue is a property given to each instance of the class.
  • OZAppsOZApps Guru
    Accepted Answer
    interesting fun when variables get localised ;)

    it is easier to use words that can be spelled, so aktiv or active would have been easier/simpler.

    just my $0.02 on naming variables
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Thx a lot for the tipps. I have to get used to this local public stuff.

    @OZApps: You're right. "aktiviert" is just the german word for "is active" :\">

    I'm really happy to work with gideros now. I used corona before. No forum access and the APK was very slow on device. I've read a lot about this problem with corona. But maybe it was my fault. Anyway gideros is better.
Sign In or Register to comment.