2014-11-04 24 views
16

Tôi chỉ bắt đầu phát triển ios nhanh chóng và tôi không thể tìm ra cách vẽ một vòng tròn. Tôi chỉ cố gắng vẽ một vòng tròn, đặt nó thành một biến và hiển thị trên màn hình để sau này tôi có thể sử dụng nó làm trình phát chính. Bất cứ ai có thể cho tôi biết làm thế nào để làm điều này hoặc cung cấp cho tôi với mã cho điều này?Làm thế nào để vẽ một vòng tròn nhanh bằng spriteKit?

tôi tìm thấy mã này trực tuyến:

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) 

nhưng khi tôi đặt này trên xcode và chạy ứng dụng gì đi lên trong cảnh trò chơi.

+1

Tại thời điểm bạn có những gì được gọi là mã? – ZeMoon

+1

Nếu bạn đang chạy mã trên trình giả lập iPhone, bạn không thể nhìn thấy vòng tròn vì vị trí của nó nằm ngoài giới hạn của khung nhìn. Hãy thử sử dụng circle.position = CGPointMake (100,100) hoặc một số giá trị khác – ZeMoon

+0

Cảm ơn bạn ZeMoon đó là một sai lầm ngớ ngẩn mà tôi không nhận thấy. Tôi đặt nó là 100,100 và bây giờ tôi có thể nhìn thấy nó. Cảm ơn một lần nữa. Cũng nhờ 0x141E! – elpita

Trả lời

27

screenshot

Nếu bạn chỉ muốn vẽ một vòng tròn đơn giản tại một số điểm nó này:

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

Mã dưới đây vẽ một vòng tròn nơi chạm người dùng. Bạn có thể thay thế mã dự án mặc định của iOS SpriteKit "GameScene.swift" bằng phần bên dưới.

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 */ 
}} 
+2

Lý tưởng nhất là bạn không muốn sử dụng CGPointMake. Sử dụng trực tiếp CGPoint được ưu tiên trong Swift. –

0

thử này

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

tôi kiểm tra mã của bạn nó hoạt động nhưng những gì có thể xảy ra là bóng biến mất bởi vì bạn phải năng động như thật. tắt nó đi và thử lại. quả bóng đang rơi ra khỏi hiện trường.

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

Swift 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) 
Các vấn đề liên quan