swift – 使用纹理旋转SKShapeNode

我正在尝试使用纹理旋转SKShapeNode,但它不起作用.基本上,我有一个带纹理的圆圈,我试图使用与SKSpriteNode相同的方式使其旋转:

let spin = SKAction.rotateByAngle(CGFloat(M_PI/4.0), duration: 1)

问题是圆圈是旋转的,而不是纹理.我可以使用以下代码检查:

let wait = SKAction.waitForDuration(0.5)
let block = SKAction.runBlock({
                     print(self.circle.zRotation)  
                     })
let sequence = SKAction.sequence([wait, block])
self.runAction(SKAction.repeatActionForever(sequence))

这是我创建SKShapeNode的代码:

let tex:SKTexture = SKTexture(image: image)
let circle = SKShapeNode(circleOfRadius: 100 )
circle.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 200)
circle.strokeColor = SKColor.clearColor()
circle.glowWidth = 1.0
circle.fillColor = SKColor.whiteColor()
circle.fillTexture = tex
self.addChild(circle)
// Runing the action
circle.runAction(spin)

请帮忙.提前致谢!

PS:我知道使用SKSpriteNode会更好,但我的目标是在圆形框架中放置一个方形图像,我认为使用SKShapeNode是完美的.如果有人知道如何创建循环SKSpriteNode,请随时在答案部分发布! 🙂

这就是我想要实现的(具有旋转功能):
《swift – 使用纹理旋转SKShapeNode》

最佳答案 您可以使用SKCropNode并将其mask属性设置为圆形纹理来实现您想要的效果:

override func didMoveToView(view: SKView) {

     let maskShapeTexture = SKTexture(imageNamed: "circle")

     let texture = SKTexture(imageNamed: "pictureToMask")

     let pictureToMask = SKSpriteNode(texture: texture, size: texture.size())

     let mask = SKSpriteNode(texture: maskShapeTexture) //make a circular mask

     let cropNode = SKCropNode()
     cropNode.maskNode = mask
     cropNode.addChild(pictureToMask)
     cropNode.position = CGPoint(x: frame.midX, y: frame.midY)
     addChild(cropNode)
}

假设要掩码的图片大小为300×300像素.在这种情况下,您用作遮罩的圆圈必须具有相同的尺寸.意味着在图像编辑器中制作时,圆圈本身的半径必须为150像素(直径为300像素).

掩码节点确定图片的可见区域.所以不要太大.蒙版节点必须是具有透明背景的完全不透明的圆.

点赞