Ios 分数不随接触而增加

Ios 分数不随接触而增加,ios,swift,sprite-kit,skphysicsbody,Ios,Swift,Sprite Kit,Skphysicsbody,当一个物体经过另一个物体时,我正在努力提高分数 我尝试更改了contactTestBitMask,结果不一。有人知道我的代码有什么问题吗 如果有人想玩的话,我已经删除了大部分不需要的代码 import SpriteKit var contact = SKSpriteNode() var birdTexture1 = SKTexture() var scoreTexture1 = SKTexture() var birds = SKNode() var moveAndRemoveBirds =

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

我尝试更改了
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) {

}
}
导入SpriteKit
var触点=SKSpriteNode()
var birdTexture1=SKTexture()
var scoreTexture1=SKTexture()
var=SKNode()
var moveAndRemoveBirds=SKAction()
var moving=SKNode()

让scoreCategory:UInt32=1我不是100%确定,但你应该改变这两件事:

  • “contact.physicsBody?.collisionBitMask=0”到“contact.physicsBody?.collisionBitMask=0”
  • 让你的身体充满活力
“动力学”属性控制基于体积的实体是否受重力、摩擦力、与其他对象的碰撞以及直接应用于该对象的力或脉冲的影响

编辑: 此外,您还需要更改:

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

那是因为你不能确定bodyA是否永远是一个类别。这样可以确保,如果crowCategory的对象和scoreCategory中的对象发生碰撞,分数将增加


祝您好运

您的代码是否通过了
if
评分+
?什么是
联系人.bodyA
联系人.bodyB
类别BITMASK
if (contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory) || 
(contact.bodyA.categoryBitMask == scoreCategory  && contact.bodyB.categoryBitMask == crowCategory)