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
Using Swift, how do I continuously test for a GamePad being on/connected?
When you build a Browser-based Canvas + Javascript Game, you do the above via:
function listenForGamepadConnected() {
window.addEventListener("gamepadconnected", (event) => {
// this is the BIGEE that is always testing
});
} // listenForGamepadConnected
And it definitely works!
I cannot find its equivalent in the Xcode/Swift world?
I do see the following that’s built into a boilerplate Game that you initially create using Xcode - but it seems that it just looks once, versus continuously:
func ObserveForGameControllers() {
NotificationCenter.default.addObserver(
self,
selector: #selector(connectControllers),
name: NSNotification.Name.GCControllerDidConnect,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(disconnectControllers),
name: NSNotification.Name.GCControllerDidDisconnect,
object: nil)
} // ObserveForGameControllers
@objc func connectControllers() {
// Unpause the Game if it is currently paused
self.isPaused = false
// Used to register the Nimbus Controllers to a specific Player Number
var indexNumber = 0
// Run through each controller currently connected to the system
for controller in GCController.controllers() {
// Check to see whether it is an extended Game Controller (Such as a Nimbus)
if controller.extendedGamepad != nil {
print("CONNECTED - Extended Gamepad #\(indexNumber)")
controller.playerIndex = GCControllerPlayerIndex.init(rawValue: indexNumber)!
indexNumber += 1
setupControllerControls(controller: controller)
}
else {
print("CONNECTED - but, NOT an Extended Gamepad #\(indexNumber)")
}
}
} // connectControllers
@objc func disconnectControllers() {
print("DIS-CONNECTED")
// Pause the Game if a controller is disconnected ~ This is mandated by Apple
self.isPaused = true
} // disconnectControllers
I try to call it in a Timer loop, but still does not work:
@objc func testForGamepadIsConnected() {
ObserveForGameControllers
var gamepadOn = !self.isPaused // does not work
} // testForGamepadIsConnected
func startTestForGamepad() {
guard (gamepadTimer != nil) else {
gamepadTimer = Timer.scheduledTimer(
timeInterval: 1.0,
target: self,
selector:#selector(testForGamepadIsConnected),
userInfo: nil,
repeats: true)
return
}
} // startTestForGamepad
func stopTestForGamepad() {
guard (gamepadTimer == nil) else {
gamepadTimer!.invalidate()
// .invalidate() removes Timer() from gamepadTimer, so reinitialize it.
gamepadTimer = Timer()
return
}
} // stopTestForGamepad
Scoured the Google world, but I’ve come up empty.
Would appreciate some genius out there providing what I’m missing.
Thanks loads.
Nimbus+ game controller not working with Ventura 13.3 and comparable tvOS
Prior to updating these OS' yesterday, my Computer App in the App Store worked.
After updating, not working.
I have contacted SteelSeries, the mgr of Nimbus+, and they refuse to admit it's their firmware.
And I have definitely started up their SteelSeries CG application and looked for a firmware update -- none.
Replacement of hardware is not the solution.
Any contrary ideas you folk may have would be extremely helpful.
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
Too Complex to Compile?
Ventura 13.4.1
New error after update to Xcode 14.3.1 from 14.3.0
// ... <= -(roomHeight/2 - kFudge) too complex to compile ??
if (playerPosY - playerHeight/2) <= (kFudge - roomHeight/2)
{
// ... this variation works, the previous one doesn't
}
kFudge and the other parms are declared Double! within AppDelegate.
I don't understand what is suddenly wrong after Xcode update.
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
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.
How to detect the location of a mouseDown event using Swift?
With the following code snippet, I get the error "Cannot find type 'NSEvent' in scope"?
import SpriteKit
func mouseDown(with event: NSEvent) {
if let ourScene = GameScene(fileNamed: "GameScene") {
let location = event.location(in: view)
let node:SKNode = ourScene.atPoint(location)
if (node.name == "creditsInfo") {
showCredits()
}
} // if let ourScene
} // mouseDown
Anybody have a floodlight to shine on this very basic error?
Detecting touching a SKSpriteNode within a touchesBegan event?
My experience to date has focused on using GamepadControllers with Apps, not a touch-activated iOS App.
Here are some short code snippets:
Note: the error I am trying to correct is noted in the very first snippet = touchesBegan within the comment <== shows "horse"
Yes, there is a "horse", but it is no where near the "creditsInfo" SKSpriteNode within my .sksfile.
Please note that this "creditsInfo" SKSpriteNode is programmatically generated by my addCreditsButton(..) and will be placed very near the top-left of my GameScene.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let ourScene = GameScene(fileNamed: "GameScene") {
if let touch:UITouch = touches.first {
let location = touch.location(in: view)
let node:SKNode = ourScene.atPoint(location)
print("node.name = \(node.name!)") // <== shows "horse"
if (node.name == "creditsInfo") {
showCredits()
}
}
} // if let ourScene
} // touchesBegan
The above touchesBegan function is an extension GameViewController which according to the docs is okay, namely, touchesBegan is a UIView method besides being a UIViewController method.
Within my primary showScene() function, I have:
if let ourScene = GameScene(fileNamed: "GameScene") {
#if os(iOS)
addCreditsButton(toScene: ourScene)
#endif
}
with:
func addCreditsButton(toScene: SKScene) {
if thisSceneName == "GameScene" {
itsCreditsNode.name = "creditsInfo"
itsCreditsNode.anchorPoint = CGPoint(x: 0.5, y: 0.5)
itsCreditsNode.size = CGSize(width: 2*creditsCircleRadius,
height: 2*creditsCircleRadius)
itsCreditsNode.zPosition = 3
creditsCirclePosY = roomHeight/2 - creditsCircleRadius - creditsCircleOffsetY
creditsCirclePosX = -roomWidth/2 + creditsCircleRadius + creditsCircleOffsetX
itsCreditsNode.position = CGPoint(x: creditsCirclePosX,
y: creditsCirclePosY)
toScene.addChild(itsCreditsNode)
} // if thisSceneName
} // addCreditsButton
To finish, I repeat what I stated at the very top:
The error I am trying to correct is noted in the very first snippet = touchesBegan within the comment <== shows "horse"
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
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 ...
Can you set isPaused = true of a SKSpriteNode and keep its SKEmitterNode still moving?
Topic:
Graphics & Games
SubTopic:
SpriteKit
How do I correctly show a PDF document?
iPad and Xcode 15.4
Within my GameViewController, I have:
func presentScene(_ theScene: SKScene) {
theScene.scaleMode = .resizeFill
if let skView = self.view as? SKView {
skView.ignoresSiblingOrder = true
skView.showsFPS = true
skView.showsNodeCount = true
#if os(iOS)
let theTransition = SKTransition.doorway(withDuration: 2.0)
skView.presentScene(theScene, transition: theTransition)
#elseif os(tvOS)
skView.presentScene(theScene)
#endif
}
} // presentScene
I believe presentScene(theScene) goes to the sceneDidLoad() func of theScene which adds various SKSpriteNodes to the scene via a call to addChild(theNode).
So far so good ...
Until I have a SKScene wherein I wish to display a PDF.
I use this snippet to display this PDF with this call within the SKScene's sceneDisLoad():
displayPDF("ABOUT_OLD_WEST_LOCOMOTIVE")
func displayPDF(_ itsName:String) {
let pdfView = PDFView()
guard let path = Bundle.main.path(forResource: itsName, ofType: "pdf")
else { return }
print("path")
guard let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path))
else { return }
print("pdfDocument")
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument
} // displayPDF
The 2 print statements show, yet the SKScene does not display the PDF via pdfView.document = pdfDocument?
Anyone have a clue what errors I have committed?
Appreciate it.