omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    SpriteKit contactDelegate doesn’t work

    Pythonista
    delegate objcutil contacts python spritekit
    3
    8
    5462
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • smartvipere75
      smartvipere75 last edited by smartvipere75

      Hello, I’m trying to set contactDelegate on physicsWorld of SKScene that’s created through create_objc_class but it doesn’t work. Any ideas? Thanks in advance.

      Here is that part of code:

      scene_class = create_objc_class(name,SKScene,methods=[didMoveToView_,update_,touchesBegan_withEvent_],protocols=[])
      		
      		screen_size = ui.get_screen_size()
      		rect = CGRect(CGPoint(0, 0),CGSize(screen_size[0], screen_size[1]))
      		
      		self.scene = scene_class.sceneWithSize_(rect.size)
      		
      		delegate_class = create_objc_class('PhysicsDelegate',NSObject,methods=[didBeginContact_],protocols=['SKPhysicsContactDelegate'])
      		self.scene.physicsWorld().setContactDelegate_(delegate_class.alloc().init())
      
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        By doesn't work, is it crashing, or just does not receive callbacks?
        You might try keeping a copy (rather than alloc.init() inside the function call) -- I am not 100% sure if setting the delegate also increments the retain count, so you might be losing your object before it gets used.

        smartvipere75 1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          Also, it might be useful to point to a repo containing an example that works up to to where tou are having trouble.

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by

            Please post your entire code, if possible, e.g. as a Gist or GitHub repo.

            smartvipere75 1 Reply Last reply Reply Quote 0
            • smartvipere75
              smartvipere75 @omz last edited by

              @omz Here is full code:
              https://gist.github.com/anonymous/2530353b2a41c19746bf9c0e0fa9a57f

              Note:
              The code is basically modified version of this:
              https://github.com/jbking/pythonista-misc/blob/master/spritekit/skview-demo.py

              1 Reply Last reply Reply Quote 0
              • smartvipere75
                smartvipere75 @JonB last edited by smartvipere75

                @JonB said:

                By doesn't work, is it crashing, or just does not receive callbacks?
                You might try keeping a copy (rather than alloc.init() inside the function call) -- I am not 100% sure if setting the delegate also increments the retain count, so you might be losing your object before it gets used.

                I mean it doesn’t receive callbacks, as if I didn’t set delegate. And I posted a link to full code

                1 Reply Last reply Reply Quote 0
                • omz
                  omz last edited by omz

                  I can't test this right now, but I have a suspicion that you need to set the argtypes and restype properties of the ObjC method manually. The derivation from the protocols argument doesn't always work reliably if the protocol isn't used as part of Pythonista itself. I hope this makes sense.

                  smartvipere75 1 Reply Last reply Reply Quote 0
                  • smartvipere75
                    smartvipere75 @omz last edited by

                    @omz said:

                    I can't test this right now, but I have a suspicion that you need to set the argtypes and restype properties of the ObjC method manually. The derivation from the protocols argument doesn't always work reliably if the protocol isn't used as part of Pythonista itself. I hope this makes sense.

                    I completely forgot about argtypes and restype honestly. It makes sense, I will keep trying to find out correct argtypes. Thanks for your suggestion. Please let me know once you test.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors