2014-11-04 235 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)