举个例子,我在屏幕上有一个向左然后向右的圆圈。我希望这个圆只有在两个特定的正方形(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)

}

关于swift - 为SKCropNode分配多个掩码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36949258/

10-12 05:09