分数不随联系而增加

时间:2015-09-02 12:44:53

标签: ios swift sprite-kit skphysicsbody

当一个物体经过另一个物体时,我正在努力提高我的分数。

我试图更改contactTestBitMask,结果好坏参与了吗?有人知道我的代码有什么问题吗?

我已经删除了大部分不需要的代码,如果有人愿意使用它的话。

import SpriteKit

var contact = SKSpriteNode()
var birdTexture1 = SKTexture()
var scoreTexture1 = SKTexture()
var birds = SKNode()
var moveAndRemoveBirds = SKAction()
var moving = SKNode()

let scoreCategory: UInt32 = 1 << 3
let contact2Category: UInt32 = 1 << 2
let crowCategory: UInt32 = 1 << 2

var scoreLabelNode = SKLabelNode()
var score = NSInteger()
var highScoreLabelNode = SKLabelNode()
var highScore = NSInteger()


class GameScene: SKScene {
override func didMoveToView(view: SKView) {


    self.addChild(moving)
    moving.addChild(birds)

    birdTexture1 = SKTexture(imageNamed: "crow")
    birdTexture1.filteringMode = SKTextureFilteringMode.Nearest

    var distanceToMoveBird = CGFloat(self.frame.size.width + 2 * birdTexture1.size().width);
    var moveBirds = SKAction.moveByX(-distanceToMoveBird, y:0, duration:NSTimeInterval(0.0040 * distanceToMoveBird));
    var removeBirds = SKAction.removeFromParent();
    moveAndRemoveBirds = SKAction.sequence([moveBirds, removeBirds]);

    var spawnBirds = SKAction.runBlock({() in self.spawnBird()})
    var delayBirds = SKAction.waitForDuration(NSTimeInterval(4.0))
    var spawnThenDelayBirds = SKAction.sequence([spawnBirds, delayBirds])
    var spawnThenDelayForeverBirds = SKAction.repeatActionForever(spawnThenDelayBirds)
    self.runAction(spawnThenDelayForeverBirds)

    var scoreTexture1 = SKTexture(imageNamed: "contactNode")
    scoreTexture1.filteringMode = SKTextureFilteringMode.Nearest

    contact = SKSpriteNode(texture: scoreTexture1)
    contact.position = CGPoint(x: self.frame.size.width / 3.3, y: self.frame.size.height / 2.2 )
    contact.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, 800))
    contact.physicsBody?.categoryBitMask = scoreCategory
    contact.physicsBody?.contactTestBitMask = crowCategory
    contact.physicsBody?.collisionBitMask = 0
    contact.physicsBody!.dynamic = false
    contact.physicsBody!.allowsRotation = false
    self.addChild(contact)

    score = 0
    scoreLabelNode.fontName = "Helvetica-Bold"
    scoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.6, y: self.frame.size.height / 1.2 )
    scoreLabelNode.fontSize = 40
    scoreLabelNode.alpha = 0.7
    scoreLabelNode.fontColor = SKColor.redColor()
    scoreLabelNode.zPosition = -30
    scoreLabelNode.text = "Score \(score)"
    self.addChild(scoreLabelNode)

}

func spawnBird() {
    var bird = SKSpriteNode()
    bird.position = CGPointMake( self.frame.size.width + birdTexture1.size().width * 2, 0 );
    var height = UInt32( self.frame.size.height / 1 )
    var height_max = UInt32( 500 )
    var height_min = UInt32( 300 )
    var y = arc4random_uniform(height_max - height_min + 1) + height_min;
    var bird1 = SKSpriteNode(texture: birdTexture1)
    bird1.position = CGPointMake(0.0, CGFloat(y))
    bird1.physicsBody = SKPhysicsBody(rectangleOfSize: bird1.size)
    bird1.physicsBody?.dynamic = false
    bird1.physicsBody?.categoryBitMask = crowCategory
    bird1.physicsBody?.collisionBitMask = scoreCategory
    bird1.physicsBody?.contactTestBitMask = scoreCategory
    bird.addChild(bird1)

    bird.runAction(moveAndRemoveBirds)

    birds.addChild(bird)

}

func didBeginContact(contact: SKPhysicsContact) {

    if( moving.speed > 0 ) {

        if contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory {

            score++
            scoreLabelNode.text = "Score \(score)"


        }else {

            //code removed
        }

    }

}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

}

override func update(currentTime: CFTimeInterval) {

}
}

1 个答案:

答案 0 :(得分:2)

我不是百分百肯定,但你应该改变这两件事:

  • “contact.physicsBody?.collisionBitMask = 0”改为“contact.physicsBody?.collisionBitMask = crowCategory”
  • 让你的物理实体变得动态。

“动态属性控制基于体积的主体是否受到重力,摩擦,与其他对象的碰撞以及您直接应用于对象的力或冲动的影响。”

编辑: 此外,您想要更改:

if contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory {

if (contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory) || 
(contact.bodyA.categoryBitMask == scoreCategory  && contact.bodyB.categoryBitMask == crowCategory) 

那是因为你无法确定bodyA总是会成为crowCategory。通过这种方式,你可以确保如果一个crowCategory的对象和一个来自scoreCategory的对象发生碰撞,那么得分就会增加。

祝你好运