dulongj
dulongj

Reputation: 327

How can I spawn sprites every 10 degrees around a circle?

I'm very new to coding and would appreciate some help with a small game I'm making to learn. So I have a circle and I'm trying to create objects at every 10 degree interval around the circle. I tried this:

override func didMoveToView(view: SKView) {

     self.spawnArrows()

}

func spawnArrows() {

     for var i = 0; i < 36; i++ {

         let arrow = self.createArrow(specificPointOnCircle(Float(self.frame.size.width), center: CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)), angle: Float(i * 10)))
         self.addChild(arrow)

     }
}

func specificPointOnCircle(radius:Float, center:CGPoint, angle:Float) -> CGPoint {

    let theta = angle * Float(M_PI) + 2.0
    let x = radius * cosf(theta)
    let y = radius * sinf(theta)
    return CGPoint(x: CGFloat(x) + center.x, y: CGFloat(y) + center.y)

}

func createArrow(position: CGPoint) -> SKSpriteNode {

    let arrow = SKSpriteNode(imageNamed: "Arrow.png")
    arrow.zPosition = 2
    arrow.size = CGSize(width: self.frame.size.width * 0.12, height: self.frame.size.width * 0.025)
    arrow.position = position
    return arrow

}

But nothing at all is showing up. Is my math wrong somewhere, or my syntax, or perhaps both? Any help is appreciated.

Upvotes: 2

Views: 170

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16837

Issue 1:

Your formula to convert degree to radian is wrong, the formula is:

let theta = angle * Float(M_PI) / 180

Issue 2:

Your radius is the entire size of your scene, so you are plotting points from the center of your scene to the length of your scene, which goes 2x beyond the scenes width. At the very minimum, set the radius to width / 2 to get to see it at the very edge of your width

Upvotes: 0

Related Questions