2
votes

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.

1
Your maths is dizzy. Print your arrow.location in spawnArrows(). It doesn't seem to change at all. Angle is the angle of your SpriteNode. Sorry, but I have no time to check further atm... :(Akaino
degree to radian formula is let theta = angle * Float(M_PI) / 180Knight0fDragon
your radius is HUGE by the way, so huge that it is larger than your scene size, you need to make that smallerKnight0fDragon
Setting the radius to Float(self.frame.size.width)/10 and changing the radian formula to let theta = angle * Float(M_PI) / 180 solved the problem for me. You should make that an answer @Knight0fDragonAkaino

1 Answers

0
votes

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