为 SKCropNode 分配多个掩码

Assign Multiple Masks to SKCropNode

例如,我有一个在屏幕上向左然后向右移动的圆圈。我希望圆仅在两个特定正方形(maskNodes)内时可见。我正在使用 SKCropNode 来尝试实现这一点,但 SKCropNode 掩码只允许我分配一个掩码。有没有人知道为 SKCropNode 分配两个或更多掩码的方法,或者是否有可能这样做。谢谢!

override func didMoveToView(view: SKView) {

    anchorPoint = CGPointMake(0.5, 0.5)
    backgroundColor = UIColor.whiteColor()

    let mask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
    mask1.position.x = -100

    let mask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
    mask2.position.x = 100

    let cropNode = SKCropNode()
    cropNode.maskNode = mask1 // && mask2
    addChild(cropNode)

    let circle = SKShapeNode(circleOfRadius: 25)
    circle.fillColor = UIColor.blackColor()
    cropNode.addChild(circle)


    // Move Circle

    let moveLeft = SKAction.moveToX(-frame.size.width/2, duration: 2)
    let moveRight = SKAction.moveToX(frame.size.width/2, duration: 2)
    let seq = SKAction.repeatActionForever(SKAction.sequence([moveLeft, moveRight]))

    circle.runAction(seq)

}

想通了,只需要将两个掩码添加到父级,然后将父级指定为 SKCropNode 掩码。

override func didMoveToView(view: SKView) {

    anchorPoint = CGPointMake(0.5, 0.5)
    backgroundColor = UIColor.whiteColor()

    let maskParent = SKSpriteNode()

    let mask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
    mask1.position.x = -100
    maskParent.addChild(mask1)

    let mask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
    mask2.position.x = 100
    maskParent.addChild(mask2)

    let cropNode = SKCropNode()
    cropNode.maskNode = maskParent

    addChild(cropNode)

    let circle = SKShapeNode(circleOfRadius: 25)
    circle.fillColor = UIColor.blackColor()
    cropNode.addChild(circle)

    // Move Circle

    let moveLeft = SKAction.moveToX(-frame.size.width/2, duration: 2)
    let moveRight = SKAction.moveToX(frame.size.width/2, duration: 2)
    let seq = SKAction.repeatActionForever(SKAction.sequence([moveLeft, moveRight]))

    circle.runAction(seq)

}