当前位置: 动力学知识库 > 问答 > 编程问答 >

swift - How to fix some physics bodies behavior, when some bodies go off the physics edges?

问题描述:

I am working with a game, which has a physics world. I create physics box around the whole scene for prevent the sprites go off the screen. But the issue is, when i add too many bodies to the scene, some of them go off this physics box.

This is the physics box around the scene:

 homeArea.size = CGSizeMake(self.frame.size.width, self.frame.size.height)

homeArea.position = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0)

homeLayer.addChild(homeArea)

homeArea.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(-homeArea.frame.width / 2.0, -homeArea.frame.height / 2.0, homeArea.frame.size.width, homeArea.frame.size.height))

This is these too many bodies:

func addSprites() {

for i in 0...20 {

for j in 0...20 {

let shape = SKShapeNode(rect: CGRectMake(-5.0, -5.0, 10.0, 10.0))

shape.fillColor = SKColor.redColor()

shape.position = CGPointMake(self.frame.size.width - (shape.frame.size.width * CGFloat(i)), 0.0 + (shape.frame.size.height * CGFloat(j)))

shape.physicsBody = SKPhysicsBody(rectangleOfSize: shape.frame.size)

shape.physicsBody?.dynamic = true

addChild(shape)

}

}

}

And i create a cannon shooting to these bodies. There i want to create something like splash from cannon's bullet. I shoot to this wall from sprites, and notice from nodes count, that because of my bullet, some sprites go off the screen. I added the method usesPreciseCollisionDetection. But it's all the same.

分享给朋友:
您可能感兴趣的文章:
随机阅读: