2015-11-15 66 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) 

和多數民衆贊成它。


也許並不完美的解決方案,但開始。至少它工作。