Sprite-kit collison

时间:2016-09-05 00:10:40

标签: ios swift sprite-kit collision skspritenode

我项目中的所有代码都运行正常,但我遇到了一些问题。当飞机接触到屏幕的边界时,它会在接触后开始旋转。我无法弄清楚如何制作它,以便在它碰到屏幕边框时不旋转。我使用时只会遇到这个问题:

plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)

其他形式的定义我没有问题,但我需要它是纹理的大小。

plane = SKSpriteNode(imageNamed: sett.store() as! String)
plane.size = CGSize(width: 80, height: 80)
plane.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 4)
// scorenode collison off, makes images spin on any contact
plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)
plane.physicsBody?.categoryBitMask = Physics.Plane
plane.physicsBody?.collisionBitMask = Physics.Bird
plane.physicsBody?.contactTestBitMask = Physics.Bird | Physics.Score | Physics.Coin
plane.physicsBody?.affectedByGravity = true
plane.physicsBody?.dynamic = true
plane.zPosition = 2
self.addChild(plane)

// screen boarder so cant leave screen
let boredBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
boredBody.friction = 0
self.physicsBody = boredBody

我的另一个问题是飞机

plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)

但我也需要这个,所以这是纹理的大小。当它在屏幕上点击我的得分节点时,它应该只增加1点,但当它越过scoreNode时它会变化1-约60点。我该如何解决这个问题?

let scoreNode = SKSpriteNode()
scoreNode.size = CGSize(width: 800, height: 1)
scoreNode.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 350)
scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.dynamic = false
scoreNode.physicsBody?.categoryBitMask = Physics.Score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = Physics.Plane
scoreNode.color = SKColor.whiteColor()

// when scoreNode and plane touch && initates score and highScore
if firstBody.categoryBitMask == Physics.Score && secondBody.categoryBitMask == Physics.Plane || firstBody.categoryBitMask == Physics.Plane && secondBody.categoryBitMask == Physics.Score {
    score += 1
    if score >= highScore{
        highScore = score
        scoreLabel.text = "SCORE: \(score)"
        highScoreLabel.text = "HIGH SCORE: \(highScore)"
        let highScoreDefault = NSUserDefaults.standardUserDefaults()
        highScoreDefault.setValue(highScore, forKey: "highScore")
        highScoreDefault.synchronize()
    } else {
        scoreLabel.text = "SCORE: \(score)"
        highScoreLabel.text = "HIGH SCORE: \(highScore)"
    }
}

1 个答案:

答案 0 :(得分:1)

由于碰撞在物理体上产生了角速度,所以它是旋转的。这是应该的。如果你真的不想让物体具有角速度,你可以通过设置节点物理体

来做到这一点

allowsRotation属性为false。

在这种情况下,它将类似于:

scoreNode.physicsBody?.allowsRotation = false

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/allowsRotation