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
-
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())
-
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. -
Also, it might be useful to point to a repo containing an example that works up to to where tou are having trouble.
-
Please post your entire code, if possible, e.g. as a Gist or GitHub repo.
-
@omz Here is full code:
https://gist.github.com/anonymous/2530353b2a41c19746bf9c0e0fa9a57fNote:
The code is basically modified version of this:
https://github.com/jbking/pythonista-misc/blob/master/spritekit/skview-demo.py -
@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
-
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.
-
@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.