为SKCropNode分配多个掩码

时间:2016-04-30 00:06:13

标签: swift sprite-kit mask 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)

}

1 个答案:

答案 0 :(得分:1)

想出来,只需将两个掩码添加到父级,然后将父级指定为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)

}