0

While placing an SKSpriteNode (bar) on top another SKNode(base), it comes out as it should. Half of it lies on top of the base, the the other within. The code for this is:

self.position = CGPoint(x: self.size.width/2, y: myGV.gemBaseSize.height )

The outcome makes sense since the anchorpoint of the bar is 0.5 0.5.

When I take the anchorpoint into account, the bar ends up way above the base.

self.position = CGPoint(x: self.size.width/2, y: myGV.gemBaseSize.height + (self.size.height/2))

Below is an image that shows the first and second attempts, and below that the full code for the bar" ... only the self.position line is changed in the code. You may have to zoom in, but the lower left picture shows that the bar is half into and half on the base.

If someone has any idea of what's going on, I could really use some help.

screen shot of both attempts

Just in case, the entire code for the bar is here:

import Foundation
import SpriteKit
class MyBorder : SKSpriteNode{
    var nodeType = NodeType.border
    init(){
         //create SKSpriteNode, size it based on screen size
        super.init(texture: SKTexture(imageNamed: "border"), color: .clear, size: CGSize(width: myGV.safeSceneRect.width, height: myGV.safeSceneRect.height * 0.07))
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        self.isHidden = true
        self.isUserInteractionEnabled = false
        self.zPosition = theZ.border
        self.alpha = 0.5
        // position border on top of block
        self.position = CGPoint(x: myGV.safeSceneRect.width/2, y: myGV.blockBaseSize.height)
        print("Screen Size \(myGV.safeSceneRect)")
        print(“Block Size \(myGV.blockBaseSize)")
        print("Border Size \(self.size)")
        print("Border Pos \(self.position)")
        myGV.gameScene!.addChild(self)
        self.isHidden = false
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
LuisC329
  • 131
  • 8
  • There is lots of useful information about anchor points in this post https://stackoverflow.com/questions/1968017/changing-my-calayers-anchorpoint-moves-the-view. I had a similar problem long ago and found my solution somewhere in here, if I remember correctly. – trndjc Nov 20 '20 at 21:46
  • @bsod Read it. I don't believe it's an anchor point issue. Like I said, the first result makes total sense (where the bar is halfway in). as far as I know, the adjustment I am doing is correct. – LuisC329 Nov 21 '20 at 16:44

1 Answers1

0

My boneheaded error. Rather than delete my question, here's the answer just in case it'll help someone else.

I saved the texture file incorrectly.

It was padded on top and bottom with transparent area

LuisC329
  • 131
  • 8