SpriteKit

RSS for tag

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

Posts under SpriteKit tag

200 Posts

Post

Replies

Boosts

Views

Activity

SpriteKit game + inAppPurchases
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
0
0
639
May ’22
In swift for sprite kit, how can I have a functionality that tracks the touches of two nodes and a time? Along with tracking the textures?
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") } } } }
1
0
666
May ’22
SKView showsFields draws only on a portion of the screen
The SKView.showsFields = true only draws on a portion of the screen - nothing shows on the bottom-left. This can be easily tested using Apple's default game playground. I only removed the action for the Hello World label and added a radialGravityField into the scene. Also other fields like electricField do not draw anything on the screen. Tested in Xcode 11.7 and Xcode 12.5.1. Is this a bug in the recent releases? override func didMove(to view: SKView) { // ... let field = SKFieldNode.radialGravityField() addChild(field) } and sceneView.showsFields = true
4
1
1.5k
Apr ’22
How do you add a new SpriteKit scene to a Swift Playgrounds App project
Xcode 13.2 adds the Swift Playgrounds App project template that works with the upcoming version of Swift Playgrounds. The project template creates an iOS SwiftUI app. SwiftUI has a sprite view to show SpriteKit scenes so I tried adding a new SpriteKit scene to the project. Choosing File > New > File adds a Swift file to the project. The New File Assistant doesn't open. I can add an existing SpriteKit scene to the project. I can view and edit the scene. I can also show the scene in a sprite view so I know this project template supports SpriteKit. Adding an existing SpriteKit scene also adds a Resources folder to the project. When I choose to create a new file from the Resources folder, the New File Assistant opens. But SpriteKit Scene is not one of the file types I can add. How do I add a new SpriteKit scene file to a Swift Playgrounds App project?
2
0
1.7k
Apr ’22
Ultra-large texture atlases in SpriteKit
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.
0
0
930
Apr ’22
Tiles disappear from SKTileMapNode when moving it
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?
1
0
681
Apr ’22
How to display sprites in RealityKit?
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?
1
0
1.4k
Apr ’22
SKScene jitters/lags when a new SKSpriteNode is added
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])) } }
2
0
1.5k
Apr ’22
Show Another View On level completion
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.
1
0
952
Apr ’22
How can i fix Thread 1: signal SIGABRT Error?
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)
2
0
1.3k
Apr ’22
SKScene displayed as a SCNMaterial
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 🙏
2
0
805
Apr ’22
Any tutorials for macOS Swift Game Development?
Working with a student who wants to build a game in SpriteKit as a Mac app, and I was wondering if there are any good tutorials out there for this? Secondary, is there a better way to do key controls for a macOS games in Swift? Using the KeyDown function with the key code has been more of an annoying process than I would like. Is there any way of simply doing if keyPressed == "D" {...}?
3
0
3.1k
Apr ’22
Here is a simplistic SpriteKit example. Anybody knows why I can't reset the scene by removing all children with tap of a button from SwiftUI?
// // ContentView.swift // TestSprite1 // // Created by Alireza | Datance on 4/14/22. // import SwiftUI import SpriteKit class GameScene: SKScene {   override func didMove(to view: SKView) {     physicsBody = SKPhysicsBody(edgeLoopFrom: frame)   }   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {      //with this we would only process single touch     // good question how to process multi touch      guard let touch = touches.first else { return }           let location = touch.location(in: self)     let box = SKSpriteNode(color: SKColor.red, size: CGSize(width: 50, height: 50))     box.position = location     box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50))     addChild(box)                 } } struct ContentView: View {   var scene: SKScene {     let scene = GameScene()     scene.size = CGSize(width: 300, height: 400)     scene.scaleMode = .fill     return scene      }       func initScene () {     // why doesn't it work     scene.removeAllActions()     scene.removeAllChildren()         }       var body: some View {     VStack {       Text("Hello, world!")         .padding()       Button("Reset") {         initScene()       }               SpriteView(scene: scene)         .frame(width: 300, height: 400)         .ignoresSafeArea()     }         } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView( )   } }
0
0
643
Apr ’22
SpriteKit UITouch and panGesture conflict
I have a SpriteKit scene using UITouch to drag and drop. That works. I have also an SKCameranode and a pinchGesture so you can pinch to zoom in and out of the scene. That works too. Lastly, I set up a panGesture so that you can pan the scene when it is zoomed in. That also works, but it conflicts with the drag and drop. The drag and drop works when the scene first loads, but if you zoom in on the scene, you can no longer drag and drop because it just tries to pan, and even if you zoom back out to normal size, the drag and drop function remains messed up and will only partially move. I know there are instructions on how to allow simultaneous gestures or how to prefer one gesture over another, but only UIGestureRecognizer is mentioned in Apple's explanation of those. I didn't see anything about UITouch. Is there a way to make touches using UITouch and UIGestureRecognizers not conflict with each other?
0
0
576
Apr ’22
SKAction.moveby and touchesMoved at the same time
I am using xcode 13.3 beta 3 to make a game in spritekit using swift. The game is working pretty good and i am very happy with it so far. There is a problem i am trying to figure out. The game is a block type game where you drag a block into the game area from a holding area and drop it in an open spot. When you first touch and hold the block or touch and move the block, a method moves the block a certain number of points above the touch area Y axis using SKAction.moveby. The block x axis matches the touch area. Then you move the block to an open are and drop it. If you do this very slow, there is no problem. But if you try to do it fast, you can see it do the SKAction.moveby first and complete, then the block quickly moves from where it finished the moveby animation to where you current touch position is it. This makes it feel very jerky and unnatural. Is there a way to make the block follow your fingers position AND animate using moveby at the same time? I feel that the problem is with the skaction.moveby because if you move your finger fast enough, you can be way above where the animation would normally finish and thats why you see the block jump to the touch. I wonder if there is a way to use the update() method to somehow change the blocks position without using moveby or move to. Thank you for any help you can provide me. I can also you show you my touchesBegin, moved and ended methods if that will help.
1
0
1.8k
Mar ’22
SKLabelNode blurry
Anyone know how to fix what seem to be an issue with SKLabelNode where it is very blurry? this started happening after i update xcode.
Replies
1
Boosts
0
Views
1.2k
Activity
May ’22
SpriteKit game + inAppPurchases
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
Replies
0
Boosts
0
Views
639
Activity
May ’22
In swift for sprite kit, how can I have a functionality that tracks the touches of two nodes and a time? Along with tracking the textures?
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") } } } }
Replies
1
Boosts
0
Views
666
Activity
May ’22
SKView showsFields draws only on a portion of the screen
The SKView.showsFields = true only draws on a portion of the screen - nothing shows on the bottom-left. This can be easily tested using Apple's default game playground. I only removed the action for the Hello World label and added a radialGravityField into the scene. Also other fields like electricField do not draw anything on the screen. Tested in Xcode 11.7 and Xcode 12.5.1. Is this a bug in the recent releases? override func didMove(to view: SKView) { // ... let field = SKFieldNode.radialGravityField() addChild(field) } and sceneView.showsFields = true
Replies
4
Boosts
1
Views
1.5k
Activity
Apr ’22
How do you add a new SpriteKit scene to a Swift Playgrounds App project
Xcode 13.2 adds the Swift Playgrounds App project template that works with the upcoming version of Swift Playgrounds. The project template creates an iOS SwiftUI app. SwiftUI has a sprite view to show SpriteKit scenes so I tried adding a new SpriteKit scene to the project. Choosing File > New > File adds a Swift file to the project. The New File Assistant doesn't open. I can add an existing SpriteKit scene to the project. I can view and edit the scene. I can also show the scene in a sprite view so I know this project template supports SpriteKit. Adding an existing SpriteKit scene also adds a Resources folder to the project. When I choose to create a new file from the Resources folder, the New File Assistant opens. But SpriteKit Scene is not one of the file types I can add. How do I add a new SpriteKit scene file to a Swift Playgrounds App project?
Replies
2
Boosts
0
Views
1.7k
Activity
Apr ’22
Ultra-large texture atlases in SpriteKit
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.
Replies
0
Boosts
0
Views
930
Activity
Apr ’22
Displaying HDR content in SpriteKit
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.
Replies
0
Boosts
0
Views
1.4k
Activity
Apr ’22
Tiles disappear from SKTileMapNode when moving it
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?
Replies
1
Boosts
0
Views
681
Activity
Apr ’22
How to display sprites in RealityKit?
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?
Replies
1
Boosts
0
Views
1.4k
Activity
Apr ’22
SKScene jitters/lags when a new SKSpriteNode is added
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])) } }
Replies
2
Boosts
0
Views
1.5k
Activity
Apr ’22
SpriteKit sprite node lags when added to the scene
Hello! I'm using SpriteKit for my application, and when I add an SKSpriteNode into the scene it lags and the FPS drops from 60 to 45. To make sure this isn't a code issue, I tested this in an app instead of a playground and it worked fine. Will I get penalized for poor performance or is there a way to solve this issue?Thank you 🙂
Replies
1
Boosts
0
Views
1.2k
Activity
Apr ’22
Show Another View On level completion
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.
Replies
1
Boosts
0
Views
952
Activity
Apr ’22
How can i fix Thread 1: signal SIGABRT Error?
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)
Replies
2
Boosts
0
Views
1.3k
Activity
Apr ’22
SKScene displayed as a SCNMaterial
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 🙏
Replies
2
Boosts
0
Views
805
Activity
Apr ’22
SpriteKit Performance
Would it be possible to work with thousands of physical bodies in SpriteKit?
Replies
2
Boosts
0
Views
1.2k
Activity
Apr ’22
Is it possible to render a SpriteKit Scene into a SceneKit Plane?
Hi, I just wanted to add a 3D perspective to my SpriteKit Scene, so I wondered if I render the SpriteKit Scene as a material of SCNGeometry is possible. If not, is there another way to give a 3D perspective to a SpriteKit Scene. Thanks very much 🙏
Replies
1
Boosts
0
Views
746
Activity
Apr ’22
Any tutorials for macOS Swift Game Development?
Working with a student who wants to build a game in SpriteKit as a Mac app, and I was wondering if there are any good tutorials out there for this? Secondary, is there a better way to do key controls for a macOS games in Swift? Using the KeyDown function with the key code has been more of an annoying process than I would like. Is there any way of simply doing if keyPressed == "D" {...}?
Replies
3
Boosts
0
Views
3.1k
Activity
Apr ’22
Here is a simplistic SpriteKit example. Anybody knows why I can't reset the scene by removing all children with tap of a button from SwiftUI?
// // ContentView.swift // TestSprite1 // // Created by Alireza | Datance on 4/14/22. // import SwiftUI import SpriteKit class GameScene: SKScene {   override func didMove(to view: SKView) {     physicsBody = SKPhysicsBody(edgeLoopFrom: frame)   }   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {      //with this we would only process single touch     // good question how to process multi touch      guard let touch = touches.first else { return }           let location = touch.location(in: self)     let box = SKSpriteNode(color: SKColor.red, size: CGSize(width: 50, height: 50))     box.position = location     box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50))     addChild(box)                 } } struct ContentView: View {   var scene: SKScene {     let scene = GameScene()     scene.size = CGSize(width: 300, height: 400)     scene.scaleMode = .fill     return scene      }       func initScene () {     // why doesn't it work     scene.removeAllActions()     scene.removeAllChildren()         }       var body: some View {     VStack {       Text("Hello, world!")         .padding()       Button("Reset") {         initScene()       }               SpriteView(scene: scene)         .frame(width: 300, height: 400)         .ignoresSafeArea()     }         } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView( )   } }
Replies
0
Boosts
0
Views
643
Activity
Apr ’22
SpriteKit UITouch and panGesture conflict
I have a SpriteKit scene using UITouch to drag and drop. That works. I have also an SKCameranode and a pinchGesture so you can pinch to zoom in and out of the scene. That works too. Lastly, I set up a panGesture so that you can pan the scene when it is zoomed in. That also works, but it conflicts with the drag and drop. The drag and drop works when the scene first loads, but if you zoom in on the scene, you can no longer drag and drop because it just tries to pan, and even if you zoom back out to normal size, the drag and drop function remains messed up and will only partially move. I know there are instructions on how to allow simultaneous gestures or how to prefer one gesture over another, but only UIGestureRecognizer is mentioned in Apple's explanation of those. I didn't see anything about UITouch. Is there a way to make touches using UITouch and UIGestureRecognizers not conflict with each other?
Replies
0
Boosts
0
Views
576
Activity
Apr ’22
SKAction.moveby and touchesMoved at the same time
I am using xcode 13.3 beta 3 to make a game in spritekit using swift. The game is working pretty good and i am very happy with it so far. There is a problem i am trying to figure out. The game is a block type game where you drag a block into the game area from a holding area and drop it in an open spot. When you first touch and hold the block or touch and move the block, a method moves the block a certain number of points above the touch area Y axis using SKAction.moveby. The block x axis matches the touch area. Then you move the block to an open are and drop it. If you do this very slow, there is no problem. But if you try to do it fast, you can see it do the SKAction.moveby first and complete, then the block quickly moves from where it finished the moveby animation to where you current touch position is it. This makes it feel very jerky and unnatural. Is there a way to make the block follow your fingers position AND animate using moveby at the same time? I feel that the problem is with the skaction.moveby because if you move your finger fast enough, you can be way above where the animation would normally finish and thats why you see the block jump to the touch. I wonder if there is a way to use the update() method to somehow change the blocks position without using moveby or move to. Thank you for any help you can provide me. I can also you show you my touchesBegin, moved and ended methods if that will help.
Replies
1
Boosts
0
Views
1.8k
Activity
Mar ’22