sprite kit碰撞:忽略透明度?

时间:2014-03-31 19:45:08

标签: ios objective-c xcode sprite-kit

我正在使用Xcode的spritekit构建游戏。它是一款平台游戏,现在它可以很好地适应平坦的地面部件。我想知道是否有可能忽略png中的碰撞透明度。也就是说,如果我有一个弯曲的地板和填充槽的透明度,我可以让玩家走弯道而不是覆盖整个事物的方形边框吗?我能找到的唯一例子是Gamemaker GML语言,你可以进行“精确”碰撞,这样图像中的空白就不算作精灵的一部分了。如有必要,我可以提供代码,但这似乎更像是一个概念性问题。提前致谢

3 个答案:

答案 0 :(得分:3)

嘿,Apple文档中提供了一个简单的解决方案。

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite.physicsBody = [SKPhysicsBody bodyWithTexture:sprite.texture size:sprite.texture.size];

这会在纹理的物理路径周围创建一个物理主体。

Simulating Physics - SpriteKit Programming Guide

答案 1 :(得分:1)

所有这些都是你如何实例化相关节点的physicsBody。

node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node.size];

可能是制作物理实体的最简单和最常用的方法,但也会产生您在上面确定的问题,因为对于所有碰撞目的,节点都是矩形。

查看the documentation for SKPhysicsBody以查看可供您使用的其他选项。 PolygonFromPath和BodyWithBodies可能最适合您正在做的事情。

答案 2 :(得分:0)

SKPhysicsBody是正确的举动,只是想要注意,为SKPhysicsBody提供一个单独的(简化的)蒙版图像以改善你的表现,从颜色和几何学的角度简化,这是有意义的,这里是代码:

let birdMask: UInt32 = 0x1 << 0
let pipeMask: UInt32 = 0x1 << 1
//...

pipeImage = SKSpriteNode(imageNamed: "realImage")
//... size and position

let maskTexture = SKSpriteNode(imageNamed: mask)
maskTexture.size = pipeImage!.size // size of texture w/ real imageNamed

pipeImage!.physicsBody?.usesPreciseCollisionDetection = true
pipeImage!.physicsBody = SKPhysicsBody(texture: maskTexture.texture!, size: size)        
pipeImage!.physicsBody?.affectedByGravity = false // disable falling down...
pipeImage!.physicsBody?.allowsRotation = false
pipeImage!.physicsBody?.isDynamic = true
pipeImage!.physicsBody?.friction = 0
pipeImage!.physicsBody?.categoryBitMask = pipeMask
pipeImage!.physicsBody?.collisionBitMask = birdMask | pipeMask
pipeImage!.physicsBody?.contactTestBitMask = birdMask | pipeMask

more detailed example/guide