加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:34:35 所属栏目:百科 来源:网络整理
导读:我刚刚开始使用ios开发,我无法弄清楚如何画一个圆.我只是想画一个圆圈,将其设置为一个变量并显示在屏幕上,以便稍后将其用作主播放器.有人可以告诉我如何做到这一点,或者为我提供代码? 我在网上找到了这个代码: var Circle = SKShapeNode(circleOfRadius: 4
我刚刚开始使用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 */
}}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读