gpt4 book ai didi

ios - 如何使用 SpriteKit 绘制实心圆?

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

我正在尝试在 SpriteKit 中绘制一个实心圆。

我目前正在画一个圆圈。

node.physicsBody = SKPhysicsBody(circleOfRadius: radius)
node.fillColor = color
node.strokeColor = node.fillColor

我需要根据 0.0 到 1.0 之间的值在特定级别填充此圆。我怎样才能达到这个效果?

enter image description here

例如,上图圆圈的填充值为0.5。

最佳答案

实现此目的的一种方法是使用 SKCropNode


首先你需要创建两个圆形纹理,像这样;

circle circlefill


然后创建自定义SKSpriteNode:

import SpriteKit

class Circle : SKSpriteNode {
var fillSprite : SKSpriteNode!
var cropNode : SKCropNode!
var maskNode : SKSpriteNode!

override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)

fillSprite = SKSpriteNode(imageNamed: "bluecircle")

maskNode = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: self.size.width, height: self.size.height))
maskNode.position.y -= self.size.height/2

cropNode = SKCropNode()
cropNode.addChild(fillSprite)
cropNode.maskNode = maskNode
cropNode.zPosition = 1
self.addChild(cropNode)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func setFillAmount(amount: CGFloat) {
// amount parameter must be float value between 0.0 and 1.0
let newHeight = amount * (self.size.height*2)
maskNode.size = CGSize(width: self.size.width, height: newHeight)
}

}

要使用它:

// Image size is 150x150px in this example
let circle = Circle(texture: SKTexture(imageNamed: "whitecircle"), color: UIColor.whiteColor(), size: CGSize(width: 150, height: 150))
circle.setFillAmount(0.4)

就是这样。


也许不是完美的解决方案,但可以作为开始。至少它有效。

关于ios - 如何使用 SpriteKit 绘制实心圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33715305/

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