刚开始用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 项目“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 */
}}
关于ios - 如何使用 spriteKit 快速绘制圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727774/