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
5 Replies
2.6k Views
Hi.I am trying to add background with gradient color to my SKScene:class GameScene: SKScene { override func didMove(to view: SKView) { let gradientView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)) let image = UIImage.gradientBackgroundImage(bounds: CGRect(x: 0, y: 0, width: frame.width, height: frame.height), colors: [UIColor.yellow.cgColor, UIColor.blue.cgColor]) let background = SKSpriteNode(color: UIColor(patternImage: image), size: frame.size) addChild(background) } } extension UIImage { /** http://www.riptutorial.com/ios/example/14328/gradient-image-with-colors */ static func gradientBackgroundImage(bounds: CGRect, colors: [CGColor]) -> UIImage { let gradientLayer = CAGradientLayer() gradientLayer.frame = bounds gradientLayer.colors = colors UIGraphicsBeginImageContext(gradientLayer.bounds.size) gradientLayer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } }This is not working. Is there a better way to add gradient background to the SKScene?Thanks a lot!EDIT:If I do this in didMove(to view:):self.view?.backgroundColor = UIColor.blue let backgroundDep = IGABackgroundLayer().gradientMetar() // My method to create CAGradientLayer backgroundDep.frame = self.view!.bounds self.view!.layer.insertSublayer(backgroundDep, at: 0)...background is created nicely but added SKNodes are not seen on top of it.
Posted
by igorland.
Last updated
.
Post not yet marked as solved
0 Replies
88 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 NoCapApps.
Last updated
.
Post marked as solved
1 Replies
218 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 Johnno.
Last updated
.
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 Last updated
.
Post not yet marked as solved
6 Replies
410 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 Last updated
.
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 Last updated
.
Post not yet marked as solved
12 Replies
1.7k Views
I've an app I've been working on for quite some time now. It uses SpriteKit. The windows with the scenes in them are generated programmatically. All of a sudden, when it's up and running through XCode, the console throws out a continuous stream of errors - probably in the order of 1 per second. The main one that makes up this stream is: 2022-03-07 20:07:38.765930+0000 My*App[8461:465673] [] CurrentVBLDelta returned 0 for display 1 -- ignoring unreasonable value But others that appear slightly less frequently are: 2022-03-07 20:07:38.447800+0000 My*App[8461:465143] Metal GPU Frame Capture Enabled 2022-03-07 20:07:38.448070+0000 My*App[8461:465143] Metal API Validation Enabled 2022-03-07 20:07:38.613097+0000 My*App[8461:465640] [] [0x7f9596053820] CVCGDisplayLink::setCurrentDisplay: 1 2022-03-07 20:07:38.613415+0000 My*App[8461:465640] [] [0x7f9596053820] Bad CurrentVBLDelta for display 1 is zero. defaulting to 60Hz. 2022-03-07 20:07:38.613442+0000 My*App[8461:465640] [] [0x7f9596053800] CVDisplayLinkCreateWithCGDisplays count: 1 [displayID[0]: 0x1] [CVCGDisplayLink: 0x7f9596053820] 2022-03-07 20:07:38.613467+0000 My*App[8461:465640] [] [0x7f9596053800] CVDisplayLinkStart 2022-03-07 20:07:38.613487+0000 My*App[8461:465640] [] [0x7f9596053820] CVDisplayLink::start 2022-03-07 20:07:38.613541+0000 My*App[8461:465640] [] [0x7f9596053800] CVDisplayLinkStart 2022-03-07 20:07:38.613575+0000 My*App[8461:465640] [] [0x7f9596053820] CVDisplayLink::start 2022-03-07 20:07:38.613634+0000 My*App[8461:465671] [] [0x600000c09f10] CVXTime::reset 2022-03-07 20:07:38.613718+0000 My*App[8461:465671] [] CurrentVBLDelta returned 0 for display 1 -- ignoring unreasonable value 2022-03-07 20:07:38.639810+0000 My*App[8461:465671] [] CurrentVBLDelta returned 0 for display 1 -- ignoring unreasonable value 2022-03-07 20:07:38.639887+0000 My*App[8461:465671] [] [0x7f9596053820] CVCGDisplayLink::getDisplayTimes display: 1 -- SLSDisplayGetCurrentVBLDelta returned 0 => generating fake times 2022-03-07 20:07:38.673702+0000 My*App[8461:465653] [] [0x7f9596053820] CVCGDisplayLink::setCurrentDisplay: 1 2022-03-07 20:07:38.673748+0000 My*App[8461:465653] [] [0x7f9596053820] CVDisplayLink::stop 2022-03-07 20:07:38.673862+0000 My*App[8461:465653] [] [0x7f9596053820] Bad CurrentVBLDelta for display 1 is zero. defaulting to 60Hz. 2022-03-07 20:07:38.673883+0000 My*App[8461:465653] [] [0x7f9596053820] CVDisplayLink::start I really don't know how else to describe this. I can only imagine having to hand over the entire project as lately I've only been working on the storyboard file so how would this affect it? To be honest the only change I can see is my upgrade to OSX 12.2.1. It's definitely something to do with SpriteKit since not initialising a window with a scene in it stops the errors from showing.
Posted
by todd_d.
Last updated
.
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.
Last updated
.
Post not yet marked as solved
10 Replies
1.5k Views
I wanna add a score when the character contacts with the 'coins' and game over when it contacts with the 'enemies'. But it doesn't work. I compiled the ...Category and &lt;SKPhysicsContactDelegate&gt; code in GameScene.h and these:- (instancetype)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { self.character = [SKSpriteNode spriteNodeWithImageNamed:@"character"]; self.character.position = CGPointMake(100, 100); [self addChild:self.character]; self.physicsWorld.gravity = CGVectorMake(0,0); self.physicsWorld.contactDelegate = self; } return self; }in .m above didMoveToView method. Can I use initWithSize in Xcode 8? And I do use these code in .m:character.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:character.size]; //1 character.physicsBody.dynamic = YES; //2 character.physicsBody.categoryBitMask = characterCategory; //3 character.physicsBody.contactTestBitMask = coinCategory | enemiesCategory;//4 character.physicsBody.collisionBitMask = 0; // 5I use them in "setPhysicsBody:(SKPhysicsBody *)physicsBody" method. And of couse I compiled the didBeginContact method. Did I miss anything? Please help me. Thank you very much.
Posted Last updated
.
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 jadrima.
Last updated
.
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 jsclev.
Last updated
.
Post not yet marked as solved
1 Replies
438 Views
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.
Posted
by Vykes.
Last updated
.
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 uquiz.
Last updated
.
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 Last updated
.
Post not yet marked as solved
3 Replies
483 Views
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
Posted
by calin.
Last updated
.
Post not yet marked as solved
0 Replies
484 Views
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?
Posted
by szymczyk.
Last updated
.
Post not yet marked as solved
0 Replies
168 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.
Last updated
.
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.
Last updated
.
Post not yet marked as solved
0 Replies
146 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 madsvmunk.
Last updated
.