Post

Replies

Boosts

Views

Activity

How do I change a UIBezierPath.currentPoint to a SKSpriteNode.position?
How do I change a UIBezierPath.currentPoint to a SKSpriteNode.position? Here are the appropriate code snippets: func createTrainPath() { let startX = -tracksWidth/2, startY = tracksPosY savedTrainPosition = CGPoint(x: startX, y: startY!) trackRect = CGRect(x: savedTrainPosition.x, y: savedTrainPosition.y, width: tracksWidth, height: tracksHeight) trainPath = UIBezierPath(ovalIn: trackRect) trainPath = trainPath.reversing() // makes myTrain move CW } // createTrainPath Followed by: func startFollowTrainPath() { let theSpeed = Double(5*thisSpeed) var trainAction = SKAction.follow( trainPath.cgPath, asOffset: false, orientToPath: true, speed: theSpeed) trainAction = SKAction.repeatForever(trainAction) createPivotNodeFor(myTrain) myTrain.run(trainAction, withKey: runTrainKey) } // startFollowTrainPath So far, so good (I think?) ... Within other places in my code, I call: return trainPath.currentPoint I need to convert trainPath.currentPoint to myTrain.position ... When I insert the appropriate print statements, I see for example: myTrain.position = (0.0, -295.05999755859375) trainPath.currentPoint = (392.0, -385.0) which obviously disqualifies a simple = , as in: myTrain.position = trainPath.currentPoint Since this = is not correct, what is ? After more investigation, my guess is that .currentPoint is in SKSpriteNode coordinates and .position is in SKScene coordinates.
0
0
660
Feb ’24
Why does orient Path = true cause the sprite node to rotate CW when the path is horizontal?
Why does orient Path = true cause the sprite node to rotate CW when the path is horizontal? Here's just one example: let trainAction = SKAction.follow( trainPath.cgPath, asOffset: false, orientToPath: true, duration: 0.1) myTrain.run(trainAction.reversed()) I have myTrain.zRotation = 0.0 myTrain is a simple locomotive,trainPath = the railroad tracks Docs state with orientToPath: true, that the Node will always orient itself such that it's facing the UIBezierPath. To me, facing means that the wheels of the locomotive are facing the BezierPath (= trainPath) Apparently I am wrong in this interpretation ... if so, what is the correct interpretation? FYI, comparing the snapshot of my initial placement of the locomotive on the tracks using .position with what happens after I start moving the locomotive: These snapshots indicate that facing means that the nose of the locomotive is facing the BezierPath (= trainPath). If true, how do I eliminate that CW rotation?
1
0
437
Jan ’24
How do I change just the starting point of an existing UIBezierPath? .. and leave all other points the same?
How do I change just the starting point of an existing UIBezierPath? .. and leave all other points the same? I have definitely seen many posts on how to do this .. honestly, I just do not understand many of them My existing path has one .move(to:) and many .addLine(to:) My goal is: (1) replace the .move(to:) with .addLine(to:) and then (2) change one of the .addLine(to:) with .move(to:) All other CGPoints stay the same. In order to get the new starting point, I set: savedPathPoint = myPath.currentPoint when I stop the following motion. At some point, I want to resume this following motion with the new starting point = savedPathPoint via myPath.move(to: savedPathPoint) let myAction = SKAction.follow( myPath.cgPath, asOffset: false, orientToPath: true, duration: 0.1) mySprite.run(myAction) When I look at the above problem description, it really appears simple. Nevertheless, its implementation alludes me. Thanks bunches in advance for any hints whatsoever.
0
0
398
Jan ’24
How to prevent initial rotation of a node when following a UIBezierPath?
Problem = I have a locomotive trying to follow an oval-shaped UIBezierPath. The challenge is that when I use orientToPath:true in my call to SKAction.follow, the locomotive immediately rotates even though the locomotive is initially positioned on the straight portion of the oval before it reaches the rounded portion of the oval. If I could prevent this initial rotation, then the rotation would not happen until it reached the rounded portion of the oval - as it should. So, can you please provide some guidance as to how I could prevent this initial rotation? Here is the code: func createTrainPath() { // this custom method ensures that the locomotive // moves CW, versus CCW trackPath = getBezierPath(theNode: myTrain, offset: 0.0) } // createTrainPath func startFollowTrainPath() { let theSpeed = Double(5*thisSpeed) var trainAction = SKAction.follow( trackPath.cgPath, asOffset: true, orientToPath: true, // <== speed: theSpeed) trainAction = SKAction.repeatForever(trainAction) myTrain.run(trainAction, withKey: runTrainKey) } // startFollowTrainPath
3
0
596
Jan ’24
How to attach a struct to the main GameViewController?
How to attach a struct to the main GameViewController? This struct occupies the top half of the main ViewController. The goal is to have this struct be part of the main GameViewController and not in separate UIHostingController. SO, when I rotate the main SKScene for example the struct also rotates OR when I switch SKScenes both the top-half struct and the bottom-half switch together because they are one SKScene. The programmer who invented HStacks and VStacks is a blooming genius … but I need to avoid what I have now which is the HStack VStack combo closing and then its neighbors in the main SKScene closing. Here are the essential code segments - all in my GameViewController: Declared at the top: var useGSO = false var gsoChildVC: UIViewController! // = UIHostingController override func viewDidLoad() { showScene() /* "gso" = "Game Static Objects" */ if useGSO { // create a UIHostingController and extract a rootView for later processing gsoChildVC = UIHostingController(rootView: GameStaticObjects()) } } // viewDidLoad Within my showScene(): func addGamePieces(toScene: SKScene) { if thisSceneName == "GameScene" { // various SKSpriteNodes // // STATIC People, Animals and Buildings // are now added to the top of our SKScene // addGSOChild(toScene: toScene) } // if thisSceneName == "GameScene" else { removeGSOChild() } } // addGamePieces func addGSOChild(toScene: SKScene) { // gsoChildVC = nil *if* useGSO = false guard gsoChildVC == nil else { let gsoParentVC = self // parent = us = GameViewController // default value // gsoChildVC.view.translatesAutoresizingMaskIntoConstraints = true /**/ let sameWidth = gsoParentVC.view.bounds.width let newHeight = 0.5 * gsoParentVC.view.bounds.height let boundsWithNewBottom = CGRectMake(0.0, 0.0, sameWidth, newHeight) gsoChildVC.view.frame = boundsWithNewBottom /**/ // first, add the view of the hild to the view of the parent gsoParentVC.view.addSubview(gsoChildVC.view) // then, add the child to the parent gsoParentVC.addChild(gsoChildVC) // notify the child ViewController that we're finished gsoChildVC.didMove(toParent: gsoParentVC) return } } // addGSOChild func removeGSOChild() { // gsoChildVC = nil *if* useGSO = false guard gsoChildVC == nil else { // First, remove the Child from its Parent gsoChildVC.removeFromParent() // Then, remove the Child's view from the Parent's gsoChildVC.view.removeFromSuperview() return } } // removeGSOChild As stated at the top of this OP, the HStack and Stack code appears great .. but the UIHostingController is distinct from my GameViewController. As a direct result, such operations as switching SKScenes is done in 2 halves - (1) the HStack and Stack top half and (2) whatever non-struct stuff appears below it. If I switch SKScenes, I want both halves to switch as one entity. FWIW, the struct that contains the HStack and Stack code is outside the GameViewController class as suggested by Apple. and as specified above, I call: gsoChildVC = UIHostingController(rootView: GameStaticObjects()) within my viewDidLoad() method. I would love some basic guidance here as to what basic error I am making.
0
0
349
Dec ’23
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?
3
0
567
Dec ’23
Resetting position and size of SKSpriteNodes with *every* rotation??
Do I really have to reset position and size of SKSpriteNodes with every rotation between landscape and portrait layout?? I've done that and it works .. but this seems to be overkill that the writers of Xcode internally compensated for .. vs. having the individual programmer having to do it. I have a getGameParms() which resets positions based on: #if os(iOS) && !targetEnvironment(macCatalyst) func getGameParms() { let roomRect = UIScreen.main.bounds roomWidth = roomRect.width * roomScale roomHeight = roomRect.height * roomScale if (thisSceneName == "GameScene") { if UIDevice.current.orientation.isLandscape { // changes here } else { // changes } } // if (thisSceneName == "GameScene") } // getGameParms #elseif os(tvOS) func getGameParms() { let roomRect = UIScreen.main.bounds roomWidth = roomRect.width * roomScale roomHeight = roomRect.height * roomScale if (thisSceneName == "GameScene") { // other changes here } } // getGameParms #endif Again, the burdensome changes are done .. but are they really necessary? As an alternative, I have called getGameParms() just once within my viewDidLoad() and the results are not correct. For example, I place one SKSpriteNode at the very bottom of the UIScreen and it does not stay there with UIDevice rotation. I also size it horizontally such that it expands to the full width of the UIScreen. Rotation destroys that. if getGameParms() is called only once as just described. Some explanation as to why would be appreciated.
0
0
403
Dec ’23
Can you access the objects in a struct from outside the struct?
Can you access the objects in a struct from outside the struct? So far I have a group of HStacks and VStacks whose embedded objects are all Images(“name”). By definition these Images are fixed in terms of their .position and .size for example. Can I dynamically access these embedded objects from outside the struct? In short, move them? .. or include a Node so I could change its color? All changes made from Swift outside the struct?
1
0
409
Dec ’23
Where do I place my code to instantiate my Project’s `struct` code within `SwiftUI`?
Where do I place my code to instantiate my Project’s struct code within SwiftUI? As you can see way below, I call: _ = StaticObjects() Within GameScene’s didMove It seems the reason I don’t see the same #Preview image in my GameScene is that I am not explicitly returning a SwiftUI some View within my struct layout. I do know that didMove is really called .. but I just don’t see anything within the Simulator My #Preview works great: The following code appears within GameScene.swift of my Xcode Project: import SpriteKit import SwiftUI struct StaticObjects : View { // some View is returned when our struct is instantiated var body: some View { let theBuildings = ["hotel", "saloon", "watertower", "jail", "farmhouse", "barn"] let theFigures = ["desperado", "sheriff", "space", "space", "horse", "guy"] VStack { HStack(alignment: .bottom) { ForEach(theBuildings, id: \.self) { theBuilding in Image(theBuilding) .resizable() // for rotation .scaledToFit() .zIndex(2) Spacer() } // ForEach } // HStack for theBuildings HStack(alignment: .bottom) { ForEach(theFigures, id: \.self) { theFigure in Image(theFigure) .resizable() // for rotation .frame(width: 120, height: 230) .offset(x: 0.0, y: -50.0) .zIndex(2) Spacer() } // ForEach } // HStack for theFigures // aligns vertically the entire struct to the top Spacer() } // VStack } // body: } // struct StaticObjects /* #Preview { StaticObjects() } */ class GameScene: SKScene, SKPhysicsContactDelegate { override func sceneDidLoad() { super.sceneDidLoad() } // sceneDidLoad override func didMove(to view: SKView) { super.didMove(to:view) physicsWorld.contactDelegate = self _ = StaticObjects() // doesn’t work } // didMove func didBegin(_ contact: SKPhysicsContact) { // ... } // didBegin } // class GameScene
2
0
363
Dec ’23
12.9 inch iPad Simulator not presenting SpriteNodes correctly for Xcode
12.9 inch iPad Simulator not presenting SpriteNodes correctly for Xcode When I generate my SKScene, I use ourScene.scaleMode = .aspectFill I then display several SKShapeNodes, e.g., let roomWidth = UIScreen.main.bounds.width let roomHeight = UIScreen.main.bounds.height let circleRadius = 30.0 itsStatusNode = SKShapeNode(circleOfRadius: circleRadius) let circleOffsetX = 95.0 let circleOffsetY = 70.0 let circlePosX = roomWidth/2 - circleRadius - circleOffsetX let circlePosY = roomHeight/2 - circleRadius - circleOffsetY itsStatusNode.position = CGPoint(x: circlePosX, y: circlePosY) toScene.addChild(itsStatusNode) The above Code works for all Xcode Simulators: iPad (10th Generation) iPad Air (5th Generation) iPad Pro (11 inch) (4th Generation) iPad Mini (6th Generation) except the iPad (12.9 inch )(6th Generation) For example, these work for all except the 12.9" (landscape) circleOffsetX = 95.0 circleOffsetY = 70.0 (portrait) circleOffsetX = 70.0 circleOffsetY = 95.0 For just the 12.9" these work (landscape) circleOffsetX = 190.0 circleOffsetY = 145.0 (portrait) circleOffsetX = 150.0 circleOffsetY = 190.0 I have seen other reports that point other errors out for just the 12.9 inch Simulator. Or, is this one of many examples when I have to upload the App to a real Device such as my iPad or Apple TV? FWIW, I only have a iPad Mini, not the 12.9 inch version As always, thanks bunches for just reading this.
1
0
529
Oct ’23
Error = Device orientations are not supported in Mac Catalyst processes
Error = Device orientations are not supported in Mac Catalyst processes Got this error when selecting the Destination = "My Mac (designed for iPad". I tried using the following Code Snippet in my GameViewController, but it did not work. Can anyone provide some guidance?: #if os(iOS) && !targetEnvironment(macCatalyst) // for rotation Landscape <-> Portrait = iOS, // but *not* for Mac (built for iPad) override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() showScene() } // viewDidLayoutSubviews #endif
1
0
524
Oct ’23
Cannot look at contents of file in Print Queue with Sonoma
Cannot look at contents of file in Print Queue with Sonoma Before Sonoma, after I selected print in a application, e.g., Word, I could double click on the document stored in the Print Queue and the pages would be displayed. With Sonoma, cannot see file contents?? One last thing pertaining to this Print Queue = I can't delete any of the accumulated "Completed" Jobs .. menu item to delete selected Jobs is dimmed??
0
0
674
Oct ’23
Error processing request from MAD on result: Error Domain=NSOSStatusErrorDomain
New after update to Xcode 15: Error processing request from MAD on result: Error Domain=NSOSStatusErrorDomain Code=-128 "Request was canceled" Here is my code which I call within my GameViewController's viewDidLoad(). present(itsPlayerController!, animated:true) { self.itsMoviePlayer?.play() self.itsMovieIsFinished = false } Here is my code which I call at various times via playMovie(movieName: "pose"): func playMovie(movieName: String) { NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: itsPlayerController?.player?.currentItem) present(itsPlayerController!, animated:true) { self.itsMoviePlayer?.play() self.itsMovieIsFinished = false } } // playMovie @objc func playerDidFinishPlaying(note: NSNotification) { self.itsPlayerController?.dismiss(animated:false, completion:nil) nextScene() changeScene() // re-init for next time setupMovie(theMovieName: "pose") } NB: If I do not call playMovie ==> NO ERROR THE PROBLEM = my call to present within playMovie. If I totally comment out present, = NO ERROR If I comment out just the guts of present, other errors are introduced. I have checked the syntax of calling present and I don't see any error there. All the above pertains to the Destination = iPad Mini (6th Generation If the Destination = Apple TV, then we get within GameViewDidLoad Unable to simultaneously satisfy constraints. For both Destinations, the Game runs OK - it's just these Warnings?? HAALP
3
0
1.4k
Sep ’23
How do I resize a new image to an existing Sprite?
I have multiple images that at various times I need to replace a target image for a SKSpriteNode. Each of these multiple images has a different size. The target SKSpriteNode has a fixed frame that I want to stay fixed. This target is created via: myTarget = SKSpriteNode(imageNamed: “target”) myTarget.size = CGSize(…) myTarget.physicsBody = SKPhysicsBody(rectangleOf: myTarget.size) How do I resize each of the multiple images so that each fills up the target frame (expand or contract)? Pretend the target is a shoebox and each image is a balloon that expands or contracts to fill the shoebox. I have tried the following that fails, that is, it changes the size of the target to fit the new image .. in short, it does the exact opposite of what I want. let newTexture = SKTexture(imageNamed: newImage) let changeImgAction = SKAction.setTexture(newTexture, resize: true) myTarget.run(changeImgAction) Again, keep frame of myTarget fixed and change size of newTexture to fit the above frame ..
1
0
837
Jun ’23