UIBezierPath *not* showing?

I am developing a game in which my Locomotive is following a UIBezierPath that mimics an oval shaped Track.

The math of the following is not "adding up" .. if it is, I am obviously not understanding the math of the layout.

Bottom line = the following code does not display the UIBezierPath.

Here's the code that creates this UIBezierPath .. please note the math in the comments:

func createTrainPath() {
        
    trackRect = CGRect(x: 0.0,
                       y: 0.0 - roomHeight/2 + tracksHeight,
                       width: tracksWidth,
                       height: tracksHeight)
    // trackRect = (0.0, -207.0, 940.0, 476.0)   // portrait
    // trackRect = (0.0, -274.0, 470.0, 238.0)   // landscape
    print("trackRect = \(trackRect!)")
    trackPath = UIBezierPath(ovalIn: trackRect)

    let theTrackShapeNode = SKShapeNode(path: trackPath.cgPath, centered: true)
    theTrackShapeNode.zPosition = 100   // for testing
    theTrackShapeNode.position = myTracks.position
    theTrackShapeNode.lineWidth = 4.0
    theTrackShapeNode.strokeColor = SKColor.blue

    if let ourScene = GameScene(fileNamed: "GameScene") {
        print("here")   // this shows
        ourScene.addChild(theTrackShapeNode)   // does *not* show?
    }
        
}   // createTrainPath

In Portrait mode, the UIScreen measures 1024 x 1366 and the Track measures 940 x 476 with its top edge down 207 from the center of the UIScreen.

In Landscape mode, the UIScreen measures 1366 x 1024 and the Track measures 470 x 238 with its top edge down 274 from the center of the UIScreen.

In both modes, the bottom edge of the Track is supposed to be flush with the bottom edge of the UIScreen .. and it is.

If the math is correct, then I am obviously not understanding the math of the layout .. because the UIBezierPath is not showing.

The math appears correct .. but the UIBezierPath just is not showing.

So where is my brain off-center?

Replies

There are multiple ways for this to go wrong, but I think the issue is here:

                       y: 0.0 - roomHeight/2 + tracksHeight,

If roomHeight is more than 2x the size of of tracksHeight, this is going to push the node off the bottom of the scene. Don't you mean:

                       y: 0.0 + roomHeight/2 - tracksHeight,

?

Once again, my head got confused between:

tracksPosY = roomPosY - roomHeight/2 + tracksHeight/2

where the .position is negative below the (0, 0) = center of the Scene and positive above (0, 0)

With this UIBezierPath, the top of the CGRect is positive below the (0, 0) as exhibited by these data:

// (l,t,r,b)
// trackRect = (0.0, 207.0, 1024.0, 476.0)   // portrait
// trackRect = (0.0, 36.0, 1366.0, 476.0)    // landscape
// trackRect = (0.0, 64.0, 1920.0, 476.0)    // osTV

The bad news is that I still don't see a blue line anywhere.

Given that, I am still doing something insanely wrong.

I'm beginning to think my problem is with this statement:

theTrackShapeNode.position = myTracks.position

This is seriously suspect only because of the above-mentioned flip-flop of .position. and the CGRect.

What do you think?

  • In my OP, I call trackPath = UIBezierPath(ovalIn: trackRect) instead of adding lines and curves to create a UIBezierPath from scratch. Is this a problem?

Add a Comment

It is totally clear to me that I am in a total fog ...

When I set up my GameScene.sks within Xcode, I produced this:

which I set as 1366 x 1366

The frame: designation below is in line with the anchor setting .. so the fog continues as to why the blue UIBezierPath is not showing

//  print("ourScene = \(ourScene)")
//  ourScene = <SKScene> name:'GameScene'
//                       frame:{{-683, -683}, {1366, 1366}}
//                       anchor:{0.5, 0.5}