Trouble making SKSpriteNode bounce off anotherSKSpriteNode using Bit Masks and func didBegin(..)
Here are some brief Code Snippets:
For brevity, let's consider 4 SKSpriteNode's
The Bit Masks and the .png Strings are defined within AppDelegate, along with:
var noCollision: UInt32 = 00000000
var noContact: UInt32 = 00000000
var roomCategory: UInt32 = 00000001
var playerCategory: UInt32 = 00000010
var ballCategory: UInt32 = 00000100
var UCategory: UInt32 = 00001000
Next Snippet appears within GameViewController which calls .addChild for each Node:
myRoom = SKSpriteNode(imageNamed: "room.png")
myRoom.size = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
myRoom.physicsBody = SKPhysicsBody(rectangleOf: myRoom.size)
myRoom.physicsBody?.categoryBitMask = roomCategory
myRoom.physicsBody?.contactTestBitMask = noContact
myPlayer = SKSpriteNode(imageNamed: "player.png")
myPlayer.size = CGSize(width: playerWidth, height: playerHeight)
myPlayer.physicsBody = SKPhysicsBody(rectangleOf: myPlayer.size)
myPlayer.physicsBody?.categoryBitMask = playerCategory
myPlayer.physicsBody?.collisionBitMask = noCollision
myPlayer.physicsBody?.contactTestBitMask = noContact
myBall = SKSpriteNode(imageNamed: "ball.png")
myBall.size = CGSize(width: ballWidth, height: ballHeight)
myBall.physicsBody = SKPhysicsBody(rectangleOf: myBall.size)
myBall.physicsBody?.categoryBitMask = ballCategory
myBall.physicsBody?.collisionBitMask = noCollision
myBall.physicsBody?.contactTestBitMask = roomCategory | UCategory
myUStake = SKSpriteNode(imageNamed: "ustake.png")
myUStake.size = CGSize(width: U1Width, height: U1Height)
myUStake.physicsBody = SKPhysicsBody(rectangleOf: myU1.size)
myUStake.physicsBody?.categoryBitMask = UCategory
myUStake.physicsBody?.collisionBitMask = noCollision
myUStake.physicsBody?.contactTestBitMask = ballCategory
Finally, here's my func didBegin
func didBegin(_ contact: SKPhysicsContact) {
let bodyAName = contact.bodyA.node?.name
let bodyBName = contact.bodyB.node?.name
let playerHitBall = ((bodyBName == myPlayer.name) && (bodyAName == myBall.name)) ||
((bodyAName == myPlayer.name) && (bodyBName == myBall.name))
let ballHitU = ((bodyBName == myBall.name) && (bodyAName == myU1.name)) ||
((bodyAName == myBall.name) && (bodyBName == myU1.name))
if playerHitBall {
// moveBallIfHitsWall()
// attaBoy()
print("player hit ball")
}
if ballHitU {
// attaBoy()
// moveBallIfHitsUStake(theUStake: UStakes[0])
print("ball hit U1")
}
} // didBegin
No print statements show in the Debugger.
Anybody that has some light to shine on my error or errors would be greatly appreciated.
Thanks in advance.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
With Xcode 14.2, had trouble pairing with my Apple TV (latest update). It continuously showed "Not Connected".
So, I unpaired my apple tv and now the ATV does not show under Devices and Simulators.
My ATV shows as the Target of my Xcode Project
My Ventura 13.3 iMac mirrors to my ATV just great, but not my Xcode 14.2.
Have turned off my ATV, quit Xcode and Shutdown and then re-started my iMac and no luck.
Help desperately needed.
Other posts seem to show an identical problem experienced by others.
Specifically the Nimbus+ ... but I tried for a few days a PlayStation Dual Sense ... and the identical problem with just Mac OS Ventura 13.3.
One more thing, ZERO problems with my iPhone and your Apple TV. So, the following problem is ONLY with Ventura 13.3 (NOT 13.2 and earlier - just 13.3).
Here's the problem .. the Nimbus+ will not stay connected and after about 15 seconds disconnects by itself.
The above PlayStation Dual Sense sticks around for about 60 seconds and then disconnects by itself.
For the record, I have several times chatted with Nimbus Tech Support. Total Failure, sorry to say.
Some chatter out there in Google Land said that the blinking lights on my Nimbus+ show (to him) that the chargeable batteries are "bricked" and need to be reset. If true, then why does my Nimbus+ work great with my iPhone and Apple TV? Just asking ...
I'm a computer programmer but I am not a hardware person who can take apart the Nimbus and hit its reset button.
There you have it ... any help at all will be appreciated.
JL
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 ..
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
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.
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?
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?
I know that this is not a coding question and I have posted on the regular Apple Forum and have been working with the 800 Tech Support folk for 4 months.
I am in a world of hurt.
Have ATV + Samsung TV + external soundbar.
All software versions are current
NO Cable box (direct input). 4 months ago I had Cable box = zero problems.
With Samsung on and ATV off, stereo = Dolby 5.1 sounds great.
With ATV also on, no stereo.
I can use Siri remote to activate stereo until I change to a new movie. ATV then reverts to mono = PCM
One Tech Support lady said to just not watch multiple movie.
?$@!*+^?
I have spoken with engineers at Samsung and Sonos (soundbar). Both say my cabling is correct = 1 cable from soundbar to HDMI Arc on TV + 1 cable from ATV to another HDMI port on TV.
Another Tech Support folk said to connect ATV to soundbar. Soundbars don’t have gobs of HDMI ports, the TV does.
?!$@*^+%#
I have spoken with Apple Tech Support with zero help for 3-4 months.
I think I have been a good soldier. But it is time for real results.
Please help
How to ensure current SKScene has fully loaded before engaging it with the GamePad Controller?
MAJOR REWRITE FOR THE SAKE OF HOPEFULLY (?) INCREASED CLARITY
The problem is this = when stopping sound is involved when I do switch SKScenes, if I press the buttons of the GamePad Controller (which cycle thru these other SKScenes) too fast, the movement of the Game Pieces fails to resume when I return to the Game Scene after the above cycling.
This problem occurs only with the os(tvOS) version, but not with the iPad version. And the reason for this distinction is that each SKScene for the iPad has to fully load due to the fact that the button I press to switch SKScenes is at the top-left corner of the iPad -- so, by definition, by the time I have access to this button, the current SKScene has fully loaded.
By definition, there is no such button for os(iOS).
Given this button’s absence, I need the Swift version of jQuery’s
$(document).ready (function() {.
Any help will be appreciated to the rafters ...
GameScene’s didBegin is not called?
I have read a high volume of solutions here and many, many other Sites and I cannot figure out why?
What am I doing wrong?
Here are my relevant 2 Code Snippets:
Within GameViewController, this function is called every time I show the CGScene:
var noCollision: UInt32 = 00000000,
noContact: UInt32 = 00000000
func addGamePiecesToScene(toScene: SKScene) {
// thisSceneName is set before we're called
if thisSceneName == "GameScene" {
/*
BACKGROUND
*/
// GameScene.sks file loads this for us, but
// we still need myRoom to set the data below:
myRoom = SKSpriteNode(imageNamed: roomImg)
myRoom.name = "room"
myRoom.zPosition = 0
myRoom.size = CGSize(width: roomWidth, height: roomHeight)
myRoom.physicsBody = SKPhysicsBody(rectangleOf: myRoom.size)
myRoom.physicsBody?.categoryBitMask = roomCategory
myRoom.physicsBody?.collisionBitMask = noCollision
myRoom.physicsBody?.contactTestBitMask = noContact
/*
MAIN Game Pieces
*/
myPaddle = SKSpriteNode(imageNamed: paddleImg)
myPaddle.name = "paddle"
myPaddle.zPosition = 3
myPaddle.size = CGSize(width: paddleWidth, height: paddleHeight)
myPaddle.position = CGPoint(x: paddlePosX, y: paddlePosY) // = original or moved
myPaddle.physicsBody = SKPhysicsBody(rectangleOf: myPaddle.size)
myPaddle.physicsBody?.categoryBitMask = paddleCategory
myPaddle.physicsBody?.collisionBitMask = roomCategory | ballCategory
myPaddle.physicsBody?.contactTestBitMask = roomCategory | ballCategory
myPaddle.physicsBody!.affectedByGravity = false
myPaddle.physicsBody!.isDynamic = true
toScene.addChild(myPaddle)
//
myBall = SKSpriteNode(imageNamed: ballImg)
myBall.name = "ball"
myBall.zPosition = 4
myBall.size = CGSize(width: ballWidth, height: ballHeight)
myBall.position = CGPoint(x: ballPosX, y: ballPosY) // = original or moved
myBall.physicsBody = SKPhysicsBody(rectangleOf: myBall.size)
myBall.physicsBody?.categoryBitMask = ballCategory
myBall.physicsBody?.collisionBitMask = roomCategory
myBall.physicsBody?.contactTestBitMask = roomCategory
myBall.physicsBody!.affectedByGravity = false
myBall.physicsBody!.isDynamic = true
myBall.physicsBody!.usesPreciseCollisionDetection = true
toScene.addChild(myBall)
/*
EXTRA Game Pieces = bird, rock, trees + tower are loaded by the .sks File
*/
} // if thisSceneName == "GameScene"
} // addGamePiecesToScene
Within my GameScene is the cited didBegin function:
func didBegin(_ contact: SKPhysicsContact) {
print("didBegin") // never shows in Console
// Respond to an object hitting other objects based on their names
let bodyAName = contact.bodyA.node?.name
let bodyBName = contact.bodyB.node?.name
// N.B. – according to Apple docs,
// there is no guarantee which object is bodyA and which is bodyB
let paddleHitWall = ((bodyBName == myPaddle.name) && (bodyAName == myRoom.name)) ||
((bodyAName == myPaddle.name) && (bodyBName == myRoom.name))
let ballHitWall = ((bodyBName == myBall.name) && (bodyAName == myRoom.name)) ||
((bodyAName == myBall.name) && (bodyBName == myRoom.name))
let paddleHitBall = ((bodyBName == myPaddle.name) && (bodyAName == myBall.name)) ||
((bodyAName == myPaddle.name) && (bodyBName == myBall.name))
if paddleHitWall {
ouch()
movePaddle(dx: -dPaddleX, dy: -dPaddleY)
print("paddle hit Wall")
}
else if ballHitWall {
moveBall(dx: -dBallX, dy: -dBallY)
print("ball hit Wall")
}
else if paddleHitBall {
attaBoy()
moveBall(dx: -dBallX, dy: -dBallY)
print("paddle hit ball")
}
} // didBegin
What in the above 2 Code Snippets is wrong and, when corrected, didBegin is once again called?
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
Calling SKAction.follow(..) causes my SKSpriteNode to rotate 90 degrees CW and not stay horizontal as it follows my UIBezierPath?
I have this code (within my GameViewController Class) which implements the following of a SKSpriteNode along a UIBezierPath.
=====
Please note that a brilliant contributor solved the above challenge by creating a new Class, e.g., class NewClass: NSObject. Nevertheless, I need the solution to appear in an extension of my GameViewController
=====
func createTrainPath() {
trackRect = CGRect(x: tracksPosX - tracksWidth/2,
y: tracksPosY,
width: tracksWidth,
height: tracksHeight)
trainPath = UIBezierPath(ovalIn: trackRect)
} // createTrainPath
func startFollowTrainPath() {
var trainAction = SKAction.follow(
trainPath.cgPath,
asOffset: false,
orientToPath: true,
speed: theSpeed)
trainAction = SKAction.repeatForever(trainAction)
myTrain.run(trainAction, withKey: runTrainKey)
} // startFollowTrainPath
func stopFollowTrainPath() {
guard myTrain == nil else {
myTrain.removeAction(forKey: runTrainKey)
savedTrainPosition = getPositionFor(myTrain, orPath: trainPath)
return
}
} // stopFollowTrainPath
How to ensure current SKScene has fully loaded before engaging it with the GamePad Controller?
The problem is this = when stopping sound is involved when I do switch SKScenes, if I press the buttons of the GamePad Controller (which cycle thru these other SKScenes) too fast, the movement of the Game Pieces fails to resume when I return to the Game Scene after the above cycling.
This problem occurs only with the os(tvOS) version, but not with the iPad version. And the reason for this distinction is that each SKScene for the iPad has to fully load due to the fact that the button I press to switch SKScenes is at the top-left corner of the iPad -- so, by definition, by the time I have access to this button, the current SKScene has fully loaded.
By definition, there is no such button for os(tvOS).
Given this button’s absence, I believe I need the Swift version of jQuery’s
$(document).ready (function() {.
Any help will be appreciated to the rafters ...
I have an oval UIBezierPath with a moving SKSpriteNode,
I stop its motion and record the stopped position. I then restart this motion and want it to restart where it initially stopped.
Works great if motion is not stopped. Movement is great around entire oval Path.
Also works great as long as this stop-restart sequence occurs along the top half of the oval UIBezierPath. However, I have problems along the bottom half of this Path -- it stops okay, but the restart position is not where it previously stopped.
My method to create this oval UIBezierePath is as follows:
func createTrainPath() {
trainRect = CGRect(x: tracksPosX - tracksWidth/2,
y: tracksPosY - tracksHeight/2,
width: tracksWidth,
height: tracksHeight)
// these methods come from @DonMag
trainPoints = generatePoints(inRect: trainRect,
withNumberOfPoints: nbrPathPoints)
trainPath = generatePathFromPoints(trainPoints!,
startingAtIDX: savedTrainIndex)
} // createTrainPath
My method to stop this motion is as follows:
func stopFollowTrainPath() {
guard (myTrain != nil) else { return }
myTrain.isPaused = true
savedTrainPosition = myTrain.position
// also from @DonMag
savedTrainIndex = closestIndexInPath(
trainPath,
toPoint: savedTrainPosition) ?? 0
} // stopFollowTrainPath
Finally, I call this to re-start this motion:
func startFollowTrainPath() {
var trainAction = SKAction.follow(trainPath.cgPath,
asOffset: false,
orientToPath: true,
speed: thisSpeed)
trainAction = SKAction.repeatForever(trainAction)
myTrain.run(trainAction, withKey: runTrainKey)
myTrain.isPaused = false
} // startFollowTrainPath
Again, great if motion is not stopped. Movement is great around entire oval Path.
Again, no problem for stopping and then restarting along top half of oval .. the ohoh occurs along bottom half.
Is there something I need to do within GameScene's update method that I am missing? For example, do I need to reconstruct my UIBezierPath? every time my node moves between the top half and the bottom half and therein account for the fact that the node is traveling in the opposite direction from the top half?
Topic:
Graphics & Games
SubTopic:
SpriteKit