如何使用spriteKit在swift中绘制圆圈?

elp*_*ita 22 cgpoint ios sprite-kit skshapenode swift

我刚开始使用swift进行ios开发,我无法弄清楚如何绘制圆圈.我只想绘制一个圆圈,将其设置为变量并显示在屏幕上,以便我以后可以将其用作主要播放器.谁能告诉我怎么做或者为我提供代码?

我在网上找到了这个代码:

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)
Run Code Online (Sandbox Code Playgroud)

但当我把它放在xcode上运行应用程序时,游戏场景中没有任何内容.

Vol*_*ost 32

截图

如果你只是想在某个时刻绘制一个简单的圆圈,那就是:

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)
}
Run Code Online (Sandbox Code Playgroud)

下面的代码绘制了一个用户触摸的圆圈.您可以使用以下代码替换默认的iOS SpriteKit项目"GameScene.swift"代码.

截图

//
// 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 */
}}
Run Code Online (Sandbox Code Playgroud)

  • 理想情况下,您不想使用CGPointMake.直接使用CGPoint在Swift中是首选. (2认同)

Jos*_*osh 5

迅速

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
addChild(circle)
Run Code Online (Sandbox Code Playgroud)