26

我刚开始用 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)

但是当我把它放在 xcode 上并运行应用程序时,游戏场景中什么也没有出现。

4

5 回答 5

37

截屏

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

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”代码替换为以下代码。

截屏

//
// 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 */
}}
于 2015-03-28T16:58:45.767 回答
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)
于 2017-04-23T21:28:35.993 回答
1

在现代 Swift(Xcode 13.2.1 和 Swift 5.1)中,在创建项目时尝试使用苹果的默认游戏模板。然后去掉动作文件,将GameScenegameScene.swift 中类的内容替换为:

override func didMove(to view: SKView) {
    let Circle = SKShapeNode(circleOfRadius: 40)
    Circle.position = CGPoint(x: 100, y: 100)
    Circle.name = "defaultCircle"
    Circle.strokeColor = SKColor.black
    Circle.glowWidth = 10.0
    Circle.fillColor = SKColor.yellow
    Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40)
    Circle.physicsBody?.isDynamic = false 
    self.addChild(Circle)
}
于 2022-01-04T00:33:42.367 回答
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)
于 2014-11-04T16:25:32.837 回答
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)
于 2014-12-23T22:08:41.600 回答