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

前端之家收集整理的这篇文章主要介绍了如何使用spriteKit在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上并运行应用程序时,游戏场景中没有任何东西出现.

如果你只是想在某个时候画一个简单的圆圈就是这样的:
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 Project“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 */
}}
原文链接:https://www.f2er.com/swift/319638.html

猜你在找的Swift相关文章