I have two objects that collide even when they are not moving, and are apart from each other.
Now I know it's rude not to include code. But before I start reducing my code to post it up here, at let me describe what's happening with a photo. Maybe I'll get lucky and it's something I'm overlooking since I am still learning.
Here are the two screenshots. I had to spilt the link because the forum won't let me post it completely.
https ://i.ibb.co/QYHcp3L/Untitled.png
In the left shot, everything you see, the two balls, and the left and bottom edges are created where you see them. The only thing I have not done is given the edges their SKPhysicsBodies. The edges purposes are just to keep the balls on screen. My intent is to keep the edges just a little off screen, but the same thing happens. I merely moved them in for the screenshots.
But in the right shot, I've just given the edges their physics bodies, isPinned is set to true for the edges and the masks are set so that ball and edge collide.
Yet for some reason, with nothing moving, the red ball collides with the bottom and left edges till it's pushed just off to the side.
This is all done under didMove. And I even tried not giving the edges their physics attributes till I tapped on the blank screen, and boom....everything suddenly collided.
If I have missed some sort of setting, would love to know. In the meantime, I'll start condensing all my code into one much smaller file.
Post
Replies
Boosts
Views
Activity
Below is the code from GameViewController I use to present my GameScene. GameScene comes out respecting the safe areas (iPhone X).
However if I create an SKSprite node, the coords for the upper screen draw into the safe area.
My impression was that would not happen, nor can I find the top and bottom anchors.
p.s. on a side note, I cannot view originalS.frame.width or originalS.frame.height in the debugger, they come out as invalid expressions. yet they don't crash the app.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)
}
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
originalS = myView
originalS.backgroundColor = .clear
originalS.translatesAutoresizingMaskIntoConstraints = false
originalS.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
originalS.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
originalS.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
originalS.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor , constant: -0).isActive = true
var scene : GameScene!
DispatchQueue.main.async {
scene = GameScene(size: CGSize(width: originalS.frame.width,
height: originalS.frame.height))
scene.anchorPoint = CGPoint(x: 0.0, y: 0.0)
scene.backgroundColor = .black
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
view.showsPhysics = false
}
}
I would like to create a rectangular box where only the outline has an SKPhysicsBody. Because I would like to have multiple physics bodies in the rectangle.
Right now as a solid it's causing a lot of complications.
Here's the code I have:
myGSlot.physicsBody = SKPhysicsBody(rectangleOf: size)
myGSlot.physicsBody!.affectedByGravity = true
myGSlot.physicsBody!.affectedByGravity = true
myGSlot.physicsBody!.isDynamic = true
myGSlot.physicsBody!.restitution = 0.2
Is there a way I don't have to constantly unwrap myGSlot.physicsBody?
Still new, but slowly building my first game, and getting there.
I'd like to be able to add a fading streak or trail as one my SKSpriteNodes returns to it's home base.
The only thing I can think of is calculating the distance, breaking it down into x moments, then gradually move the Sprite a little, with some fade options, and then repeat in a loop till it gets back to home base.
I know that has to be wrong because it's just so ugly.
When I look at the Sprite Kit's Particle File, it has so few options. And I'm not really sure that's what I should be looking at. I have also looked at SKAction's options, and if there's an option there I'm missing it. A good book or website dedicated to SKAction would be nice.
But right am just looking for my solution.
Thanks as always
Hello, in my starter app I have:
GameScene zPosition = -1
SKScene
// takes up entire screen
BallHolder zPosition = 0
SKSpriteNode
(child of GameScene)
// Ball holder, at the bottom left of the screen, for all the balls.
// This way I just have to move the base to move all the balls
MyBall zPosition = 1
SKSpriteNode
(each child of BallHolder)
// multiple balls
If I click on the GameScene, it gets the touchesBegan.
If I click on the BallHolder, it gets the touchesBegan.
If I click on one of the balls, MyBall get's touchesBegan.
I would like to receive all touchesBegan through GameScene, then go through the array of all nodes, in reverse, at that location I touch.
So my question is...How do I make it so that GameScene handles all the touchesBegan? Or did I misunderstand how it should work?
I am using touchesBegan within the class of MyBall, which is an SKSpriteNode, itself, so it gets called when I click on the MyBall, and I do go past the guard line.
However, when I print the touchedNodes.count I get zero, and touchedNodes does have a raw value.
If anyone knows what I'm doing wrong here....or missing?
p.s. I tried doing this from GameScene itself, but touchesBegan was never received.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
		guard let touch = touches.first else { return }
		
		let location = touch.location(in: self)
		let touchedNodes = self.nodes(at: location)
		print ("\(touchedNodes.count)")
		
		//combine next two lines
		for node in touchedNodes.reversed(){
				var thisBall = node as? MyBall
				if thisBall!.nodeType == "ball" {
						self.currentNode = node
				}
		}
}
Am trying to create a game completely in Xcode, no storyboarding. My concern is not to go into the screen's safe area's layout.
While I have found a way to do this for GameViewController: UIViewController, I have no way to do it for the the GameScene: SKScene, which is where all the action takes place.
I have tried numerous methods to get the values before the GameScene is presented, looking at screen values in viewDidMoveToSuperview and other views that are supposedly called last in GameViewController, but the coordinates I get are with the full screen size, or an empty set in CGRect.
Can someone tell me how to get this done? It's been 2 days!
Thanks
Below is the code I use to create an SKSpriteNode. And I have a quick bit of code (not shown) that am using to get the point
where "touchesBegan" is initiated.
What I found is that the images I've created are touchable in the outer corners of the "round slots", that what I call them. Is there a way to make the touchable area represent the shape of the image even closer?
tried to include the screenshots as links, but was unable to
http://warptv.com/images/slot.png
http://warptv.com/images/screen.png
init(iNamed: String, sSize: CGFloat, xSpacing: CGFloat, ySpacing: CGFloat) {
let texture = SKTexture(imageNamed: iNamed)
super.init(texture: texture, color: .clear, size: texture.size())
self.anchorPoint = CGPoint(x: 0.0, y: 0.0)
self.size = CGSize(width: sSize, height: sSize)
self.zPosition = 1
self.position = CGPoint(x: xSpacing, y: ySpacing / 1.4)
myGlobalVars.pegHolder.addChild(self)
self.isHidden = false
}
The best way I can explain what I am asking is this:
I have objects that I have created within my ViewScene and I have zero intentions of having all of them interacting with anytype of gestures. So let's say I have created 5 SKSpriteNode's throughout the screen, but one of them serves no purpose. No gesture is to affect it in anyway.
My thinking is if there was a way to set that object to "ignore gestures" (I call it making it a static object) then there would be less use of my iPhone/iPad's CPU. Therefore making the app more efficient.
Am I correct? And if so, how do I set that SKSpriteNode to ignore everything gesture related?
I hope I have worded this question in a legible way.
p.s. I am not using any storyboarding in my app, am building everything via Xcode
Right now to watch an expression in the debugger I need to copy it, click add expression in the watch window, paste it, et al.
Is there a keyboard shortcut to quicken this?
I am using what I learned in the video tutorials to fill the screen (code below). aspectFit leaves me lots of blank space so I can't use that. However, after applying aspectFill the scene width & height values remain unchanged.
Is there a way I can either read size of the actual screen, or detect where the scene boundaries are off the screen?
if let view = self.view as! SKView?
{
let scene = GameScene(size: CGSize(width: 1536 ,
height: 2048))
	
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
Am creating round object and am using SKTexture rather than an SKColor. What I can't find anywhere on the web is how to scale the texture.
Please tell me there is a way
Actually I have asked this question before, but since I didn't know what I was talking about, I couldn't ask it properly. people tried to help me, but I was too lost to better explain. Now, at least I know what I'm looking for.
At the very bottom I have subclassed SKShapeNode, and I call it with the proper parameters:
pegPlaceHolder = placeHolderRect(rectOf: CGSize(width: 100, 100 ))
And all is well. What I'd like to know is there a way that I can call the subclass with added parameters so that the subclass calls the default init with the value of RectOf, as it is doing now. And then uses the other parameters within the init?
class placeHolderRect : SKShapeNode
{
var height: CGFloat = 0.0
var width: CGFloat = 0.0
override init() {
super.init()
self.isHidden = true
self.strokeColor = SKColor.green
self.fillColor = SKColor.green
self.zPosition = 0
}
}
I have looked to Google but I can't find out if SKSprite objects have a hidden or visible property? Right now am using clear as the color for stroke and fill. But if I can do it with one call the would be nice.