Spritekit得分倍增

时间:2015-06-23 04:26:39

标签: xcode swift

游戏的得分部分运行正常,但最近我实施了GameKit,并且它在分数上增加了比代码中更多的分数。

        else if key == kCollectableStarKey {
        sprite = Collectable(texture: atlas.textureNamed("StarGold"))

        (sprite as! Collectable).collectionSound = Sound(named: "Collect.caf")
        (sprite as! Collectable).pointValue = 3
        (sprite as! Collectable).delegate = self.delegate

        sprite.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size.width * 0.3)
        sprite.physicsBody?.categoryBitMask = kCollectableCategory
        sprite.physicsBody?.dynamic = false

        self.addChild(sprite)
    }

    else if key == kBoneKey {


        sprite = Collectable(texture: atlas.textureNamed("FishBone"))


        (sprite as! Collectable).collectionSound = Sound(named: "fail.caf")
        (sprite as! Collectable).pointValue = -2
        (sprite as! Collectable).delegate = self.delegate


        sprite.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size.width * 0.3)
        sprite.physicsBody?.categoryBitMask = kCollectableCategory
        sprite.physicsBody?.dynamic = false

        self.addChild(sprite)
    }

这是Collectable协议

    protocol CollectableDelegate {

    func wasCollected(collectable: Collectable)

}

class Collectable: SKSpriteNode {

    var delegate: CollectableDelegate!
    var collectionSound: Sound!
    var pointValue: Int = 0

    func collect() {
        self.collectionSound.play()
        self.runAction(SKAction.removeFromParent())
        if let delegate = self.delegate {
            self.delegate.wasCollected(self)
        }
    }

}

和骨协议:

protocol BoneDelegate {

    func wasGathered(collectable: boneCollectable)

}

class boneCollectable: SKSpriteNode {

    var delegate: BoneDelegate!
    var gatherSound: Sound!
    var boneValue: Int = 0

    func gather() {
        self.gatherSound.play()
        self.runAction(SKAction.removeFromParent())
        if let delegate = self.delegate {
            self.delegate.wasGathered(self)
        }
    }

}

我上传了一个问题视频: Video here

1 个答案:

答案 0 :(得分:0)

所以看来问题出在我的主精灵的物理体上。

    // Setup physics body with path.
    let offsetX = self.frame.size.width * self.anchorPoint.x;
    let offsetY = self.frame.size.height * self.anchorPoint.y;
    var planeBodyPath = CGPathCreateMutable();
    CGPathMoveToPoint(planeBodyPath, nil, 19 - offsetX, 0 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 40 - offsetX, 50 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 14 - offsetX, 40 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 38 - offsetX, 11 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 11 - offsetX, 20 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 5 - offsetX, 20 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 36 - offsetX, 28 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 20 - offsetX, 27 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 20 - offsetX, 7 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 54 - offsetX, 13 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 70 - offsetX, 31 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 70 - offsetX, 40 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 69 - offsetX, 55 - offsetY);



    CGPathCloseSubpath(planeBodyPath);
    self.physicsBody = SKPhysicsBody(polygonFromPath: planeBodyPath)

删除此内容后添加:

    // Setup physics body with path.
    let offsetX = self.frame.size.width * self.anchorPoint.x;
    let offsetY = self.frame.size.height * self.anchorPoint.y;
    var planeBodyPath = CGPathCreateMutable();

    CGPathMoveToPoint(planeBodyPath, nil, 64 - offsetX, 11 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 77 - offsetX, 42 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 75 - offsetX, 10 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 38 - offsetX, 30 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 11 - offsetX, 54 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 5 - offsetX, 57 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 36 - offsetX, 61 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 76 - offsetX, 27 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 74 - offsetX, 7 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 54 - offsetX, 13 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 14 - offsetX, 31 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 27 - offsetX, 62 - offsetY);
    CGPathAddLineToPoint(planeBodyPath, nil, 69 - offsetX, 55 - offsetY);

    CGPathCloseSubpath(planeBodyPath);
    self.physicsBody = SKPhysicsBody(polygonFromPath: planeBodyPath)

所有与评分系统一起正常工作。我不知道为什么它会导致错误。