SpriteKit

RSS for tag

Drawing shapes, particles, text, images, and video in two dimensions using SpriteKit.

SpriteKit Documentation

Posts under SpriteKit subtopic

Post

Replies

Boosts

Views

Activity

Unable to Install Tap on Input Node in MacOS for Live Audio Speech Recognition
I'm developing a game that will use speech recognition to execute various commands. I am using code from Apple's Recognizing Speech in Live Audio documentation page. When I run this in a Swift Playground, it works just fine. However, when I make a SpriteKit game application (basic setup from Xcode's "New Project" menu option), I get the following error: required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat) Upon further research, it appears that my input node has no channels. The following is the relevant portion of my code, along with debug output: let inputNode = audioEngine.inputNode print("Number of inputs: \(inputNode.numberOfInputs)") // 1 print("Input Format: \(inputNode.inputFormat(forBus: 0))") // <AVAudioFormat 0x600001bcf200: 0 ch, 0 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved> let channelCount = inputNode.inputFormat(forBus: 0).channelCount print("Channel Count: \(channelCount)") // 0 <== Agrees with the inputFormat output listed previously // Configure the microphone input. print("Number of outputs: \(inputNode.numberOfOutputs)") // 1 let recordingFormat = inputNode.outputFormat(forBus: 0) print("Output Format: \(recordingFormat)") // <AVAudioFormat 0x600001bf3160: 2 ch, 44100 Hz, Float32, non-inter> inputNode.installTap(onBus: 0, bufferSize: 256, format: recordingFormat, block: audioTap) // <== This is where the error occurs. // NOTE: 'audioTap' is a function defined in this class. Using this defined function instead of an inline, anonymous function. The code snippet is included in the game's AppDelegate class (which includes import statements for Cocoa, AVFoundation, and Speech), and executes during its applicationDidFinishLaunching function. I'm having trouble understanding why Playground works, but a game app doesn't work. Do I need to do something specific to get the application to recognize the microphone? NOTE: This if for MacOS, NOT iOS. While the "How To" documentation cited earlier indicates iOS, Apple stated at WWDC19 that it is now supported on the MacOS. NOTE: I have included the NSSpeechRecognitionUsageDescription key in the applications plist, and successfully acknowledged the authorization request for the microphone.
1
0
2.7k
Dec ’21
SpriteKit SKScene Can't Find SKS File
I'm toying around with SpriteKit and using SKS files to construct scenes. I've started with the stock Game project and added one extra SKS file and a class to go along with it. My class will attempt to load the SKS file to build its scene just like the stock files do. However, when I go to the GameViewController and actually try to use my class, I get a runtime error that my SKS file can't be found. I've verified that the SKS file is in the project, is part of the bundle, is visible at runtime from within the bundle, but my class that uses the SKScene initializer to load the file can't see it and I'm not sure why.
0
0
511
Dec ’21
How to handle touch event when use SwiftUI & SpriteView on watchOS 8
I want to learning make a sample game on Apple Watch which install watchOS 8. I use SwiftUI to wrap the SpriteView for displaying the game scene. All work fine except handling the touch event. When I override the function " touchesEnded " in GameScene, it report error for no such function on watchOS. How to handle the tap event on watchOS ? Any suggestion is welcome, thank you. struct ContentView: View {     var sz = WKInterfaceDevice.current().screenBounds.size     var scene: SKScene {         let scene = GameScene()         scene.size = sz         scene.scaleMode = .resizeFill         return scene     }          var body: some View {         SpriteView(scene: scene)             .frame(width: sz.width, height: sz.height)             .ignoresSafeArea()     } } class GameScene: SKScene {     override func sceneDidLoad() {         print("sceneDidLoad size=\(size.width),\(size.height)")         myInit()     }   override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { // build error for no touchesEnded func on watchOS } }
0
0
526
Dec ’21
SKShapeNode bad Performance
While learning and playing around with Swift and Spritekit i run into an issue: I can´t seem to create many SKShapeNodes at Runtime. I am trying to fill the Screen with "random" Triangles (The green Triangle on top is just a "dummy" Triangle moving from the left to the right so i get to see the FPS) These brown-ish Triangles are SKShapeNodes created like this: func createTwoFloorTrianglesAtLocation(xPos:CGFloat, yPos:CGFloat, width:CGFloat, height:CGFloat, orientation:Bool) -> [SKShapeNode] { let path1 = UIBezierPath() let path2 = UIBezierPath() if orientation { path1.move(to: CGPoint(x: xPos, y: yPos)) path1.addLine(to: CGPoint(x: xPos, y: yPos + height)) path1.addLine(to: CGPoint(x: xPos + width, y: yPos)) path2.move(to: CGPoint(x: xPos + width, y: yPos)) path2.addLine(to: CGPoint(x: xPos + width, y: yPos + height)) path2.addLine(to: CGPoint(x: xPos, y: yPos + height)) } else { path1.move(to: CGPoint(x: xPos, y: yPos)) path1.addLine(to: CGPoint(x: xPos + width, y: yPos + height)) path1.addLine(to: CGPoint(x: xPos, y: yPos + height)) path2.move(to: CGPoint(x: xPos + width, y: yPos)) path2.addLine(to: CGPoint(x: xPos, y: yPos)) path2.addLine(to: CGPoint(x: xPos + width, y: yPos + height)) } let triangle1 = SKShapeNode(path: path1.cgPath) triangle1.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]! triangle1.name = "colorSprite" triangle1.lineWidth = 0.0 let triangle2 = SKShapeNode(path: path2.cgPath) triangle2.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]! triangle2.name = "colorSprite" triangle2.lineWidth = 0.0 return [triangle1, triangle2] } I add these Triangles calling #addChild(SKNode) to the GameScene. These Triangles don´t move or anything, they are supposed to disappear later in the Game by "drawing lines" above them. The Problem is that i get very bad FPS and a high CPU-Usage as well as a high Power-Usage in both the XCode-Simulator and on a real Device (iPhone 13 Pro Max, too). If i reduce the Amount of Triangles it gets better, but i´d like to not do that. Is there any way to fix this issue? I also read online (https://stackoverflow.com/questions/23461966/create-an-sktexture-from-an-skshapenode) that some use SKSpriteNode instead of SKShapeNode. But if use the following code to "convert" my ShapeNodes to SpriteNodes before adding them as a Child to the Scene, i see the Node-Counter in the Bottom left is also around 1000ish but there are no Rectangles rendered. func addNodesToParent(_ stuff:[SKShapeNode]) { for n in stuff { addChild(SKSpriteNode(texture: self.view?.texture(from: n))) //addChild(n) } } I hope someone has an idea that i can try. Thanks in advance, Greetings, Nico
1
0
981
Dec ’21
Spritekit as material of SCNnode.. crash.. bad
What I'm try to do, is replicate some digital instrument gauge ...using Spritekit.. On my project I have to use a SpriteKit Scene as material for Scenekit SCNnode. In order to do that I create a sub class of SKscene and and apply it as "material.diffuse.contents" of the SCNNode. all working fine, I can see my SKscene as material of the SCNNode. Here the issue: I try to animate/replicate the correct indication of the instrument based of some value my app received from other source. To do so, i decide to use the SKsceneDelegate and use the method "update(_ currentTime: TimeInterval)" simply the update method look for the sknode of the green needle "fulcro" remove it , and make a new line calculating the angle and new coordinate for the new line. Like this I should have a smooth needle indication... Here is the update method inside my subclass SKscene: override func update(_ currentTime: TimeInterval) {                  guard let fulcroToRemove = self.childNode(withName: "fulcro") else {return}         fulcroToRemove.removeFromParent() // issue here...          let fulcro = SKNode()         fulcro.name = "fulcro"         fulcro.position = CGPoint(x: 300, y: 260)         let line = makeLine(startPos: CGPoint(x: 0, y: 0),                             endPos: getCoordinate(angleDeg: getCurrentEGT()),                             name: "EGT_LINE")         fulcro.addChild(line)         self.addChild(fulcro)     } here how I apply the material SKscene to my SCNnode class LowerECAM : SCNNode {     var systems: PlaneSystems     init(systems: PlaneSystems) { self.systems = systems         super.init()         createLowerECAM()     }     required init?(coder: NSCoder) {         fatalError("init(coder:) has not been implemented for Make Cockpit")     }     func createLowerECAM(){ systems.lowerECAMView.presentScene(systems.newAPU)         let plane = SCNPlane(width: 1, height: 1)         let material = SCNMaterial()         material.isDoubleSided  = true         material.diffuse.contents = systems.lowerECAMView.scene // apply here plane.materials = [material]         let lowerECAM = SCNNode(geometry: plane)         lowerECAM.eulerAngles = SCNVector3(deg2rad(180), deg2rad(90), 0)         lowerECAM.scale = SCNVector3(3.3, 3.3, 3.3)         self.name = "LowerECAM"         self.addChildNode(lowerECAM)     }     func deg2rad(_ number: Double) -> Double {         return number * .pi / 180     }   func rad2deg(_ number: Double) -> Double {         return number * 180 / .pi     } } But.. I'm getting the following crash: Question?? why scene kit crash when the render I'm using is the Spritekit render.. ?? I did't even write the SceneKit render method.. The issue looks like is when I remove the "fulcro" but I can't find the solution. I tried to make a new project only with spritekit and it works perfect.. can't understand why as material of SCNode it crash. Any Suggestion.. Appreciate.. Thanks a lot
0
0
1.1k
Dec ’21
Drag and Drop Activity
Hello, please excuse this question if it seems too simple.. I am new to this and trying to learn :). Im after developing a children's app where the child would drag items into place for example images of numbers being placed in the correct drop areas etc. I don't want to use Unity for this and im keen to learn native swift... Any tips, pointers or starting points would be really welcome for this project please. Be Gentle I am totally new to this! :) Chris
1
0
518
Dec ’21
SKEmitterNode ignoring zPosition on iOS
When compiling and testing an old game (Paul Pixel - The Awakening) on the latest Xcode 12.3 for the latest iOS 14.3, the Spritekit particles appeared on top. The SKEmitterNode.zPosition value was ignored. I regularly compile the app with Xcode. So this regression happened with either iOS 14.2 or Xcode 12.2. It doesn't happen when compiling for macOS. The bug is on iOS only. Does anybody have the same problem? I have already reported the bug to Apple, but they're not responding.
3
0
1.2k
Nov ’21
How do I call scenes without using sks files in Objective-C?
I'm certain this has been asked before, but I do not want to use sks files. I have some very old Cocos2d code that I'm trying to port without a complete rewrite. I would rather programatically move my scene sprites around based on the screensize and do it in Obj-C. It is unclear to me how to do this. I sort of found an answer here: https://stackoverflow.com/questions/43927323/are-spritekit-scene-sks-files-necessary-in-my-project Tried to do something equivalent in Obj-C, but got no where.
1
0
711
Nov ’21
Unable to Install Tap on Input Node in MacOS for Live Audio Speech Recognition
I'm developing a game that will use speech recognition to execute various commands. I am using code from Apple's Recognizing Speech in Live Audio documentation page. When I run this in a Swift Playground, it works just fine. However, when I make a SpriteKit game application (basic setup from Xcode's "New Project" menu option), I get the following error: required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat) Upon further research, it appears that my input node has no channels. The following is the relevant portion of my code, along with debug output: let inputNode = audioEngine.inputNode print("Number of inputs: \(inputNode.numberOfInputs)") // 1 print("Input Format: \(inputNode.inputFormat(forBus: 0))") // <AVAudioFormat 0x600001bcf200: 0 ch, 0 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved> let channelCount = inputNode.inputFormat(forBus: 0).channelCount print("Channel Count: \(channelCount)") // 0 <== Agrees with the inputFormat output listed previously // Configure the microphone input. print("Number of outputs: \(inputNode.numberOfOutputs)") // 1 let recordingFormat = inputNode.outputFormat(forBus: 0) print("Output Format: \(recordingFormat)") // <AVAudioFormat 0x600001bf3160: 2 ch, 44100 Hz, Float32, non-inter> inputNode.installTap(onBus: 0, bufferSize: 256, format: recordingFormat, block: audioTap) // <== This is where the error occurs. // NOTE: 'audioTap' is a function defined in this class. Using this defined function instead of an inline, anonymous function. The code snippet is included in the game's AppDelegate class (which includes import statements for Cocoa, AVFoundation, and Speech), and executes during its applicationDidFinishLaunching function. I'm having trouble understanding why Playground works, but a game app doesn't work. Do I need to do something specific to get the application to recognize the microphone? NOTE: This if for MacOS, NOT iOS. While the "How To" documentation cited earlier indicates iOS, Apple stated at WWDC19 that it is now supported on the MacOS. NOTE: I have included the NSSpeechRecognitionUsageDescription key in the applications plist, and successfully acknowledged the authorization request for the microphone.
Replies
1
Boosts
0
Views
2.7k
Activity
Dec ’21
SpriteKit SKScene Can't Find SKS File
I'm toying around with SpriteKit and using SKS files to construct scenes. I've started with the stock Game project and added one extra SKS file and a class to go along with it. My class will attempt to load the SKS file to build its scene just like the stock files do. However, when I go to the GameViewController and actually try to use my class, I get a runtime error that my SKS file can't be found. I've verified that the SKS file is in the project, is part of the bundle, is visible at runtime from within the bundle, but my class that uses the SKScene initializer to load the file can't see it and I'm not sure why.
Replies
0
Boosts
0
Views
511
Activity
Dec ’21
How to handle touch event when use SwiftUI & SpriteView on watchOS 8
I want to learning make a sample game on Apple Watch which install watchOS 8. I use SwiftUI to wrap the SpriteView for displaying the game scene. All work fine except handling the touch event. When I override the function " touchesEnded " in GameScene, it report error for no such function on watchOS. How to handle the tap event on watchOS ? Any suggestion is welcome, thank you. struct ContentView: View {     var sz = WKInterfaceDevice.current().screenBounds.size     var scene: SKScene {         let scene = GameScene()         scene.size = sz         scene.scaleMode = .resizeFill         return scene     }          var body: some View {         SpriteView(scene: scene)             .frame(width: sz.width, height: sz.height)             .ignoresSafeArea()     } } class GameScene: SKScene {     override func sceneDidLoad() {         print("sceneDidLoad size=\(size.width),\(size.height)")         myInit()     }   override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { // build error for no touchesEnded func on watchOS } }
Replies
0
Boosts
0
Views
526
Activity
Dec ’21
touchesBegan for tvOS in the game?
I have a game with SpriteKit for iPhone, iPad and Mac. I am gonna make for Apple TV. But I take it override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { doesn't work for tvOS. Then what I am supposed to use to make my buttons selectable and I can press ones?
Replies
2
Boosts
0
Views
790
Activity
Dec ’21
SKShapeNode bad Performance
While learning and playing around with Swift and Spritekit i run into an issue: I can´t seem to create many SKShapeNodes at Runtime. I am trying to fill the Screen with "random" Triangles (The green Triangle on top is just a "dummy" Triangle moving from the left to the right so i get to see the FPS) These brown-ish Triangles are SKShapeNodes created like this: func createTwoFloorTrianglesAtLocation(xPos:CGFloat, yPos:CGFloat, width:CGFloat, height:CGFloat, orientation:Bool) -> [SKShapeNode] { let path1 = UIBezierPath() let path2 = UIBezierPath() if orientation { path1.move(to: CGPoint(x: xPos, y: yPos)) path1.addLine(to: CGPoint(x: xPos, y: yPos + height)) path1.addLine(to: CGPoint(x: xPos + width, y: yPos)) path2.move(to: CGPoint(x: xPos + width, y: yPos)) path2.addLine(to: CGPoint(x: xPos + width, y: yPos + height)) path2.addLine(to: CGPoint(x: xPos, y: yPos + height)) } else { path1.move(to: CGPoint(x: xPos, y: yPos)) path1.addLine(to: CGPoint(x: xPos + width, y: yPos + height)) path1.addLine(to: CGPoint(x: xPos, y: yPos + height)) path2.move(to: CGPoint(x: xPos + width, y: yPos)) path2.addLine(to: CGPoint(x: xPos, y: yPos)) path2.addLine(to: CGPoint(x: xPos + width, y: yPos + height)) } let triangle1 = SKShapeNode(path: path1.cgPath) triangle1.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]! triangle1.name = "colorSprite" triangle1.lineWidth = 0.0 let triangle2 = SKShapeNode(path: path2.cgPath) triangle2.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]! triangle2.name = "colorSprite" triangle2.lineWidth = 0.0 return [triangle1, triangle2] } I add these Triangles calling #addChild(SKNode) to the GameScene. These Triangles don´t move or anything, they are supposed to disappear later in the Game by "drawing lines" above them. The Problem is that i get very bad FPS and a high CPU-Usage as well as a high Power-Usage in both the XCode-Simulator and on a real Device (iPhone 13 Pro Max, too). If i reduce the Amount of Triangles it gets better, but i´d like to not do that. Is there any way to fix this issue? I also read online (https://stackoverflow.com/questions/23461966/create-an-sktexture-from-an-skshapenode) that some use SKSpriteNode instead of SKShapeNode. But if use the following code to "convert" my ShapeNodes to SpriteNodes before adding them as a Child to the Scene, i see the Node-Counter in the Bottom left is also around 1000ish but there are no Rectangles rendered. func addNodesToParent(_ stuff:[SKShapeNode]) { for n in stuff { addChild(SKSpriteNode(texture: self.view?.texture(from: n))) //addChild(n) } } I hope someone has an idea that i can try. Thanks in advance, Greetings, Nico
Replies
1
Boosts
0
Views
981
Activity
Dec ’21
Spritekit as material of SCNnode.. crash.. bad
What I'm try to do, is replicate some digital instrument gauge ...using Spritekit.. On my project I have to use a SpriteKit Scene as material for Scenekit SCNnode. In order to do that I create a sub class of SKscene and and apply it as "material.diffuse.contents" of the SCNNode. all working fine, I can see my SKscene as material of the SCNNode. Here the issue: I try to animate/replicate the correct indication of the instrument based of some value my app received from other source. To do so, i decide to use the SKsceneDelegate and use the method "update(_ currentTime: TimeInterval)" simply the update method look for the sknode of the green needle "fulcro" remove it , and make a new line calculating the angle and new coordinate for the new line. Like this I should have a smooth needle indication... Here is the update method inside my subclass SKscene: override func update(_ currentTime: TimeInterval) {                  guard let fulcroToRemove = self.childNode(withName: "fulcro") else {return}         fulcroToRemove.removeFromParent() // issue here...          let fulcro = SKNode()         fulcro.name = "fulcro"         fulcro.position = CGPoint(x: 300, y: 260)         let line = makeLine(startPos: CGPoint(x: 0, y: 0),                             endPos: getCoordinate(angleDeg: getCurrentEGT()),                             name: "EGT_LINE")         fulcro.addChild(line)         self.addChild(fulcro)     } here how I apply the material SKscene to my SCNnode class LowerECAM : SCNNode {     var systems: PlaneSystems     init(systems: PlaneSystems) { self.systems = systems         super.init()         createLowerECAM()     }     required init?(coder: NSCoder) {         fatalError("init(coder:) has not been implemented for Make Cockpit")     }     func createLowerECAM(){ systems.lowerECAMView.presentScene(systems.newAPU)         let plane = SCNPlane(width: 1, height: 1)         let material = SCNMaterial()         material.isDoubleSided  = true         material.diffuse.contents = systems.lowerECAMView.scene // apply here plane.materials = [material]         let lowerECAM = SCNNode(geometry: plane)         lowerECAM.eulerAngles = SCNVector3(deg2rad(180), deg2rad(90), 0)         lowerECAM.scale = SCNVector3(3.3, 3.3, 3.3)         self.name = "LowerECAM"         self.addChildNode(lowerECAM)     }     func deg2rad(_ number: Double) -> Double {         return number * .pi / 180     }   func rad2deg(_ number: Double) -> Double {         return number * 180 / .pi     } } But.. I'm getting the following crash: Question?? why scene kit crash when the render I'm using is the Spritekit render.. ?? I did't even write the SceneKit render method.. The issue looks like is when I remove the "fulcro" but I can't find the solution. I tried to make a new project only with spritekit and it works perfect.. can't understand why as material of SCNode it crash. Any Suggestion.. Appreciate.. Thanks a lot
Replies
0
Boosts
0
Views
1.1k
Activity
Dec ’21
Drag and Drop Activity
Hello, please excuse this question if it seems too simple.. I am new to this and trying to learn :). Im after developing a children's app where the child would drag items into place for example images of numbers being placed in the correct drop areas etc. I don't want to use Unity for this and im keen to learn native swift... Any tips, pointers or starting points would be really welcome for this project please. Be Gentle I am totally new to this! :) Chris
Replies
1
Boosts
0
Views
518
Activity
Dec ’21
SKEmitterNode ignoring zPosition on iOS
When compiling and testing an old game (Paul Pixel - The Awakening) on the latest Xcode 12.3 for the latest iOS 14.3, the Spritekit particles appeared on top. The SKEmitterNode.zPosition value was ignored. I regularly compile the app with Xcode. So this regression happened with either iOS 14.2 or Xcode 12.2. It doesn't happen when compiling for macOS. The bug is on iOS only. Does anybody have the same problem? I have already reported the bug to Apple, but they're not responding.
Replies
3
Boosts
0
Views
1.2k
Activity
Nov ’21
How do I call scenes without using sks files in Objective-C?
I'm certain this has been asked before, but I do not want to use sks files. I have some very old Cocos2d code that I'm trying to port without a complete rewrite. I would rather programatically move my scene sprites around based on the screensize and do it in Obj-C. It is unclear to me how to do this. I sort of found an answer here: https://stackoverflow.com/questions/43927323/are-spritekit-scene-sks-files-necessary-in-my-project Tried to do something equivalent in Obj-C, but got no where.
Replies
1
Boosts
0
Views
711
Activity
Nov ’21