SpriteKit

RSS for tag

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

SpriteKit Documentation

Posts under SpriteKit tag

101 Posts
Sort by:
Post not yet marked as solved
0 Replies
87 Views
I am new to SpriteKit, and I am trying to make a simple game. however, when trying to integrate gravity, the player foes not hit the floor, but instead falls behind (despite having the same zPosition). I would like to know what I need to have the floor and player collide. I have currently set the collisionBitMask and categoryBitMask, have them on the same zPosition, and have gravity turned off of the floor, but enabled on the player. is there anything else that needs to be done?
Posted
by
Post marked as solved
1 Replies
216 Views
I am updating my Watch games to use SwiftUI and am using a SpriteView to render my SpriteKit SKScene. However, my games now show the scroll bar at the top right of the Watch screen when I turn the Digital Crown. This didn't happen in the old version. Is there a way to hide the scroll bar? I see that ScrollView has a showsIndicators option to turn off the scroll bar, but I can't find this for SpriteView. Does anyone have a workaround to remove the scroll bar? This is the code I am currently using to show my GameScene.   @State private var crownPosition = 0.0   var body: some View {         GeometryReader { reader in       SpriteView(scene: GameScene(size: reader.size, crownPosition: $crownPosition))         .focusable()         .digitalCrownRotation($crownPosition)     }   } Thanks!
Posted
by
Post not yet marked as solved
2 Replies
214 Views
Hi. I'm pretty new to Swift and I'm having some trouble displaying UITextField in SpriteKit. Init() in SceneMenu will not display the UITextField, while using function showTextField() with touchesBegan() works. How can I display UITextField without the user clicking the button Show TextField? GameViewController.swift: class GameViewController: UIViewController {       override func viewDidLoad() {           let scene = SceneMenu(size: view.frame.size)     scene.scaleMode = .aspectFill     scene.backgroundColor = .white           let view = view as! SKView     view.presentScene(scene)         }     } SceneMenu.swift: class SceneMenu: SKScene {       override init(size: CGSize) {           super.init(size: size)           let btnAlert = SKLabelNode(text: "Show TextField")     btnAlert.name = "btn_text"     btnAlert.fontSize = 20     btnAlert.fontColor = SKColor.blue     btnAlert.fontName = "Avenir"     btnAlert.position = CGPoint(x: size.width / 2, y: size.height / 2)     addChild(btnAlert) let textFieldFrame = CGRect(origin: CGPoint(x: 100, y: 300), size: CGSize(width: 200, height: 30))     let textField = UITextField(frame: textFieldFrame)     textField.backgroundColor = SKColor.blue     textField.placeholder = "Type name"         view?.addSubview(textField)         }       required init?(coder aDecoder: NSCoder) {           fatalError("init(coder:) has not been implemented")         }       func showTextField() {           let textFieldFrame = CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: 200, height: 30))     let textField = UITextField(frame: textFieldFrame)     textField.backgroundColor = SKColor.blue     textField.placeholder = "Type name"         view?.addSubview(textField)         }       override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {           if let touch = touches.first {               let location = touch.location(in: self)       let action = atPoint(location)               if action.name == "btn_text" {                   showTextField()                 }             }         }     }
Posted
by
Post not yet marked as solved
6 Replies
409 Views
I'm pretty new to Swift and I'm having some trouble implementing alerts into my SpriteKit game. Well, it works however it keeps giving warnings: 2022-06-03 02:04:04.465264+0100 Project14[57880:10648157] [LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of a UICollectionViewCell that is managed by a UICollectionView is not supported, and will result in incorrect self-sizing. View: <_UIAlertControllerTextFieldViewCollectionCell: 0x13ed18d70; frame = (0 24; 270 24); gestureRecognizers = <NSArray: 0x6000009c81b0>; layer = <CALayer: 0x600000713600>> 2022-06-03 02:06:16.239075+0100 Project14[57880:10648157] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:], failed to fetch device property for senderID (778835616971358211) use primary keyboard info instead. How do I get rid of this warnings? Thanks. let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertController.Style.alert) alert.addTextField { field in field.placeholder = "Input your name" field.isSecureTextEntry = false field.textAlignment = .left } alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in print("ok") }) alert.addAction(UIAlertAction(title: "Cancel", style: .default) { _ in print("cancel") }) view?.window?.rootViewController?.present(alert, animated: true) } I cannot use just present(alert, animated: true). In this case I get compiler error - "Value of type 'SceneMenu' has no member 'present'"
Posted
by
Post not yet marked as solved
0 Replies
161 Views
About a month ago I asked whether I could use HDR in SpriteKit. This wasn't a well-phrased question since HDR seems to mean different things in different questions, which probably led to my question going unanswered. What I meant to ask was whether it's possible to use assets that have a color gamut that many modern devices are capable of displaying (XDR is somewhat standard among mid- to high-end devices). In other words: Is SpriteKit keeping up with the hardware? If not, what framework options do I have that can quickly display large Rec. 2020 images? Do any of the Core frameworks offer this capability?
Posted
by
wmk
Post not yet marked as solved
0 Replies
118 Views
I'm trying to develop a system with skspritenode spawn and then they follow the player but when ever skspritenode spawn the original action stops so it ends up with only one following the node instead of all of them.
Posted
by
Post not yet marked as solved
0 Replies
153 Views
I am aware that this question has been asked before, but the answer seems to have changed over the years, and other answers online are still not clear to me. Currently (May 2022), it seems like Apple will allow Lua scripts to be run from inside an iOS app, but I am hoping someone can provide a definitive answer. I am planning on building a Lua scripting engine into my iOS game engine to allow AI designers to control the game's AI. The game engine is written in Swift and SpriteKit and targets iOS on iPhone and iPad. I am including the Lua scripts bundled with my application bundle, which are interpreted at runtime by a Lua interpreter that I am also including inside the application bundle. I believe section 3.3.2 of the Apple Developer Program License Agreement is the pertinent section, and it sounds like using Lua scripts that are interpreted at runtime by an iOS app is fine. If anyone can offer any further confirmation or guidance to my interpretation, it would be much appreciated. My Lua scripts are directly related to the primary purpose of my app (AI scripts for a game). I posted this question on StackOverflow, but it was closed because I was told the question seeks legal advice rather than technical advice, so if that will be true here as well, can anyone tell me where I should go to ask this question?
Posted
by
Post not yet marked as solved
0 Replies
122 Views
Hi, I've written a SpriteKit game but am finding it hard to know how to add inappropriate purchases functionality within this, I know you can add a spritekit game above a swiftUI but unsure if can add swiftUI to spritekit game so can add purchase code, all info from within SpriteKit seems to not function. Or even better as code I know and love, can you add purchase functionality from within SpriteKit. would love to hear from you, Ruth
Posted
by
Post marked as solved
1 Replies
199 Views
Hello! I am new to ARKit and currently working on this sample SpriteKit AR application from Apple. To use ARKits debug option of visualizing the feature points I am trying to change the app from an AR SpriteKit base to an AR SceneKit base. (I thought it should be possible after watching this Apple session.) Therefore I made some changes. But unfortunately, the application doesn't show the camera input anymore and throws an exception after some time. The object detector still correctly identifies objects but the main screen stays gray. Help would be much appreciated! Best regards! Main changes: @IBOutlet weak var sceneView: ARSCNView! // @IBOutlet weak var sceneView: ARSKView! class ViewController: ARSCNViewDelegate {} // class ViewController: ARSKViewDelegate {} Put the SpriteKit overlayScene into a SCNPlane. Had to change how child nodes get hidden when relocalization happens.
Posted
by
Post not yet marked as solved
0 Replies
189 Views
I’m working on a matching game. In my app, I would like a functionality that tracks the touches of two nodes at time. When the user touches the nodes and if the textures displayed after the user touches the nodes match; I don’t want the user to be able to interact with the two nodes anymore. However, if the textures displayed after the user touches the two nodes do not match. I want the two nodes the user touched to reset back to their original position before the user touched the nodes. For example, let’s say the user touches the node named “fruit match card1” and it displays the texture of an “apple” then they touch the node named “fruit match card 2” and it also displays the texture of an “apple”. Since, these two nodes display the same texture that match. I don’t want the user to be able to interact with those nodes anymore since they clicked nodes that display the same texture. However, let’s say the user touched the node named “Fruit match card 1” and it displays the “Apple” texture. Then they touched the node named “Fruit match Card 3” and it displays the “Grapes” texture. Since, these two nodes do not display the same texture I want those nodes to reset back to their original position before the user touched them. Any advice or help on how I can have those kind of functionalities in my app? Basically having a functionality that tracks the touches of two nodes at a time and also a functionality that will reset the nodes back to their original position of the textures of the two nodes touched do not match? Thanks! Here is my current code: import Foundation import SpriteKit class EasyScreen: SKScene { override func didMove(to view: SKView) { var background = SKSpriteNode(imageNamed: "Easy Screen Background") let timerText = SKLabelNode(fontNamed: "Arial") timerText.fontSize = 40 timerText.fontColor = SKColor.white timerText.position = CGPoint(x: 20, y: 400) timerText.zPosition = 1 var counter:Int = 90 timerText.run(SKAction.repeatForever(SKAction.sequence([SKAction.run { counter-=1 timerText.text = " Time: \(counter)" print("\(counter)") if counter <= 0{ let newScene = TryAgainScreen(fileNamed: "Try Again Screen") newScene?.scaleMode = .aspectFill self.view?.presentScene(newScene) } },SKAction.wait(forDuration: 1)]))) background.position = CGPoint(x: 0, y: 0) background.size.width = self.size.width background.size.height = self.size.height background.anchorPoint = CGPoint(x: 0.5,y: 0.5) let matchCardOne = SKSpriteNode(imageNamed: "Fruit Match Card") let matchCardTwo = SKSpriteNode(imageNamed: "Fruit Match Card") let matchCardThree = SKSpriteNode(imageNamed: "Fruit Match Card") let matchCardFour = SKSpriteNode(imageNamed: "Fruit Match Card") let soundButton = SKSpriteNode(imageNamed: "Sound On") matchCardOne.name = "FruitMatchCard1" matchCardTwo.name = "FruitMatchCard2" matchCardThree.name = "FruitMatchCard3" matchCardFour.name = "FruitMatchCard4" soundButton.name = "Sound" matchCardOne.size = CGSize(width: 150, height: 300) matchCardTwo.size = CGSize(width: 150, height: 300) matchCardThree.size = CGSize(width: 150, height: 300) matchCardFour.size = CGSize(width: 150, height: 300) soundButton.size = CGSize(width: 75, height: 75) matchCardOne.zPosition = 1 matchCardTwo.zPosition = 1 matchCardThree.zPosition = 1 matchCardFour.zPosition = 1 soundButton.zPosition = 3 matchCardOne.anchorPoint = CGPoint(x: 0.5, y: 0.5) matchCardTwo.anchorPoint = CGPoint(x: 0.5, y: 0.5) matchCardThree.anchorPoint = CGPoint(x: 0.5, y: 0.5) matchCardFour.anchorPoint = CGPoint(x: 0.5, y: 0.5) soundButton.anchorPoint = CGPoint(x: 0.5, y: 0.5) matchCardOne.position = CGPoint(x: -125, y: 60) matchCardTwo.position = CGPoint(x: -125, y: -260) matchCardThree.position = CGPoint(x: 70, y: 60) matchCardFour.position = CGPoint(x: 70 , y: -260) soundButton.position = CGPoint(x: -180, y: -600) addChild(background) addChild(matchCardOne) addChild(matchCardTwo) addChild(matchCardThree) addChild(matchCardFour) addChild(timerText) addChild(soundButton) } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view?.isMultipleTouchEnabled = false let touch = touches.first let positionInSceneOne = touch!.location(in: self) let tappedNodes = nodes(at: positionInSceneOne) for node in tappedNodes{ if let tappedCard = node as? SKSpriteNode { if tappedCard.name == "FruitMatchCard1" { tappedCard.texture = SKTexture(imageNamed: "Apple") } } } let touchTwo = touches.first let positionInSceneTwo = touch!.location(in: self) let tappedNodesTwo = nodes(at: positionInSceneTwo) for node in tappedNodesTwo{ if let tappedCard = node as? SKSpriteNode { if tappedCard.name == "FruitMatchCard2" { tappedCard.texture = SKTexture(imageNamed: "Apple") } } } let touchThree = touches.first let positionInSceneThree = touch!.location(in: self) let tappedNodesThree = nodes(at: positionInSceneThree) for node in tappedNodesThree{ if let tappedCard = node as? SKSpriteNode { if tappedCard.name == "FruitMatchCard3" { tappedCard.texture = SKTexture(imageNamed: "Grapes") } } } let touchFour = touches.first let positionInSceneFour = touch!.location(in: self) let tappedNodesFour = nodes(at: positionInSceneFour) for node in tappedNodesFour{ if let tappedCard = node as? SKSpriteNode { if tappedCard.name == "FruitMatchCard4" { tappedCard.texture = SKTexture(imageNamed: "Grapes") } } } }
Posted
by
Post not yet marked as solved
0 Replies
167 Views
I'm looking to use high-quality and large images as animated sprites via SpriteKit. Right now the default maximum size of a texture atlas is 4096*4096, which is at least an order of magnitude below what I need. There's an option to create custom maximum sizes, but the tiny default and the age of the SpriteKit framework is giving me second thoughts even though I'd very much like to stick with an Apple framework and not have to rely on another party like Unity. Before I invest my time and energy into SpriteKit I'd like to know whether the decade-old framework, running on modern hardware (A11 and newer), can support massive texture atlases while maintaining decent performance (~30 FPS). If not, is it possible to implement something similar in less antiquated frameworks, such as RealityKit (non-AR). Thanks in advance for your time.
Posted
by
wmk
Post not yet marked as solved
0 Replies
125 Views
I'm looking to use high-quality and large images as animated sprites via SpriteKit. Is it possible to display sprites with HDR colors in SpriteKit? What about SpriteKit via SceneKit or RealityKit? I've looked all around and it seems as though *.PNG is the only accepted image format, and it doesn't seem to have HDR support.
Posted
by
wmk
Post not yet marked as solved
0 Replies
145 Views
I am developing a game for IPhone and IPad and I have created a .sks file and edited it in the Tile Map Editor using a Tile Set. I am accessing the created tile map in a Sprite Kit Scene class as SKTileMapNode and then moving it to the left programmatically then creating a sidescroller game. The problem is however that when I run the game tiles disappear from the SKTileMapNode just before the map leaves the visible area of the screen. I have tried setting shouldCullNonVisibleNodes = false but it has no effect. Has anyone encountered similar problems?
Posted
by
Post marked as solved
1 Replies
217 Views
My project involves no camera passthrough and relies heavily on sprites, but I've been discouraged from using the aging (and possibly dying) SpriteKit or SceneKit as my rendering engine by Apple engineers (here) so I'm exploring other options. Is it possible to display 2D sprites fluidly using this framework in a non-AR context? Is it possible to create, say, a 2D platformer using just RealityKit?
Posted
by
wmk
Post marked as solved
2 Replies
336 Views
I'm constructing a Swift Playground with UIKit that contains an SKScene. After creating the code I'm planning to use within an Xcode project, I tried to adapt it into my Playground. I encountered an issue while running the Playground - when a new SKSpriteNode is added to the Scene, the new and existing nodes jitter, freeze, and lag for a split-second, the FPS counter drops to around 49fps, then returns to smooth 60fps. No errors or warnings are spat, and the console is completely empty. The scene in the project didn't have this issue, and I can't recreate it on another project - even when I directly copy-paste the code from the Playground to the project. I've seen other posts with this question - and made a few myself elsewhere - and there's been no responses. I desperately need help for this as nothing I've tried is working and the deadline is in a few days! Thanks! The code that is in effect for this SKScene is below. class SimulatorController: UIViewController { override func loadView() { let view = SKView() let scene = GameScene(size: view.bounds.size) view.showsFPS = true view.showsNodeCount = true view.ignoresSiblingOrder = true scene.scaleMode = .resizeFill view.presentScene(scene) self.view = view } } class GameScene: SKScene { override func didMove(to view: SKView) { run(SKAction.repeatForever(SKAction.sequence([ SKAction.run(addNeutron), SKAction.wait(forDuration: 1) ]) )) } func random() -> CGFloat { return CGFloat(Float(arc4random()) / 0xFFFFFFFF) } func random(min: CGFloat, max: CGFloat) -> CGFloat { return random() * (max - min) + min } func addNeutron() { let neutron = SKSpriteNode(imageNamed: "neutron.heic") neutron.size = CGSize(width: 20, height: 20) neutron.physicsBody = SKPhysicsBody(rectangleOf: neutron.size) neutron.physicsBody?.isDynamic = true let actualX = random(min: 0, max: size.width) let actualY = random(min: 0, max: size.height) neutron.position = CGPoint(x: actualX, y: actualY) addChild(neutron) let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0)) let actualX2 = random(min: 0, max: size.width) let actualY2 = random(min: 0, max: size.height) let actionMove = SKAction.move(to: CGPoint(x: actualX2, y: actualY2), duration: TimeInterval(actualDuration)) let actionMoveDone = SKAction.removeFromParent() neutron.run(SKAction.sequence([actionMove, actionMoveDone])) } }
Posted
by
Post marked as solved
1 Replies
196 Views
Hi, I am Sanjiv, a Highschool student who is trying to participate in wwdc student scholarship , and I have a doubt in swift I have two bool variables, one global and one local. Once the value of the global variable changes I want the value of the local variable also to change. I want this for navigation. I have a gameScene and once the level is completed it needs to go to the next navigation view. I have tried multiple methods but none worked. Any help will be appreciated.
Posted
by
Post not yet marked as solved
0 Replies
263 Views
Hello, I am building game with SpriteKit. 2 or 3 days ago my code worked perfectly then some project files paths changed in my computer. So now my code is running on preview but it's not running on simulator. I got error message like this Thread 1: signal SIGABRT 2022-04-17 23:48:25.927505+0300 MultiplayerGame[58416:1308997] fopen failed for data file: errno = 2 (No such file or directory) 2022-04-17 23:48:25.927569+0300 MultiplayerGame[58416:1308997] Errors found! Invalidating cache... 2022-04-17 23:48:26.015933+0300 MultiplayerGame[58416:1309280] Execution of the command buffer was aborted due to an error during execution. The operation couldn’t be completed. (MTLCommandBufferErrorDomain error 9.) 2022-04-17 23:48:26.016392+0300 MultiplayerGame[58416:1309280] Execution of the command buffer was aborted due to an error during execution. The operation couldn’t be completed. (MTLCommandBufferErrorDomain error 9.) XPC_ERROR_CONNECTION_INTERRUPTED dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/mkg/Library/Developer/Xcode/DerivedData/MultiplayerGame-ghupaqvlksldiogzatxbetsvshfx/Build/Products/Debug-iphonesimulator:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMTLCapture.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib DYLD_FRAMEWORK_PATH=/Users/mkg/Library/Developer/Xcode/DerivedData/MultiplayerGame-ghupaqvlksldiogzatxbetsvshfx/Build/Products/Debug-iphonesimulator:/Users/mkg/Library/Developer/Xcode/DerivedData/MultiplayerGame-ghupaqvlksldiogzatxbetsvshfx/Build/Products/Debug-iphonesimulator/PackageFrameworks CoreSimulator 802.6 - Device: iPhone 12 Pro Max (50569CD6-26AA-45EF-BE07-3B5F9702915E) - Runtime: iOS 15.4 (19E240) - DeviceType: iPhone 12 Pro Max (lldb)
Posted
by
Post not yet marked as solved
1 Replies
197 Views
Hi, I just wanted to display a SpriteKit Scene in a SCNPlane. So I set the the SCNMaterial contents to my SKScene, but instead of getting the scene I'm getting a grey plane. This is my code by the way: var mainScene: SKScene {     let scene = Game()     scene.size = CGSize(width: 1024, height: 1024)     scene.scaleMode = .resizeFill     scene.backgroundColor = .purple     scene.view?.backgroundColor = .purple     scene.view?.allowsTransparency = false     return scene } func initMainScene() -> SceneView {     mainScene.view?.isPaused = false     let scene = SCNScene()     let mainSceneMaterial = SCNMaterial()     mainSceneMaterial.normal.contents = mainScene     mainSceneMaterial.isDoubleSided = true     let planeGeometry = SCNPlane(width: 1, height: 1)     planeGeometry.materials = [mainSceneMaterial]     let plane: SCNNode = SCNNode(geometry: planeGeometry)     let camera: SCNNode = SCNNode()     camera.name = "Camera"     camera.camera = SCNCamera()     camera.position = SCNVector3(x: 0.0, y: 0.0, z: 4.0)     let light: SCNNode = SCNNode()     light.light =  SCNLight()     light.light!.type = .omni     light.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)     scene.rootNode.addChildNode(camera)     scene.rootNode.addChildNode(light)     scene.rootNode.addChildNode(plane)     return SceneView(         scene: scene,         pointOfView: scene.rootNode.childNode(withName: "Camera", recursively: false),         options: []     ) } Here is the screenshot: Also, my SpriteKit scene has touchesBegan and touchesMoved functions implemented, will those events still work if I embed the scene in the SCNMaterial? Thanks very much 🙏
Posted
by
Post not yet marked as solved
2 Replies
196 Views
Would it be possible to work with thousands of physical bodies in SpriteKit?
Posted
by