2014-11-04 84 views
16

我剛開始用swift做ios開發,我無法弄清楚如何繪製一個圓。我只是想畫一個圓圈,將它設置爲一個變量並顯示在屏幕上,以便我稍後可以將它用作主要播放器。任何人都可以告訴我如何做到這一點或爲我提供此代碼?如何使用spriteKit在swift中繪製圓形?

我發現這個代碼在網上:

var Circle = SKShapeNode(circleOfRadius: 40) 
Circle.position = CGPointMake(500, 500) 
Circle.name = "defaultCircle" 
Circle.strokeColor = SKColor.blackColor() 
Circle.glowWidth = 10.0 
Circle.fillColor = SKColor.yellowColor() 
Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40) 
Circle.physicsBody?.dynamic = true //.physicsBody?.dynamic = true 
self.addChild(Circle) 

但是當我把這個Xcode和運行應用程序沒有在遊戲場景中出現。

+1

在你叫什麼點的代碼? – ZeMoon 2014-11-04 06:16:17

+1

如果您在iPhone模擬器上運行代碼,則無法看到該圓,因爲它的位置超出了視圖的範圍。嘗試使用circle.position = CGPointMake(100,100)或其他值 – ZeMoon 2014-11-04 06:17:57

+0

謝謝ZeMoon,這是一個愚蠢的錯誤,我沒有注意到。我將它設置爲100,100,現在我可以看到它。再次感謝。也謝謝0x141E! – elpita 2014-11-04 13:53:32

回答

27

screenshot

如果你只是想畫在某些時候一個簡單的圓形那就是:

func oneLittleCircle(){ 

    var Circle = SKShapeNode(circleOfRadius: 100) // Size of Circle 
    Circle.position = CGPointMake(frame.midX, frame.midY) //Middle of Screen 
    Circle.strokeColor = SKColor.blackColor() 
    Circle.glowWidth = 1.0 
    Circle.fillColor = SKColor.orangeColor() 
    self.addChild(Circle) 
} 

下面的代碼繪製一個圓了用戶觸摸。 您可以用下面的代碼來替換默認的iOS SpriteKit項目「GameScene.swift」代碼。

screenshot

// 
// Draw Circle At Touch .swift 
// Replace GameScene.swift in the Default SpriteKit Project, with this code. 


import SpriteKit 

class GameScene: SKScene { 
override func didMoveToView(view: SKView) { 
    /* Setup your scene here */ 
    scene?.backgroundColor = SKColor.whiteColor() //background color to white 

} 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 

    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     makeCirlceInPosition(location) // Call makeCircleInPostion, send touch location. 
    } 
} 


// Where the Magic Happens! 
func makeCirlceInPosition(location: CGPoint){ 

    var Circle = SKShapeNode(circleOfRadius: 70) // Size of Circle = Radius setting. 
    Circle.position = location //touch location passed from touchesBegan. 
    Circle.name = "defaultCircle" 
    Circle.strokeColor = SKColor.blackColor() 
    Circle.glowWidth = 1.0 
    Circle.fillColor = SKColor.clearColor() 
    self.addChild(Circle) 
} 
    override func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
}} 
+2

理想情況下,你不想使用CGPointMake。在Swift中直接使用CGPoint是首選。 – 2015-07-20 16:36:50

0

試試這個

var circle : CGRect = CGRectMake(100.0, 100.0, 80.0, 80.0) //set your dimension 
var shapeNode : SKShapeNode = SKShapeNode() 
shapeNode.path = UIBezierPath.bezierPathWithOvalInRect(circle.CGPath) 
shapeNode.fillColor = SKColor.redColor() 
shapeNode.lineWidth = 1 //set your border 
self.addChild(shapeNode) 
0

我檢查你的代碼它的工作原理,但什麼可能發生的是球消失,因爲你有動態的真實。關閉並重試。球正在退出現場。

var Circle = SKShapeNode(circleOfRadius: 40) 
    Circle.position = CGPointMake(500, 500) 
    Circle.name = "defaultCircle" 
    Circle.strokeColor = SKColor.blackColor() 
    Circle.glowWidth = 10.0 
    Circle.fillColor = SKColor.yellowColor() 
    Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40) 
    Circle.physicsBody?.dynamic = false //set to false so it doesn't fall off scene. 
    self.addChild(Circle) 
3

斯威夫特3

let Circle = SKShapeNode(circleOfRadius: 100) // Create circle 
Circle.position = CGPoint(x: 0, y: 0) // Center (given scene anchor point is 0.5 for x&y) 
Circle.strokeColor = SKColor.black 
Circle.glowWidth = 1.0 
Circle.fillColor = SKColor.orange 
self.addChild(Circle)