SpriteKit游戏中的多个联系人问题

时间:2015-03-27 22:48:20

标签: skspritenode skphysicsbody

在我的游戏中,我在这些墙壁之间有一个球,墙和间隙。球和墙是SKSpriteNode,差距是SKNode。我用间隙计算得分。

我有以下代码来处理联系人和冲突。我检查了大量文档和教程,但无法找到为什么当我的球接触差距时会发生多个间隙接触。它打印"间隙接触"每次6次。

Bodytypes定义:

enum BodyType:UInt32 {
    case ball = 1
    case wall = 2
    case gap = 4
}

球:

var ball = SKSpriteNode(texture: SKTexture(imageNamed: "img/ball.png"))
ball.position = CGPointMake(frame.width/2, 50)
ball.physicsBody = SKPhysicsBody(texture: ball.texture, size: ball.size)
ball.physicsBody?.dynamic = true
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.categoryBitMask = BodyType.ball.rawValue
ball.physicsBody?.collisionBitMask = BodyType.wall.rawValue
ball.physicsBody?.contactTestBitMask = BodyType.gap.rawValue | BodyType.wall.rawValue

墙(墙每隔2秒通过scheduledTimerWithTimeInterval创建):

var wall1 = SKSpriteNode(texture: nil, color: UIColor.blackColor(), size: CGSizeMake(frame.size.width, 20))
var wall2 = SKSpriteNode(texture: nil, color: UIColor.blackColor(), size: CGSizeMake(frame.size.width, 20))
let gapSize = 70
let gapMargin = 50
let gapPoint = CGFloat(arc4random_uniform(UInt32(frame.size.width) - gapSize - (gapMargin*2)) + gapMargin)
wall1.position = CGPointMake(-wall1.size.width/2 + gapPoint, self.frame.size.height)
wall2.position = CGPointMake(wall1.size.width/2 + CGFloat(gapSize) + gapPoint, self.frame.size.height)
wall1.physicsBody = SKPhysicsBody(rectangleOfSize: wall1.size)
wall1.physicsBody?.dynamic = false
wall2.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.size)
wall2.physicsBody?.dynamic = false
wall1.physicsBody?.categoryBitMask = BodyType.wall.rawValue
wall2.physicsBody?.categoryBitMask = BodyType.wall.rawValue

差距节点:

var gap = SKNode()
gap.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(CGFloat(gapSize), wall1.size.height))
gap.position = CGPointMake(CGFloat(gapSize)/2 + gapPoint, self.frame.size.height)
gap.runAction(moveAndRemoveWall)
gap.physicsBody?.dynamic = false
gap.physicsBody?.collisionBitMask = 0
gap.physicsBody?.categoryBitMask = BodyType.gap.rawValue

联系处理:

func didBeginContact(contact: SKPhysicsContact) {

    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch(contactMask) {

    case BodyType.ball.rawValue | BodyType.gap.rawValue:

        println("gap contact")

    default:

        println("wall contact")

        movingObjects.speed = 0

        explosion(ball.position)

        gameOver = 1

    }
}

墙面碰撞完美无瑕,打印墙面接触"当我撞到墙壁时只有一次,但是我无法修复球和间隙之间的这种多重接触问题。

更新:问题在于球的物理学家。如果我使用circleOfRadius它可以完美地运行,但是,这并没有为玩家提供很好的体验,因为我的球形不是一个完美的圆形,所以我想用纹理覆盖物理体。

0 个答案:

没有答案