gpt4 book ai didi

swift - 为 SKCropNode 分配多个掩码

转载 作者:可可西里 更新时间:2023-11-01 02:17:01 24 4
gpt4 key购买 nike

例如,我有一个在屏幕上向左然后向右移动的圆圈。我希望圆仅在两个特定正方形(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com