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

How to animate SKShapeNode's lineLength
I would like to animate the SKShapeNode’s lineLength property.Quote from the documentation:By interpolating this value over time (for example, in your scene's update(_:) callback), you can animate the shape as if it were drawn in real time.This is exactly what i would like to do but how can i change the value?The lineLength is a get property.
2
1
1.4k
Jan ’23
Sprites with same shader but different uniform values
I have been trying to find information on this, but I just can't find anything. The SKShader and SKUniform reference documents do not provide this information, and neither does the SpriteKit Programming Guide document (and in fact, it seems that shaders are not mentioned there at all, even though the document is linked to from the SKShader and SKUniform reference documents). Couldn't find this by googling either.The SKShader documentation recommends sharing shaders between sprites as much as possible. In other words, if two sprites use the same shader, it's better to share the same SKShader object between them rather than create a new shader for each one.But what if two such sprites use the same shader, but different uniform values? For example, there may be a uniform named "factor" used in the shader, and one sprite wants to use a value of 0.25 for it, while another wants to use a value of 0.4. (As far as I understand, this is something that can be done with one single shader, and it should be completely efficient. Nothing changes when rendering the sprites, except the value of that uniform, which should be possible and efficient.)I can't find any info on what the kosher way of doing this in SpriteKit using SKShader.The 'shader' property of SKSpriteNode retains the object. Which means that the one and same SKShader object will be shared between two sprites if I simply assign one to both. Obviously this won't work if I want them to use a different value for a uniform.I notice that SKShader is copyable. Should I assign a copy of the original SKShader object to each sprite? Will this work? Will it be efficient? Can then each copy be assigned different uniform values and then they will work properly? Or do I need to replace the SKUniform objects in each copy with new objects?What is the correct way of doing this?
12
1
3.4k
Jan ’23
how do I set a window size of a mac xcode app (spritekit)
how do I set the size of a multi-platform app in xcode (spritekit) ? I know that you can't change the window size on iphone but this app is multi-platform and can also run on macos. So how do I set the window size and make it unable to be stretched but able to full-screen? (on macos) and I was also wandering is there was a way to select the window's starting position. Thanks! P.S. sorry for asking this stupid question just I'm a beginner so I don't know. I had tried searching for an answer but I did not find one.
0
0
763
Jan ’23
SKEmitterNode disable antialiasing of a particle nodes
Hi there! I am using SpriteKit for most of my game development, and while I am usually able to achieve what I need using the framework, I find my self a bit stuck with the usage of SKEmitterNode. I am developing a pixel-art style game, which among others drills down to the fact that whatever is rendered to the scene, should not be anti-aliased. This because that will remove the pixelated effect. For textures, one can use the filteringMode property and set it to .nearest to achieve this. However, with an SKEmitterNode, setting this property of the emitted particles using particleTexture has no effect. As a result, all emitted particles are antialiased, which breaks the pixel art look of the game. Looking at the documentation, I guess particles are rendered in a different way compared to normal SKSpriteNode, hence this property has no effect. Specifically this sentence A particle’s texture is always stretched uniformly. draws me to that conclusion. I have been trying to workaround this, but I find no working solution to the problem. Rolling my own emitter system will be a daunting task, that I prefer not to engage into. I hope someone here might have a suggestion how to work around this. If not, I would be interested to learn how I could request this as a feature to Apple (as I dont think this is necessarily a bug). Hope you can help, Sander
0
1
779
Jan ’23
SpriteKit SKView no longer transitions between scenes in iOS 9
We have a code base that was written and released in 2013, but in iOS 9 the app no longer transitions between SKScenes when the presentScene:transition: message is sent to our SKView.Here's what we tried:Disabling Metal via the Info.plistUsing [SKTransition transitionWithCIFilter:duration:] instead of the predefined animationsTweaking zPositionSwitching to regular UIView animations (this made our SKNodes disappear)None of the above attempts fixed the issue. Note that everything transitions properly in iOS 8Any suggestions on how to locate/fix/workaround the bug?
10
0
3k
Jan ’23
SPRITE KIT: move in a x and y region on a background street view along with jumping and creating a shadow
I am trying to make player move up and done on a region and jump but I am only able to create the bounded rectangle and make player affected by gravity so they can move along the x axis. I have a street background with houses and want them to be able to move along the width and height of the street and jump but when I make player affected by gravity there is no way to keep it on the street part of the background. do I use SKregion? and how do I implement if I do and create the shadow? Thanks for all feedback
0
0
963
Jan ’23
What is the best way to create GKPolygonObstacle from Tile Map?
I am working on an iOS game where the character can move in a 2D continuous space with obstacles. I want to implement path finding via GameplayKit. My understanding is that I need to create a GKObstacleGraph object. First I need a GKPolygonObstacle, but I am unsure how. I want to create tile maps for the levels. My questions is, what is the best way to create the GKPolygonObstacle from a tile map. I know that I can create a navigation path (example in the image), is this a step in the right direction? Or do I need to create physics bodies for the obstacles?
0
1
1.4k
Jan ’23
Is it possible to animate a path similar to how StrokeEnd works in SpriteKit ?
I've been googling this for a while now and trying out various things from StackOverflow etc. I want to draw paths, then animate a line that 'grows' over that path. I'm coming to the conclusion it is NOT possible to animate a shape or bezierpath in an SKScene. Using the CABasicAnimation stuff would work, but I can't figure out how to add layers or views... Using SKActions isn't really what I want to do... i can only scale etc. Not really grow as i need.It feels like there is a massive gap in what SpriteKit can do here. Has anyone found this? Anyone found a way around it ? I'm really stuck here.. The whole concept of my game idea relys on this feature and I can't figure out a way to even start !!! 😟appreciate any ideas or pointers. thanks
8
0
4.5k
Jan ’23
Bringing visual elements from imperative to declarative paradigm
UIViews can be wrapped and used in SwiftUI. But guess what other visual elements are there that can't be wrapped? Sprites! Do you thing wrapping SKNode in similar way even make sense, also with performance considerations? For a start it could bring back physics introduced in UIKit and lacking in SwiftUI, with even more options... Have you ever thought about it, and what were your thoughts?
0
0
881
Dec ’22
Wouldn't it be beneficial to have identifiers for SKScenes, like we have for UITableViewCells?
With SKView having presentScene(withIdentifier identifier: String) -> SKScene? we won't be forced to reinstantiate the same scenes for every SKView's presentScene(_:), or else to keep a collection of strong references outside. And scene presentation could be optimised by the framework. Is it now in any way? Let's imagine a SpriteKit scene with hundreds of SKNodes, recreated all over again. Or better, let's imagine dealing with UITableViewCells for UITableViews manually, if there was no auto-recycling mechanism. Can you see any resemblance, what is your opinion about it?
0
0
837
Dec ’22
Image quality issues when deploying app
Hi, I have my app on the App Store for about 3 month. Today, I created an update and started to test via TestFlight. However, the image quality of the PNG files are really bad (though haven't changed anything!) There are items in the image that shouldn't be, ... - to me, it looks like the quality of the images were downsampled or whatever. I've included screenshots on how it looks like. On my Mac, the images are correct, also on the device emulator. It only appears when distributing the app ... might there be something wrong with the created archives? Anyone else experienced something like that, how can I fix this? Thanks for your help, Mario 1st Image: how it looks like on the simulator. 2nd Image: how it looks on the iPhone. please note how the buttons appear
2
0
1.4k
Oct ’22
Issues with UIFocusItemContainer since tvOS 16
I implemented a game for Apple TV where I have multiple SKSpriteNodes in a SKScene. Before tvOS 16 everything worked correctly, I could move around with the remote and focus all items in the correct way. Now, with tvOS 16, the app crashes with the following error: Focus item RandomSKSpriteNode has a parent focus environment of MySKScene but this environment does not provide a container for focus items. So then I tried to just set the SKScene's view, the parent environments focus item container, an empty view and more as the SKScene's focusItemContainer - this fixed the crash, but the focus behaves quite randomly. Some of the nodes cannot be focused then anymore, and the focus just jumps to random nodes, and not the one in which direction I swiped to. override var focusItemContainer: UIFocusItemContainer? { return view // parentFocusEnvironment?.focusItemContainer // UIView() } Does anyone have an idea what I have to set here as the focusItemContainer? It's so strange because everything worked before and I also cannot find anything about any breaking changes in the release notes. If you have any hints for that issue please let me know!
1
1
1.3k
Oct ’22
CIMotionBlur broken in iOS16 ?
The following code just does not behaves the same way previous to iOS 16 and with iOS 16. The blur effect does not seem to work correctly in iOS 16. class GameScene: SKScene { override func didMove(to view: SKView) { let shapeNode = SKShapeNode(circleOfRadius: 30) shapeNode.fillColor = .green shapeNode.strokeColor = .clear addChild(shapeNode) let blurredShapeNode = SKShapeNode(circleOfRadius: 30) blurredShapeNode.fillColor = .red blurredShapeNode.strokeColor = .clear let effectNode = SKEffectNode() addChild(effectNode) effectNode.addChild(blurredShapeNode) let blurAngle = NSNumber(value: 0) effectNode.filter = CIFilter( name: "CIMotionBlur", parameters: [kCIInputRadiusKey: 30, kCIInputAngleKey: blurAngle]) } }
1
0
1.2k
Oct ’22
SpriteKit - How do I learn to use this?
I'm new to iOS Development (Coming from being a web dev who made games via JavaScript). I was wondering how does one properly read and understand the documentation for SpriteKit? I do know how to code in Swift, but particularly it is very hard for me to read and understand how to use these Apple Developer Documentations. If there is anyone out there that knows some useful tutorials or tips on how to read and understand the documentation to a point of understanding how and where I can write my code, I would be very appreciated for the help.
4
0
2.8k
Oct ’22
Concurrent Processing of SKSpriteNode Copied Data in SpriteKit
I posted this question on stack overflow the other day but haven't received any replies as yet. I want to be able to access SpriteKit node characteristics on a background thread. I only require a copy of these characteristics but don't know how to do this. The full question was: I’m working on my first Swift project. It uses SpriteKit and has quite a few vehicles moving around different tracks. Each vehicle must be aware of other vehicles to avoid collisions and for overtaking etc. The processing for this is very time consuming so I want to run most of the calculations concurrently on a number of background threads. To do this, I copied the vehicles defined in the arrays track1Vehicles and track2Vehicles (arrays of SKSpriteNodes) to t1Vehicle and t2Vehicle. The idea was to perform all the calculations on these, then afterwards transfer the new positions etc back to the original arrays using SKActions on the main thread. The problem I’m having is that any reference to t1Vehicle and t2Vehicle parameters gives the error “Main actor-isolated property 'goalSpeed' can not be mutated from a non-isolated context” (goalSpeed being one of those parameters). Is there a way to allow t1Vehicle and t2Vehicle on a background thread, perhaps by disabling their physicsBodies or similar? t1Vehicle & t2Vehicle are not intended to themselves alter the display. They are intended only as a means of accessing the data as it was when the ‘findObstacles’ function was called. If there’s another way to reference this data, that would also solve the problem. The relevant parts of the code are shown below: //this code is on the main thread Task { let result = await findObstacles() let t1Vehicle = result.t1Vehicle let t2Vehicle = result.t2Vehicle } //Function called from the Task above func findObstacles() async -> (t1Vehicle: Vehicle, t2Vehicle: Vehicle) { //Create copy of vehicles for calculating proximity to other vehicles //Do calculations on another thread var t1Vehicle = track1Vehicles.dropFirst() //Straight Track Vehicles: Ignore element [0] t1Vehicle.sort(by: {$0.position.y < $1.position.y}) //Sort into positional order, 000 - 999.999 var t2Vehicle = track2Vehicles.dropFirst() t2Vehicle.sort(by: {$0.position.y < $1.position.y}) //Sort into positional order, 000 - 999.999 //processing of data here.. return (t1Vehicle, t2Vehicle) } ps. the 'Vehicle' type is an ObservableObject. A number of its parameters were also Published variables. I tried removing the '@Published' from the parameter 'goalSpeed' and it made no difference - the error still occurred on it. I'm getting the impression that nodes can only be accessed from the main thread. With my code I am copying 2 arrays of nodes and use those copies in the background thread/s. The original arrays are not altered. They could even be copied prior to going into the background thread. I need access to things such as position and velocity for each node and there can potentially be 300 of them. A fast & simple way to copy these parameters from all nodes into a normal array would do. Currently my copy still contains nodes even though I don't display them and only require read access.
6
0
1.1k
Oct ’22
SpriteKit shader affecting area outside effect node's accumulated frame
As of iOS 16, I am seeing strange behaviour in SKEffectNode where the attached shader appears to be affecting an area that is outside the accumulated frame. This happens when the frame of the effect node's subview is changed. I've prepared the following playground example which allows for reproduction of what I believe to be a bug. It consists of an SKScene, with an SKEffectNode added to it and an SKLabel added to the effect node. The effect node has a simple SKShader attached which produces a gradient effect across the frame of the SKLabel. A tap down on the scene will iterate the text of the SKLabel though 3 strings of different lengths. The issue presents itself by the fourth tap, as the gradient will then appear outside of the SKLabel frame. The accumulated frame of the SKEffectView at this point will also not be the same area that is affected by the shader. import PlaygroundSupport import SpriteKit class GameScene: SKScene {   private var i = 0   private let strings = ["Test", "Testing", "Test Test Test"]   private let effectNode = SKEffectNode()   private let label = SKLabelNode()   override func didMove(to view: SKView) {     backgroundColor = .white     anchorPoint = CGPoint(x: 0.5, y: 0.5)     addChild(effectNode)     effectNode.position = .zero     effectNode.shouldEnableEffects = true     effectNode.shader = SKShader(source: """     void main() {       vec4 texel = texture2D(u_texture, v_tex_coord);       vec4 color = vec4(1.0-v_tex_coord.x, 0.0, 0.0, 1.0);       gl_FragColor = mix(texel, color, 0.5);     }     """)     effectNode.addChild(label)     label.verticalAlignmentMode = .center     label.position = .zero     label.fontName = "Helvetica"     label.fontSize = 40     label.fontColor = .blue     label.text = strings.first   }   override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {     i += 1     if i >= strings.count {       i = 0     }     label.text = strings[i]   } } let sceneView = SKView(frame: CGRect(origin: .zero, size: CGSize(width: 500, height: 500))) let scene = GameScene(size: sceneView.frame.size) scene.scaleMode = .aspectFill sceneView.presentScene(scene) PlaygroundSupport.PlaygroundPage.current.liveView = sceneView This unexpected behaviour is not present on iOS 15, but is currently occurring on iOS 16.0, 16.0.2 and 16.1 beta 3.
0
1
755
Sep ’22
Inverted y axis on SKShader using SpriteKit
Hello there 👋 While running the following ViewController.swift on iOS 16, I realized that the y axis of v_tex_coord inside the SKShader has been inverted. The same code display a red bar at the top of the screen for iOS 15.5. This red bar is displayed at the bottom of the screen for iOS 16. Here is some images to show you the difference: iOS 15 iOS 16 Can you help me with this please? Thanks a lot 🙏 import UIKit import SpriteKit class ViewController: UIViewController {     var skView: SKView?     var scene: SKScene?     override func viewDidLoad() {         super.viewDidLoad()         // Do any additional setup after loading the view.         skView = SKView(frame: view.frame)         scene = SKScene(size: skView?.bounds.size ?? .zero)         scene?.backgroundColor = UIColor.red         view.addSubview(skView!)         skView!.presentScene(scene)         // Media         let mediaNode = SKSpriteNode(imageNamed: "archi1")         mediaNode.size = skView?.bounds.size ?? .zero         mediaNode.position = skView?.center ?? .zero         // Effect         let effectNode = SKEffectNode()         effectNode.shader = SKShader(source:         """             void main() {                 vec2 uv = v_tex_coord;                 vec4 texel = texture2D(u_texture, uv);                 vec4 color = texel;                 vec4 outColor = texel;                 // from top to bottom                 if (uv.y > .8 / 2. + .5) { // bg color                     color = i_color;                 }                 outColor = mix(texel, color, .6);                 gl_FragColor = outColor;             }         """         , uniforms: [             SKUniform(name: "i_color", vectorFloat4: vector_float4(1, 0, 0, 0))         ])         scene?.addChild(effectNode)         effectNode.addChild(mediaNode)     } }
3
8
1.5k
Sep ’22
How to animate SKShapeNode's lineLength
I would like to animate the SKShapeNode’s lineLength property.Quote from the documentation:By interpolating this value over time (for example, in your scene's update(_:) callback), you can animate the shape as if it were drawn in real time.This is exactly what i would like to do but how can i change the value?The lineLength is a get property.
Replies
2
Boosts
1
Views
1.4k
Activity
Jan ’23
Sprites with same shader but different uniform values
I have been trying to find information on this, but I just can't find anything. The SKShader and SKUniform reference documents do not provide this information, and neither does the SpriteKit Programming Guide document (and in fact, it seems that shaders are not mentioned there at all, even though the document is linked to from the SKShader and SKUniform reference documents). Couldn't find this by googling either.The SKShader documentation recommends sharing shaders between sprites as much as possible. In other words, if two sprites use the same shader, it's better to share the same SKShader object between them rather than create a new shader for each one.But what if two such sprites use the same shader, but different uniform values? For example, there may be a uniform named "factor" used in the shader, and one sprite wants to use a value of 0.25 for it, while another wants to use a value of 0.4. (As far as I understand, this is something that can be done with one single shader, and it should be completely efficient. Nothing changes when rendering the sprites, except the value of that uniform, which should be possible and efficient.)I can't find any info on what the kosher way of doing this in SpriteKit using SKShader.The 'shader' property of SKSpriteNode retains the object. Which means that the one and same SKShader object will be shared between two sprites if I simply assign one to both. Obviously this won't work if I want them to use a different value for a uniform.I notice that SKShader is copyable. Should I assign a copy of the original SKShader object to each sprite? Will this work? Will it be efficient? Can then each copy be assigned different uniform values and then they will work properly? Or do I need to replace the SKUniform objects in each copy with new objects?What is the correct way of doing this?
Replies
12
Boosts
1
Views
3.4k
Activity
Jan ’23
Changing cursor in SpriteKit
I've found only some old posts about this topic, that can't be implemented nowadays. Can someone help me to find a way to change your mouse cursor to a custom cursor in SpriteKit?
Replies
0
Boosts
1
Views
756
Activity
Jan ’23
how do I set a window size of a mac xcode app (spritekit)
how do I set the size of a multi-platform app in xcode (spritekit) ? I know that you can't change the window size on iphone but this app is multi-platform and can also run on macos. So how do I set the window size and make it unable to be stretched but able to full-screen? (on macos) and I was also wandering is there was a way to select the window's starting position. Thanks! P.S. sorry for asking this stupid question just I'm a beginner so I don't know. I had tried searching for an answer but I did not find one.
Replies
0
Boosts
0
Views
763
Activity
Jan ’23
SKEmitterNode disable antialiasing of a particle nodes
Hi there! I am using SpriteKit for most of my game development, and while I am usually able to achieve what I need using the framework, I find my self a bit stuck with the usage of SKEmitterNode. I am developing a pixel-art style game, which among others drills down to the fact that whatever is rendered to the scene, should not be anti-aliased. This because that will remove the pixelated effect. For textures, one can use the filteringMode property and set it to .nearest to achieve this. However, with an SKEmitterNode, setting this property of the emitted particles using particleTexture has no effect. As a result, all emitted particles are antialiased, which breaks the pixel art look of the game. Looking at the documentation, I guess particles are rendered in a different way compared to normal SKSpriteNode, hence this property has no effect. Specifically this sentence A particle’s texture is always stretched uniformly. draws me to that conclusion. I have been trying to workaround this, but I find no working solution to the problem. Rolling my own emitter system will be a daunting task, that I prefer not to engage into. I hope someone here might have a suggestion how to work around this. If not, I would be interested to learn how I could request this as a feature to Apple (as I dont think this is necessarily a bug). Hope you can help, Sander
Replies
0
Boosts
1
Views
779
Activity
Jan ’23
SpriteKit SKView no longer transitions between scenes in iOS 9
We have a code base that was written and released in 2013, but in iOS 9 the app no longer transitions between SKScenes when the presentScene:transition: message is sent to our SKView.Here's what we tried:Disabling Metal via the Info.plistUsing [SKTransition transitionWithCIFilter:duration:] instead of the predefined animationsTweaking zPositionSwitching to regular UIView animations (this made our SKNodes disappear)None of the above attempts fixed the issue. Note that everything transitions properly in iOS 8Any suggestions on how to locate/fix/workaround the bug?
Replies
10
Boosts
0
Views
3k
Activity
Jan ’23
SPRITE KIT: move in a x and y region on a background street view along with jumping and creating a shadow
I am trying to make player move up and done on a region and jump but I am only able to create the bounded rectangle and make player affected by gravity so they can move along the x axis. I have a street background with houses and want them to be able to move along the width and height of the street and jump but when I make player affected by gravity there is no way to keep it on the street part of the background. do I use SKregion? and how do I implement if I do and create the shadow? Thanks for all feedback
Replies
0
Boosts
0
Views
963
Activity
Jan ’23
What is the best way to create GKPolygonObstacle from Tile Map?
I am working on an iOS game where the character can move in a 2D continuous space with obstacles. I want to implement path finding via GameplayKit. My understanding is that I need to create a GKObstacleGraph object. First I need a GKPolygonObstacle, but I am unsure how. I want to create tile maps for the levels. My questions is, what is the best way to create the GKPolygonObstacle from a tile map. I know that I can create a navigation path (example in the image), is this a step in the right direction? Or do I need to create physics bodies for the obstacles?
Replies
0
Boosts
1
Views
1.4k
Activity
Jan ’23
Is it possible to animate a path similar to how StrokeEnd works in SpriteKit ?
I've been googling this for a while now and trying out various things from StackOverflow etc. I want to draw paths, then animate a line that 'grows' over that path. I'm coming to the conclusion it is NOT possible to animate a shape or bezierpath in an SKScene. Using the CABasicAnimation stuff would work, but I can't figure out how to add layers or views... Using SKActions isn't really what I want to do... i can only scale etc. Not really grow as i need.It feels like there is a massive gap in what SpriteKit can do here. Has anyone found this? Anyone found a way around it ? I'm really stuck here.. The whole concept of my game idea relys on this feature and I can't figure out a way to even start !!! 😟appreciate any ideas or pointers. thanks
Replies
8
Boosts
0
Views
4.5k
Activity
Jan ’23
Bringing visual elements from imperative to declarative paradigm
UIViews can be wrapped and used in SwiftUI. But guess what other visual elements are there that can't be wrapped? Sprites! Do you thing wrapping SKNode in similar way even make sense, also with performance considerations? For a start it could bring back physics introduced in UIKit and lacking in SwiftUI, with even more options... Have you ever thought about it, and what were your thoughts?
Replies
0
Boosts
0
Views
881
Activity
Dec ’22
Wouldn't it be beneficial to have identifiers for SKScenes, like we have for UITableViewCells?
With SKView having presentScene(withIdentifier identifier: String) -> SKScene? we won't be forced to reinstantiate the same scenes for every SKView's presentScene(_:), or else to keep a collection of strong references outside. And scene presentation could be optimised by the framework. Is it now in any way? Let's imagine a SpriteKit scene with hundreds of SKNodes, recreated all over again. Or better, let's imagine dealing with UITableViewCells for UITableViews manually, if there was no auto-recycling mechanism. Can you see any resemblance, what is your opinion about it?
Replies
0
Boosts
0
Views
837
Activity
Dec ’22
Image quality issues when deploying app
Hi, I have my app on the App Store for about 3 month. Today, I created an update and started to test via TestFlight. However, the image quality of the PNG files are really bad (though haven't changed anything!) There are items in the image that shouldn't be, ... - to me, it looks like the quality of the images were downsampled or whatever. I've included screenshots on how it looks like. On my Mac, the images are correct, also on the device emulator. It only appears when distributing the app ... might there be something wrong with the created archives? Anyone else experienced something like that, how can I fix this? Thanks for your help, Mario 1st Image: how it looks like on the simulator. 2nd Image: how it looks on the iPhone. please note how the buttons appear
Replies
2
Boosts
0
Views
1.4k
Activity
Oct ’22
Issues with UIFocusItemContainer since tvOS 16
I implemented a game for Apple TV where I have multiple SKSpriteNodes in a SKScene. Before tvOS 16 everything worked correctly, I could move around with the remote and focus all items in the correct way. Now, with tvOS 16, the app crashes with the following error: Focus item RandomSKSpriteNode has a parent focus environment of MySKScene but this environment does not provide a container for focus items. So then I tried to just set the SKScene's view, the parent environments focus item container, an empty view and more as the SKScene's focusItemContainer - this fixed the crash, but the focus behaves quite randomly. Some of the nodes cannot be focused then anymore, and the focus just jumps to random nodes, and not the one in which direction I swiped to. override var focusItemContainer: UIFocusItemContainer? { return view // parentFocusEnvironment?.focusItemContainer // UIView() } Does anyone have an idea what I have to set here as the focusItemContainer? It's so strange because everything worked before and I also cannot find anything about any breaking changes in the release notes. If you have any hints for that issue please let me know!
Replies
1
Boosts
1
Views
1.3k
Activity
Oct ’22
CIMotionBlur broken in iOS16 ?
The following code just does not behaves the same way previous to iOS 16 and with iOS 16. The blur effect does not seem to work correctly in iOS 16. class GameScene: SKScene { override func didMove(to view: SKView) { let shapeNode = SKShapeNode(circleOfRadius: 30) shapeNode.fillColor = .green shapeNode.strokeColor = .clear addChild(shapeNode) let blurredShapeNode = SKShapeNode(circleOfRadius: 30) blurredShapeNode.fillColor = .red blurredShapeNode.strokeColor = .clear let effectNode = SKEffectNode() addChild(effectNode) effectNode.addChild(blurredShapeNode) let blurAngle = NSNumber(value: 0) effectNode.filter = CIFilter( name: "CIMotionBlur", parameters: [kCIInputRadiusKey: 30, kCIInputAngleKey: blurAngle]) } }
Replies
1
Boosts
0
Views
1.2k
Activity
Oct ’22
Is there a way to save SpriteKit scenes as text files?
AFAIK SpriteKit scene files are basically binary plist files. I've managed to convert them to XML with plutil but Xcode crashes every time I try to open a converted file. Is there a way to make this work? It would help a lot for versioning and team collaboration obviously.
Replies
0
Boosts
0
Views
807
Activity
Oct ’22
Is scenekit powerful ?
Is scenekit powerful ? Or can i create a game with scenekit that has the same quality as say a game made in unity or it is just too weak ??
Replies
2
Boosts
0
Views
2.6k
Activity
Oct ’22
SpriteKit - How do I learn to use this?
I'm new to iOS Development (Coming from being a web dev who made games via JavaScript). I was wondering how does one properly read and understand the documentation for SpriteKit? I do know how to code in Swift, but particularly it is very hard for me to read and understand how to use these Apple Developer Documentations. If there is anyone out there that knows some useful tutorials or tips on how to read and understand the documentation to a point of understanding how and where I can write my code, I would be very appreciated for the help.
Replies
4
Boosts
0
Views
2.8k
Activity
Oct ’22
Concurrent Processing of SKSpriteNode Copied Data in SpriteKit
I posted this question on stack overflow the other day but haven't received any replies as yet. I want to be able to access SpriteKit node characteristics on a background thread. I only require a copy of these characteristics but don't know how to do this. The full question was: I’m working on my first Swift project. It uses SpriteKit and has quite a few vehicles moving around different tracks. Each vehicle must be aware of other vehicles to avoid collisions and for overtaking etc. The processing for this is very time consuming so I want to run most of the calculations concurrently on a number of background threads. To do this, I copied the vehicles defined in the arrays track1Vehicles and track2Vehicles (arrays of SKSpriteNodes) to t1Vehicle and t2Vehicle. The idea was to perform all the calculations on these, then afterwards transfer the new positions etc back to the original arrays using SKActions on the main thread. The problem I’m having is that any reference to t1Vehicle and t2Vehicle parameters gives the error “Main actor-isolated property 'goalSpeed' can not be mutated from a non-isolated context” (goalSpeed being one of those parameters). Is there a way to allow t1Vehicle and t2Vehicle on a background thread, perhaps by disabling their physicsBodies or similar? t1Vehicle & t2Vehicle are not intended to themselves alter the display. They are intended only as a means of accessing the data as it was when the ‘findObstacles’ function was called. If there’s another way to reference this data, that would also solve the problem. The relevant parts of the code are shown below: //this code is on the main thread Task { let result = await findObstacles() let t1Vehicle = result.t1Vehicle let t2Vehicle = result.t2Vehicle } //Function called from the Task above func findObstacles() async -> (t1Vehicle: Vehicle, t2Vehicle: Vehicle) { //Create copy of vehicles for calculating proximity to other vehicles //Do calculations on another thread var t1Vehicle = track1Vehicles.dropFirst() //Straight Track Vehicles: Ignore element [0] t1Vehicle.sort(by: {$0.position.y < $1.position.y}) //Sort into positional order, 000 - 999.999 var t2Vehicle = track2Vehicles.dropFirst() t2Vehicle.sort(by: {$0.position.y < $1.position.y}) //Sort into positional order, 000 - 999.999 //processing of data here.. return (t1Vehicle, t2Vehicle) } ps. the 'Vehicle' type is an ObservableObject. A number of its parameters were also Published variables. I tried removing the '@Published' from the parameter 'goalSpeed' and it made no difference - the error still occurred on it. I'm getting the impression that nodes can only be accessed from the main thread. With my code I am copying 2 arrays of nodes and use those copies in the background thread/s. The original arrays are not altered. They could even be copied prior to going into the background thread. I need access to things such as position and velocity for each node and there can potentially be 300 of them. A fast & simple way to copy these parameters from all nodes into a normal array would do. Currently my copy still contains nodes even though I don't display them and only require read access.
Replies
6
Boosts
0
Views
1.1k
Activity
Oct ’22
SpriteKit shader affecting area outside effect node's accumulated frame
As of iOS 16, I am seeing strange behaviour in SKEffectNode where the attached shader appears to be affecting an area that is outside the accumulated frame. This happens when the frame of the effect node's subview is changed. I've prepared the following playground example which allows for reproduction of what I believe to be a bug. It consists of an SKScene, with an SKEffectNode added to it and an SKLabel added to the effect node. The effect node has a simple SKShader attached which produces a gradient effect across the frame of the SKLabel. A tap down on the scene will iterate the text of the SKLabel though 3 strings of different lengths. The issue presents itself by the fourth tap, as the gradient will then appear outside of the SKLabel frame. The accumulated frame of the SKEffectView at this point will also not be the same area that is affected by the shader. import PlaygroundSupport import SpriteKit class GameScene: SKScene {   private var i = 0   private let strings = ["Test", "Testing", "Test Test Test"]   private let effectNode = SKEffectNode()   private let label = SKLabelNode()   override func didMove(to view: SKView) {     backgroundColor = .white     anchorPoint = CGPoint(x: 0.5, y: 0.5)     addChild(effectNode)     effectNode.position = .zero     effectNode.shouldEnableEffects = true     effectNode.shader = SKShader(source: """     void main() {       vec4 texel = texture2D(u_texture, v_tex_coord);       vec4 color = vec4(1.0-v_tex_coord.x, 0.0, 0.0, 1.0);       gl_FragColor = mix(texel, color, 0.5);     }     """)     effectNode.addChild(label)     label.verticalAlignmentMode = .center     label.position = .zero     label.fontName = "Helvetica"     label.fontSize = 40     label.fontColor = .blue     label.text = strings.first   }   override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {     i += 1     if i >= strings.count {       i = 0     }     label.text = strings[i]   } } let sceneView = SKView(frame: CGRect(origin: .zero, size: CGSize(width: 500, height: 500))) let scene = GameScene(size: sceneView.frame.size) scene.scaleMode = .aspectFill sceneView.presentScene(scene) PlaygroundSupport.PlaygroundPage.current.liveView = sceneView This unexpected behaviour is not present on iOS 15, but is currently occurring on iOS 16.0, 16.0.2 and 16.1 beta 3.
Replies
0
Boosts
1
Views
755
Activity
Sep ’22
Inverted y axis on SKShader using SpriteKit
Hello there 👋 While running the following ViewController.swift on iOS 16, I realized that the y axis of v_tex_coord inside the SKShader has been inverted. The same code display a red bar at the top of the screen for iOS 15.5. This red bar is displayed at the bottom of the screen for iOS 16. Here is some images to show you the difference: iOS 15 iOS 16 Can you help me with this please? Thanks a lot 🙏 import UIKit import SpriteKit class ViewController: UIViewController {     var skView: SKView?     var scene: SKScene?     override func viewDidLoad() {         super.viewDidLoad()         // Do any additional setup after loading the view.         skView = SKView(frame: view.frame)         scene = SKScene(size: skView?.bounds.size ?? .zero)         scene?.backgroundColor = UIColor.red         view.addSubview(skView!)         skView!.presentScene(scene)         // Media         let mediaNode = SKSpriteNode(imageNamed: "archi1")         mediaNode.size = skView?.bounds.size ?? .zero         mediaNode.position = skView?.center ?? .zero         // Effect         let effectNode = SKEffectNode()         effectNode.shader = SKShader(source:         """             void main() {                 vec2 uv = v_tex_coord;                 vec4 texel = texture2D(u_texture, uv);                 vec4 color = texel;                 vec4 outColor = texel;                 // from top to bottom                 if (uv.y > .8 / 2. + .5) { // bg color                     color = i_color;                 }                 outColor = mix(texel, color, .6);                 gl_FragColor = outColor;             }         """         , uniforms: [             SKUniform(name: "i_color", vectorFloat4: vector_float4(1, 0, 0, 0))         ])         scene?.addChild(effectNode)         effectNode.addChild(mediaNode)     } }
Replies
3
Boosts
8
Views
1.5k
Activity
Sep ’22