2

I want my SKNode To rotate like Image below :)

Image Here

Instead it is rotating around the bottom left corner of the screen! Click Here To View Video of what is happening that I do not want

How do i get it to rotate counterClockWise or Clockwise in one position like image show above?

Thank you ahead of time for someone who can help me out. not sure if i have to change anchor points or what... thank you

Here is my code below in swift.

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var top = SKSpriteNode()
var bottom = SKSpriteNode()
var line = SKSpriteNode()
var RightSide = SKSpriteNode()
var LeftSide = SKSpriteNode()
var pointBar = SKSpriteNode()
var Second_point_Bar_For_First_Hoop = SKSpriteNode()

override func didMove(to view: SKView) {

    physicsWorld.contactDelegate = self

    createHoop()

}

func createHoop() {

    top = SKSpriteNode(imageNamed: "top")
    top.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 15)
    top.size = CGSize(width: 100, height: 60)
    top.zPosition = 0

    bottom = SKSpriteNode(imageNamed: "bottom")
    bottom.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 45)
    bottom.size = CGSize(width: 100, height: 60)
    bottom.zPosition = 2

    LeftSide = SKSpriteNode()
    LeftSide.position = CGPoint(x: bottom.position.x - 40, y: bottom.position.y)
    LeftSide.size = CGSize(width: 10, height: 10)
    LeftSide.zPosition = 0
    LeftSide.color = UIColor.blue

    RightSide = SKSpriteNode()
    RightSide.position = CGPoint(x: bottom.position.x + 40, y: bottom.position.y)
    RightSide.size = CGSize(width: 5, height: 10)
    RightSide.zPosition = 0
    RightSide.color = UIColor.blue

    pointBar = SKSpriteNode()
    pointBar.position = CGPoint(x: bottom.position.x, y: bottom.position.y + 10)
    pointBar.size = CGSize(width: 90, height: 2)
    pointBar.zPosition = 100
    pointBar.color = UIColor.green
    pointBar.zPosition = 100

    Second_point_Bar_For_First_Hoop = SKSpriteNode()
    Second_point_Bar_For_First_Hoop.position = CGPoint(x: top.position.x, y: top.position.y - 10)
    Second_point_Bar_For_First_Hoop.size = CGSize(width: 90, height: 2)
    Second_point_Bar_For_First_Hoop.zPosition = 100
    Second_point_Bar_For_First_Hoop.color = UIColor.green
    Second_point_Bar_For_First_Hoop.zPosition = 100

    let hoopPair = SKNode()

    hoopPair.addChild(top)
    hoopPair.addChild(pointBar)
    hoopPair.addChild(Second_point_Bar_For_First_Hoop)
    hoopPair.addChild(bottom)
    hoopPair.addChild(LeftSide)
    hoopPair.addChild(RightSide)

    let rotate = SKAction.rotate(byAngle: 1, duration: 5)
    let repeatRotation = SKAction.repeatForever(rotate)

    hoopPair.run(repeatRotation)

    self.addChild(hoopPair)
}

override func update(_ currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */



}
}
Amen Parham
  • 73
  • 1
  • 8

2 Answers2

4

By default, SKNode anchor point is always 0.5, 0.5. This means you need to work the positions so that everything goes off of the center of the node.

Now everything is going to be relative, so your top and bottom are relative to your hoop node.

Then you need to move the hoopnode position so that it is where you want it.

Here is the code to do that:
(Note I took out all needless code to get your image to rotate on center)
(Another Node: if size does not work, use frame.size)

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var top = SKSpriteNode() var bottom = SKSpriteNode() var line = SKSpriteNode() var RightSide = SKSpriteNode() var LeftSide = SKSpriteNode() var pointBar = SKSpriteNode() var Second_point_Bar_For_First_Hoop = SKSpriteNode()

override func didMove(to view: SKView) {

    physicsWorld.contactDelegate = self

    createHoop()

}

func createHoop() {

    top = SKSpriteNode(imageNamed: "top")
    top.size = CGSize(width: 100, height: 60)
    top.position = CGPoint(x: 0, y: top.size.height/2)
    top.zPosition = 0

    bottom = SKSpriteNode(imageNamed: "bottom")
    bottom.size = CGSize(width: 100, height: 60)
    bottom.position = CGPoint(x: 0, y: -bottom.size.height/2)
    bottom.zPosition = 2


    let hoopPair = SKNode()

    hoopPair.addChild(top)
    hoopPair.addChild(bottom)

    let rotate = SKAction.rotate(byAngle: 1, duration: 5)
    let repeatRotation = SKAction.repeatForever(rotate)


    hoopPair.position = CGPoint(x:self.size.width/2,self.size.height/2)
    hoopPair.run(repeatRotation)

    self.addChild(hoopPair) }

override func update(_ currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */



} }
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • Sweet. Thank you, it works. Didn't know i had to do it that way. Thank you for the education my friend :) and of course to everyone else :) – Amen Parham Oct 18 '16 at 05:31
3

What is the anchor point of the sprite? Sprites rotate about their anchor point and yours appears to be set to (0,0) i.e. the bottom-left corner. If so, try changing it to (0.5,0.5)

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • 1
    Steve, normally this is correct, and I gave the upvote, but his issue is that his anchorpoint is set to 0.5,0.5, but his node is only drawing in 1 quadrant (The top right quadrant), not all 4, so the answer would be (0.75,0.75) to put the anchor point at the center of 1 quadrant, or to place items in all 4 quadrants – Knight0fDragon Oct 17 '16 at 15:12
  • steve, yes i think you are very close :). it is rotating around the anchor point of 0,0 but i can't seem to figure out how to change what anchor point my SKNode goes around. :) – Amen Parham Oct 17 '16 at 16:21
  • Have you tried *node.anchorPoint = CGPoint(0.5, 0.5)* – Steve Ives Oct 17 '16 at 17:13
  • 1
    Just looked it up, SKNode does not have anchor point, @AmenParham, you are going to have to center your children in the main parent, just remove all those .self.frame.width / 2, and .self.frame.height/2. and you should be fine – Knight0fDragon Oct 17 '16 at 18:30
  • What Knight0fDragon said is correct. Positioning of children inside of a parent (hoopPair) is wrong. – Whirlwind Oct 17 '16 at 19:26