我正在使用Spritekit在Swift中制作IOS游戏,玩家有一个三角形网格,我用这段代码创建:
let viewpath = CGPathCreateMutable();
CGPathMoveToPoint(viewpath, nil, 200, 0);
CGPathAddLineToPoint(viewpath, nil, 0, 400);
CGPathAddLineToPoint(viewpath, nil, 400, 400);
CGPathAddLineToPoint(viewpath, nil, 200, 0);
let playerviewshape = SKShapeNode(path: viewpath);
playerviewshape.fillColor = SKColor(red: 1, green: 1, blue: 1, alpha: 0.1)
playerviewshape.strokeColor = SKColor(red: 1, green: 1, blue: 1, alpha: 0.2)
playerview = SKSpriteNode(texture: view.textureFromNode(playerviewshape));
playerview.zPosition = 0;
playerview.position = CGPoint(x: 0, y: 200);
player.addChild(playerview);
我想知道三角形内是否有任何敌人,所以在更新循环中我使用if(enemy.intersectsNode(playerview)) {
但是这只检查它是否在节点的框架内,这是一个围绕三角形的矩形。我尝试使用三角形作为SKShapeNode,我也尝试使用containsPoint而不是intersectsNode。
如何更改框架/检测交叉点的方式,因此它只检测精灵是否在三角形内?
答案 0 :(得分:1)
我写了一些对开始设置物理有用的代码:
(P.S。我不知道哪种类型的玩家是所以我使用playerview只是为了做一个例子..)
enum CollisionTypes: UInt32 {
case Playerview = 1 // my hero
case WarField = 2 // the warfield
case Enemy1 = 4 // simple enemies type
case Enemy2 = 8 // 2 level enemies type
case Enemy3 = 16 // boss enemies type
case Enemy4 = 32 // boss of the boss enemies type
}
playerview.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
playerview.physicsBody!.categoryBitMask = CollisionTypes.Playerview.rawValue
playerview.physicsBody!.contactTestBitMask = CollisionTypes.Enemy1.rawValue | CollisionTypes.Enemy2.rawValue | CollisionTypes.Enemy3.rawValue | CollisionTypes.Enemy4.rawValue
playerview.physicsBody!.collisionBitMask = 0
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == CollisionTypes.Playerview.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Enemy1.rawValue) {
print("The collision was between the playerview and a enemy1")
}
...
}