ios 如何使用 spriteKit 快速绘制圆圈?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26727774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:29:33  来源:igfitidea点击:

How to draw a circle in swift using spriteKit?

iosswiftsprite-kitcgpointskshapenode

提问by elpita

I just started doing ios development with swift and I can't figure out how to draw a circle. I'm just trying to draw a circle, set it to a variable and display in on the screen so I can later use it as the main player. Could anyone tell me how to do this or provide me with the code for this?

我刚开始用swift做ios开发,我不知道如何画一个圆圈。我只是想画一个圆圈,将它设置为一个变量并显示在屏幕上,以便我以后可以将它用作主要播放器。谁能告诉我如何做到这一点或为我提供代码?

I found this code online:

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

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)

but when I put this on xcode and run the application nothing comes up in the game scene.

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

回答by Voltaire's Ghost

screenshot

截屏

If you just want to draw one simple circle at some point it's this:

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

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)
}

The below code draws a circle where the user touches. You can replace the default iOS SpriteKit Project "GameScene.swift" code with the below.

下面的代码在用户触摸的地方绘制一个圆圈。您可以使用以下代码替换默认的 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 */
}}

回答by Josh

Swift

迅速

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)

回答by Mr. Developer

try this

尝试这个

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)

回答by Bill peek

I check your code it works but what maybe happening is the ball disappears because you have dynamic as true. turn it off and try again. the ball is dropping out of the scene.

我检查了你的代码它有效,但可能发生的是球消失了,因为你的动态是真实的。将其关闭并重试。球正在退出现场。

 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)