SKPhysicsContact Not Detecting categoryBitMask Collision

杀马特。学长 韩版系。学妹 提交于 2019-12-06 05:45:43

Your leftWall, rightWall and bottomWall have the default contactTest and collision bit masks, so will collide with everything and generate contacts with nothing. Except they all have their isDynamic property set to false, so they can’t collide with or contact anything (although other things can collide and contact them if those other things have isDynamic set to true).

But this is probably what you want (Floors and walls are usually not dynamic). I suspect the real problem is that objects of Floor class won’t have a physics body, unless you initialise it somewhere else. You have these lines:

self.physicsBody?.categoryBitMask = ... self.physicsBody?.contactTestBitMask = ... self.physicsBody?.collisionBitMask = ...

but you haven't actually created self.physicsBody. Your code would crash except you’ve used optional chaining (self,phyicsBody?) and so Swift gets to the ‘?’ and says “Oh - it’s optional and doesn’t exist. I’ll just stop here then”

At the very least, try creating a physics body for your Floor object using the physics bodies of the 3 wall objects:

Self.physicsBody = SKPhysicsBody(Bodies: [leftWall.physicsBody, rightWall.PhysicsBoidy, bottomWall.physicsBody])

(SKPhysicsBody has an initialiser that takes an array of physics bodies to create a new physics body).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!