Post

Replies

Boosts

Views

Activity

Reply to Can I make a hollow SKPhysicsBody?
I believe I have solved my problem. What I did was essentially use SKContraint to nail each child to it's original spot in the parent. It seems to work and I hope that this is the correct answer. The code below is run at each child's creation 				let rangeX = SKRange(lowerLimit: indexSpacing, upperLimit: indexSpacing) 				let contraintX = SKConstraint.positionX(rangeX) 				let rangeY = SKRange(lowerLimit: (self.size.height/2), upperLimit: (self.size.height/2)) 				let contraintY = SKConstraint.positionY(rangeY) 				self.constraints = [contraintX, contraintY]
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to I can get GameScene:SKScene to appear using the layout constraints, but why can I still draw inside the safe ares?
Ok, I went back and forth on OOper's advice, and after repeated errors I finally came across the right combination, by accident. GameViewController launches the scene GameScene, which respects the constraints/safeAreaLayouts. However, it's a mish-mash of using myView and view. While it works, am not sure why, because reading it kind of seems disjointed. I get the constraints from view, but launch from the sub-view myView. Am I getting lucky? Or is this bound to crash? import UIKit import SpriteKit import GameplayKit var myGlobalVars = GlobalVars(backGround: SKSpriteNode(), 															pegHolder: SKSpriteNode(), currentNode: SKSpriteNode(), 															widthPoints: 0.0, heightPoints: 0.0, 															widthPixels: 0.0, heightPixels: 0.0, passGo: false, sceneRect: .zero, fileName: " ") class GameViewController: UIViewController { 		private let myView : UIView = { 				let myView = UIView() 				myView.translatesAutoresizingMaskIntoConstraints = false 				myView.backgroundColor = .clear 				return myView 		}() 		 		private func addConstraints(){ 				var constraints = [NSLayoutConstraint]() 				 				constraints.append(myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)) 				constraints.append(myView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)) 				constraints.append(myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)) 				constraints.append(myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)) 				 				NSLayoutConstraint.activate(constraints) 		} 		 		override func viewDidLayoutSubviews() { 				super.viewDidLayoutSubviews() 				let topSafeArea: CGFloat 				let bottomSafeArea: CGFloat 				if #available(iOS 11.0, *) { 						topSafeArea = view.safeAreaInsets.top 						bottomSafeArea = view.safeAreaInsets.bottom 				} else { 						topSafeArea = topLayoutGuide.length 						bottomSafeArea = bottomLayoutGuide.length 				} 		} 		let logo = UIImage(named: "startup") 		override func viewDidLoad() { 				super.viewDidLoad() 				view.addSubview(myView) 				addConstraints() 				 				if let view = self.view as! SKView? 				{ 						 						myGlobalVars.widthPixels = UIScreen.main.nativeBounds.width 						myGlobalVars.heightPixels = UIScreen.main.nativeBounds.height 						myGlobalVars.widthPoints = UIScreen.main.bounds.width 						myGlobalVars.heightPoints = UIScreen.main.bounds.height 						var scene : GameScene! 						DispatchQueue.main.async { [self] in 										 scene = GameScene(size: CGSize(width: myView.frame.width, 																											 height: myView.frame.height)) 						scene.anchorPoint = CGPoint(x: 0.0, y: 0.0) 								scene.backgroundColor = .clear 								scene.scaleMode = .aspectFit 								myGlobalVars.sceneRect = scene.frame 								myGlobalVars.gameScene = scene 								view.isHidden = false 								view.presentScene(scene) 						} 						myGlobalVars.passGo = true 						 						view.ignoresSiblingOrder = true 						view.showsFPS = true 						view.showsNodeCount = true 				} 		} 		 		override var shouldAutorotate: Bool { 				return false 		} 		override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 				if UIDevice.current.userInterfaceIdiom == .phone { 						return .allButUpsideDown 				} else { 						return .all 				} 		} 		override var prefersStatusBarHidden: Bool { 				return false 		} }
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’20
Reply to I have two objects colliding that shouldn't be
I appreciate that you tried to answer my question with no code. Sadly, the container has no physicsbody. And I can't condense this huge project into paste-able code. So I'm just going to have to rebuild. But if I may pick your brain for a couple of thoughts? If without the code you can't I understand 1 - I removed the safeAreaLayout constraints, and that didn't help but what intrigues me is.... 2 - If I increase the ball axes each by 15, no contact. 3 - The edge borders I use are set at width-1 and height-1 If I make them -15, -15... there is contact. So I'm wondering if edge based SKPhysics body have something different that I'm not aware of.
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’20
Reply to Am having trouble moving code into a separate function
That is the whole code, at the very bottom of the OP. GameScene itself is empty currently. The only two outside functions I call are: func getScreenDimensions (screen : inout ScreenDimensions) { 		screen.widthPix		 = UIScreen.main.nativeBounds.width 		screen.heightPix		= UIScreen.main.nativeBounds.height 		screen.widthPts		 = UIScreen.main.bounds.width 		screen.heightPts		= UIScreen.main.bounds.height } func setViewAttributes(view: UIView) { 		view.alpha								= 0.0 		view.frame.size.height		= UIScreen.main.nativeBounds.height 		view.frame.size.width		 = UIScreen.main.nativeBounds.width 		view.translatesAutoresizingMaskIntoConstraints = false 		view.backgroundColor			= .clear } Which work
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’20
Reply to Am having trouble moving code into a separate function
While the code in the OP works, I moved everything screen dimension related to be run in the viewDidLayoutSubviews (which is where it should be) and at that moment I was able to move this code: private func addConstraints(){ 		var constraints = [NSLayoutConstraint]() 		constraints.append(myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)) constraints.append(myView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)) constraints.append(myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)) constraints.append(myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)) NSLayoutConstraint.activate(constraints) } into it's own function, that I could then successfully call. I'm going to assume that my problem was trying do all this before viewDidLayoutSubviews and that's why I couldn't move it to its own function? Although am shocked it worked before since I called it too early. Going to wait for your thoughts before I close this thread.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’20
Reply to Am having trouble moving code into a separate function
After going through the code, over and over I realized I was calling the safeAreaLayout routine before viewDidLayoutSubviews. Now I can move my call to a function and it works. Now I'd love to know why it was working before, when I was calling it prematurely. But maybe I should be grateful that I found a substantial error. So now my code looks the same as the code in the OP, except every call regarding screen dimensions, and safeAreaLayout, are done in the viewDidLayoutSubviews function. And addConstraints is now an external function override func viewDidLayoutSubviews() { 		super.viewDidLayoutSubviews() 		getScreenDimensions (screen: &screenDims) 		view.addSubview(myView) 		addConstraints() 		myGlobalVars.sceneRect = view.frame 		createConstraints(view: myView) 		 		if #available(iOS 11.0, *) { 				myGlobalVars.topSafeArea		= view.safeAreaInsets.top 				myGlobalVars.bottomSafeArea = view.safeAreaInsets.bottom 		} else { 				myGlobalVars.topSafeArea		= topLayoutGuide.length 				myGlobalVars.bottomSafeArea = bottomLayoutGuide.length 		} }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’20
Reply to Am having trouble moving code into a separate function
For instance, why do you initialize myGlobalVars in viewDidLayoutSubviews ? Thank you for pointing that out. Claude31, Yes need to move that to viewDidLoad I strongly recommend not to add subview, not to change or add constraints in viewDidLayoutSubviews. Hello OOper, you've been super helpful before. May I ask why? Every video, or online posts say that when viewDidLayoutSubviews is called, that that's when you can be guaranteed that the scene's dimensions are set properly. The reason I add a subView was because I couldn't call the constraints on GameViewController. If I am doing this wrong, would love to hear how I should be doing it.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’20
Reply to Am having trouble moving code into a separate function
Then you should better go back to your old thread: I can get GameScene:SKScene to appear using the layout constraints, but why can I still draw inside the safe ares? - https://www.example.com/You have gone for a wrong direction too far, that's why many of your codes do not work as expected. I have gone back to that, Googled some more, and found how to get the safeAreaLayout for the GameViewControler itself, code below, so I can skip the myView part, . But that itself brings up two issues... Should I start another thread?override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() #if DEBUG print ("GVC viewWillLayoutSubviews") #endif let safeAreaInsets = self.view.safeAreaInsets; }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’20
Reply to I have a question about using self.view.safeAreaInsets in GameViewController
NO.If you add right constraints between the main view of the UIViewController and a view (myView), it should work. So, you have no need to search for equivalent. This is what I was using...found it from posts. And someone told me that that was absolutely bad, and that it would eventually ** up in my face. What I did was: GameViewController (viewWillLayoutSubviews) Created an UIView Made it a subView of GameViewController Got all constraints, from UIView, and applied them Then still within GVC** I created GameScene using the values I got from constraints I noticed tho, I don't add the GameScene to any view. Is that wrong?
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’20
Reply to I have a question about using self.view.safeAreaInsets in GameViewController
You usually add a subview and add constraints to it inside viewDidLoad(). Yikes! Yup, didn't take that into account...that would be recursivly bad. Thanks. You need to change the type of myView from UIView to  SKView, as I wrote once in another thread of yours. Except that there is no restraint for SKViews...can't find it, and have been told in more than one post there is no way to set restraints for SKViews. In SpriteKit games, you usually create a fixed size GameScene (SKScene) and make iOS scale it for the view. And there's the bottom line. I have found no way to create the GameScene size fixed other than getting constraints from myView(UIView). And remember, am not using storyboards at all.
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’20
Reply to I have a question about using self.view.safeAreaInsets in GameViewController
(complete code for GameViewController is at the bottom) Sorry, but I do not understand what you mean by restraints. I am not saying to set restraints something. Just saying to change the type of myView to SKView I meant setting the safeAreaLayout constraints, so that every screen will fall within the proper edges of the screen (iPhoneX). SKView appears to have no process to do that. Have you really removed Main.storyboard from your project? No, I couldn't. So it's still there and Main storyboard filename is set to Main within info.plist. If you're about to tell me that setting safeArea in Main.storyboard will take care of every screen...you're going hear me cry no matter where you are. And not using storyboards has nothing to do with changing the type of myView. Am only using myView as a UIView to set safeAreaLayout. If I can set SafeAreaLayout for every screen somewhere else, I wouldn't use myView at all. p.s. Would love to know how you're changing some text color here in the forum, makes reading these replies easier import UIKit import SpriteKit class GameViewController: UIViewController { 		 		public let myView : UIView = { 				let myView = UIView() 				return myView 		}() 		private func addConstraints(){ 				 var constraints = [NSLayoutConstraint]() 				//add 				 constraints.append(myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)) 				 constraints.append(myView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)) 				 constraints.append(myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)) 				 constraints.append(myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)) 				//activate 				 NSLayoutConstraint.activate(constraints) 		 } 		override func viewDidLoad() { 				super.viewDidLoad() 				 				if let view = self.view as! SKView? { 						 						view.addSubview(myView) 						var scene : GameScene! 						DispatchQueue.main.async { scene = GameScene(size: CGSize(width: screenDims.widthPts - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: screenDims.heightPts-self.view.safeAreaInsets.top-self.view.safeAreaInsets.bottom)) 								myGlobalVars.widthPoints = screenDims.widthPts - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right 								myGlobalVars.heightPoints = screenDims.heightPts - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom 								#if DEBUG 										print("SIZE \(screenDims.heightPts)") 										print("SIZE \(self.view.safeAreaInsets.top)") 										print("SIZE \(self.view.safeAreaInsets.bottom)") 								#endif 								 								scene.anchorPoint = CGPoint(x: 0.0, y: 0.0) 								scene.backgroundColor	 = .clear 								scene.scaleMode				 = .aspectFit 								view.isHidden					 = false 								view.presentScene(scene) 						} 						view.ignoresSiblingOrder		= true 						view.showsFPS							 = true 						view.showsNodeCount				 = true 						view.showsPhysics					 = true 				} 		} 		override func viewWillLayoutSubviews() { 				super.viewWillLayoutSubviews() 				#if DEBUG 						print ("GVC viewWillLayoutSubviews") 				#endif 				let constraints = [ 						view.centerXAnchor.constraint(equalTo: view.centerXAnchor), 						view.centerYAnchor.constraint(equalTo: view.centerYAnchor), 						view.widthAnchor.constraint(equalToConstant: 100), 						view.heightAnchor.constraint(equalTo: view.widthAnchor)] 				NSLayoutConstraint.activate(constraints) 		} 		override func viewDidLayoutSubviews() { 				super.viewDidLayoutSubviews() 				getScreenDimensions (screen: &screenDims) 				if myGlobalVars.safeSceneRect == .zero { 						addConstraints() 						createConstraints(view: myView) 						myGlobalVars.sceneRect = view.frame 				} 				 				if #available(iOS 11.0, *) { 						myGlobalVars.topSafeArea		= view.safeAreaInsets.top 						myGlobalVars.bottomSafeArea = view.safeAreaInsets.bottom 				} else { 						myGlobalVars.topSafeArea		= topLayoutGuide.length 						myGlobalVars.bottomSafeArea = bottomLayoutGuide.length 				} 		} 		 		override var shouldAutorotate: Bool { 				return false 		} 		override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 				if UIDevice.current.userInterfaceIdiom == .phone { 						return .portraitUpsideDown 				} else { 						return .all 				} 		} 		override var prefersStatusBarHidden: Bool { 				#if DEBUG 						print ("GVC prefersStatusBarHidden") 				#endif 				return false 		} }
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’20
Reply to I have a question about using self.view.safeAreaInsets in GameViewController
OOper .... there aren't enough letters in the alphabet to thank you, not only for going through all the trouble to edit my code, but for showing me that I had to change the class of Main.storyboard. I pulled out my source code from a few days (where I got the error below). Once I made that one change to the storyboard, it all worked. Before when I tried creating myView as an SKView I always got this error Cannot convert return expression of type 'UIView' to return type 'SKView' Side note: When I changed the class in storyboard to UIView, it was not available in the dropdown list. I had to type it in manually. On some other boards and/or posts I was told SKView couldn't apply constraints. Don't worry am going to check your answer soon, I just need to clarify two things you changed: calling addContraints() in viewDidLoad I can posts a bunch of links that say that bounds are not guaranteed to be set in viewDidLoad and to wait till viewDidLayoutSubviews Also, I noticed you removed my calling GameScene from a thread. That was used because GameScene routines were running ahead of schedule. So someone told me to use DispatchQueue.main.async. Can I go back to that? As for why I left Main.storyboard, I'll start another post for that
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’20