2015-11-15 121 views
2

我想在SpriteKit中绘制一个实心圆。如何使用SpriteKit绘制实心圆?

我目前正在绘制一个圆。

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

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

enter image description here

例如,圈之上被填充以0.5的值。

+0

您对您的要求的描述:“我需要根据0.0和1.0之间的值在一定水平上填充此圆。”我不清楚。你需要它看起来像图片中的一个,还是寻找渐变效果? – Aky

+0

@Aky我试图让这个圆圈看起来像图片中的那个。我编辑了我的帖子,使其更清晰。 – Sudeep

回答

1

我只有一次给你了部分答案。作为Carrl说,你可以得出那种你正在寻找使用核芯显卡的形状(除了我用它是建立在它之上UIBezierPath):

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), false, 0) 
UIColor.greenColor().set() 
let p = UIBezierPath() 
p.moveToPoint(CGPointMake(0, 50)) 
p.addArcWithCenter(CGPointMake(50, 50), radius: 50, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: true) 
p.closePath() 
p.fill() 
p.removeAllPoints() 
p.moveToPoint(CGPointMake(0, 50)) 
UIColor.greenColor().colorWithAlphaComponent(0.5).set() 
p.addArcWithCenter(CGPointMake(50, 50), radius: 50, startAngle: CGFloat(M_PI), endAngle: CGFloat(2 * M_PI), clockwise: true) 
p.closePath() 
p.fill() 
let im1 = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

circle

你仍然需要图了解如何在SKSpriteNode中将此用作纹理,但是您应该能够在此网站上找到答案。 (SKShapeNode可能是一种选择,但我最后一次在斯威夫特游乐场用它尝试过,我记得它扔在试图填补形状节点的路径错误。)来实现这一

+0

伟大的解决方案! – Sudeep

0

我建议你以编程的方法,从CoreGraphics中绘制图像:

UIGraphicsBeginImageContextWithOptions(size, false, 1) 
//your code to draw the shape here 
let imageYouWant = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

然后用imageYouWant随着图像的SKSpriteNode

+0

为0.0到1.0之间的每个可能的值创建图像是不现实的。 – NRitH

+0

@NRitH只是当你想改变状态时创建它 – duan

2

一种方法是使用SKCropNode


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

circlecirclefill


然后创建自定义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) 

和多数民众赞成它。


也许并不完美的解决方案,但开始。至少它工作。