我刚开始使用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上并运行应用程序时,游戏场景中没有任何内容。
答案 0 :(得分:29)
如果您只想在某个时刻绘制一个简单的圆圈,那就是:
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 */
}}
答案 1 :(得分: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)
答案 2 :(得分: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)
答案 3 :(得分: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)