SpriteKit

RSS for tag

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

SpriteKit Documentation

Posts under SpriteKit subtopic

Post

Replies

Boosts

Views

Activity

how to randomly spawn enemies on screen
let randomNumber = arc4random_uniform(2) let x: CGFloat = randomNumber == 0 ? 1 : -1 enemy.position = CGPoint(x: (CGFloat(arc4random_uniform(UInt32(UIScreen.main.bounds.width))) * x), y: UIScreen.main.bounds.height) here is my code for enemy spawn, it works but some of my enemies are spawning outside of my screen. What can i change to make them all spawn inside the border
1
0
780
Sep ’21
update has already been overridden
Im getting a overridden error for my player and enemy. I made a enemy file and added this code into my gamescene. My enemy only spawns in if i delete my player movement code func spawnEnemy() {       let enemy = Enemy(image: SKSpriteNode(imageNamed: "enemy"))   self.addChild(enemy)   }       override func update(_ currentTime: TimeInterval) {     spawnEnemy()   }
0
0
404
Sep ’21
AUv3 with SwiftUI and Spritekit crash
I have the bare bones of an AUv3 plug in. I have used UIHostingController to allow me to add a SwiftUI view. Then from the SwiftUI I have added a SprikeKit scene which I want to use to start building an interface for the AU. It's a very basic GameScene for now. If I run one instance of it in Logic then it's OK. If I add another instance it crashes with a Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) error. This seems to be down to how I declare a SpriteNode variable. If I declare it within the GameScene as a "let" then it's fine. If I declare it outside the class - (see hashed code) then it will crash with two instances. It would be useful to me later on for this to be a public variable as I want to perform functions on it outside of the class. Is there a better way to declare the SpriteNode variable to make it stable? import SpriteKit //public var ball = SKShapeNode(circleOfRadius: 30) var ball = SKShapeNode(circleOfRadius: 30) class GameScene: SKScene {         let ball = SKShapeNode(circleOfRadius: 30)     override func didMove(to view: SKView) {            //physicsBody = SKPhysicsBody(edgeLoopFrom: frame)         ball.position = CGPoint(x: 200, y: 200)         ball.fillColor = .lightGray         self.addChild(ball)          }
0
0
664
Sep ’21
How to use EnvironmentObject in an init()
I am trying to use an EnvironmentObject in an init. I am aware that this is not possible because it hasn't been initialised yet. But what is the solution? I have tried loading the scene.gameCenterManger in .onAppear but that doesn't seem to work. I want to use @State for my gamescene variable, because SKScene will reset itself when the view does a refresh. struct GameSceneView: View {     @EnvironmentObject var gameCenterManager:GameCenterManager @State var scene = GameScene()     init() {   scene.size = CGSize(width: 1000, height: 1800)       scene.scaleMode = .aspectFill // doing it as below will throw an error. //scene.gameCenterManager = gameCenterManager     }     var body: some View {         SpriteView(scene: scene )             .environmentObject(gameCenterManager)             .frame(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)             .ignoresSafeArea()             .overlay(ImageOverlay(), alignment: .bottomTrailing)            .onAppear{ scene.gameCenterManager = gameCenterManager  }     } }
0
0
828
Aug ’21
How to add particle trail
When adding a particle emitter in SpriteKit, I want each particle to stay in the same spot instead of moving with the node the particle is coming off of. My node moves, and I want the particle to flow off of the back and float up (as a trail). Not to be stuck dragged behind the node. Any help? Im using swift in Xcode.
1
0
585
Aug ’21
What happens if I execute an SKAction run sequence on a node that is already executing another sequence?
I have an SKSpriteNode with an SKAction being run on it: theGem!.run(premAction, completion: {theGem!.run(repeatAction)}) Can't seem to find out the proper steps to run another action, such as: theGem.run(endsequence, completion: {theGem.removeAllActions(); theGem.run(stopAction)}) Should I stop the previous action first? Is there a way to turn the repeat part off so that the first SKAction ends smoothly?
0
0
503
Aug ’21
System Font Not Working on SKLabelNodes in iOS13
I have a label in a SpriteKit appvar speedLabel: SKLabelNode = SKLabelNode()And when I try to assign it a Font like so:speedLabel.fontName = UIFont.preferredFont(forTextStyle: .body).fontNameIn iOS 11.0-12.x, that returns ".SFUIText" and gives the label the proper san-serif system font.In iOS 13.0, it returns ".SFUI-Regular" and shows up as some serifed, maybe Times New Roman font.I remember having issues with SpriteKit Label Nodes and fonts in the past. If you assigned it an invalid font, it would default to this serifed font.Anyone have any ideas how to fix this?
5
0
4.2k
Jul ’21
Pixelate transition in SpriteKit using CIFilter
Working from Apple documentation I wanted to apply the CIPixellate filter to a SpriteKit Node and found that SKEffectNode grants access to CoreGraphic's filters and could be a good bridge. I can get the image to pixelate, but I am looking for a smooth transition between the two stages and believe that I am having trouble with the "simd_smoothstep" function: let pixelateNode: SKEffectNode = SKEffectNode() let testSprite = SKSpriteNode(imageNamed: "testSprite") let filter = CIFilter(name: "CIPixellate")! let inputScale = simd_smoothstep(1, 0, abs(0.9)) let filter = CIFilter(name:"CIPixellate", parameters: [kCIInputImageKey: testSprite, kCIInputScaleKey: inputScale]) pixelateNode.filter = filter pixelateNode.shouldEnableEffects = true addChild(pixelateNode) pixelateNode.addChild(testSprite) ref: https://developer.apple.com/documentation/coreimage/customizing_image_transitions ref: https://developer.apple.com/documentation/coreimage/customizing_image_transitions nb. this is a repost in the SpriteKit forum. The old post can be removed: https://developer.apple.com/forums/thread/684687
1
0
820
Jul ’21
Pixelate transition in SpriteKit using CIFilter
Working from Apple documentation I wanted to apply the CIPixellate filter to a SpriteKit Node and found that SKEffectNode grants access to CoreGraphic's filters and could be a good bridge. I can get the image to pixelate, but I am looking for a smooth transition between the two stages and believe that I am having trouble with the "simd_smoothstep" function: let pixelateNode: SKEffectNode = SKEffectNode() let testSprite = SKSpriteNode(imageNamed: "testSprite") let filter = CIFilter(name: "CIPixellate")! let inputScale = simd_smoothstep(1, 0, abs(0.9)) let filter = CIFilter(name:"CIPixellate", parameters: [kCIInputImageKey: testSprite, kCIInputScaleKey: inputScale]) pixelateNode.filter = filter pixelateNode.shouldEnableEffects = true addChild(pixelateNode) pixelateNode.addChild(testSprite) ref: https://developer.apple.com/documentation/coreimage/customizing_image_transitions
1
0
705
Jul ’21
how to randomly spawn enemies on screen
let randomNumber = arc4random_uniform(2) let x: CGFloat = randomNumber == 0 ? 1 : -1 enemy.position = CGPoint(x: (CGFloat(arc4random_uniform(UInt32(UIScreen.main.bounds.width))) * x), y: UIScreen.main.bounds.height) here is my code for enemy spawn, it works but some of my enemies are spawning outside of my screen. What can i change to make them all spawn inside the border
Replies
1
Boosts
0
Views
780
Activity
Sep ’21
how to make object fall with gravity
How can i change my objects gravity without having my player fall with it. I added physicsWorld.gravity = CGVector(dx: 0, dy: 0) on my players movement and affectedByGravity on both my object and player. If I change dy to -9.8, my object falls but along with my player
Replies
0
Boosts
0
Views
403
Activity
Sep ’21
update has already been overridden
Im getting a overridden error for my player and enemy. I made a enemy file and added this code into my gamescene. My enemy only spawns in if i delete my player movement code func spawnEnemy() {       let enemy = Enemy(image: SKSpriteNode(imageNamed: "enemy"))   self.addChild(enemy)   }       override func update(_ currentTime: TimeInterval) {     spawnEnemy()   }
Replies
0
Boosts
0
Views
404
Activity
Sep ’21
AUv3 with SwiftUI and Spritekit crash
I have the bare bones of an AUv3 plug in. I have used UIHostingController to allow me to add a SwiftUI view. Then from the SwiftUI I have added a SprikeKit scene which I want to use to start building an interface for the AU. It's a very basic GameScene for now. If I run one instance of it in Logic then it's OK. If I add another instance it crashes with a Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) error. This seems to be down to how I declare a SpriteNode variable. If I declare it within the GameScene as a "let" then it's fine. If I declare it outside the class - (see hashed code) then it will crash with two instances. It would be useful to me later on for this to be a public variable as I want to perform functions on it outside of the class. Is there a better way to declare the SpriteNode variable to make it stable? import SpriteKit //public var ball = SKShapeNode(circleOfRadius: 30) var ball = SKShapeNode(circleOfRadius: 30) class GameScene: SKScene {         let ball = SKShapeNode(circleOfRadius: 30)     override func didMove(to view: SKView) {            //physicsBody = SKPhysicsBody(edgeLoopFrom: frame)         ball.position = CGPoint(x: 200, y: 200)         ball.fillColor = .lightGray         self.addChild(ball)          }
Replies
0
Boosts
0
Views
664
Activity
Sep ’21
Gamescene.sks deleted
I deleted the grid on gamescene.sks and i dont know how to get it back. Now my game wont run
Replies
0
Boosts
0
Views
431
Activity
Aug ’21
How to use EnvironmentObject in an init()
I am trying to use an EnvironmentObject in an init. I am aware that this is not possible because it hasn't been initialised yet. But what is the solution? I have tried loading the scene.gameCenterManger in .onAppear but that doesn't seem to work. I want to use @State for my gamescene variable, because SKScene will reset itself when the view does a refresh. struct GameSceneView: View {     @EnvironmentObject var gameCenterManager:GameCenterManager @State var scene = GameScene()     init() {   scene.size = CGSize(width: 1000, height: 1800)       scene.scaleMode = .aspectFill // doing it as below will throw an error. //scene.gameCenterManager = gameCenterManager     }     var body: some View {         SpriteView(scene: scene )             .environmentObject(gameCenterManager)             .frame(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)             .ignoresSafeArea()             .overlay(ImageOverlay(), alignment: .bottomTrailing)            .onAppear{ scene.gameCenterManager = gameCenterManager  }     } }
Replies
0
Boosts
0
Views
828
Activity
Aug ’21
How to add particle trail
When adding a particle emitter in SpriteKit, I want each particle to stay in the same spot instead of moving with the node the particle is coming off of. My node moves, and I want the particle to flow off of the back and float up (as a trail). Not to be stuck dragged behind the node. Any help? Im using swift in Xcode.
Replies
1
Boosts
0
Views
585
Activity
Aug ’21
What happens if I execute an SKAction run sequence on a node that is already executing another sequence?
I have an SKSpriteNode with an SKAction being run on it: theGem!.run(premAction, completion: {theGem!.run(repeatAction)}) Can't seem to find out the proper steps to run another action, such as: theGem.run(endsequence, completion: {theGem.removeAllActions(); theGem.run(stopAction)}) Should I stop the previous action first? Is there a way to turn the repeat part off so that the first SKAction ends smoothly?
Replies
0
Boosts
0
Views
503
Activity
Aug ’21
System Font Not Working on SKLabelNodes in iOS13
I have a label in a SpriteKit appvar speedLabel: SKLabelNode = SKLabelNode()And when I try to assign it a Font like so:speedLabel.fontName = UIFont.preferredFont(forTextStyle: .body).fontNameIn iOS 11.0-12.x, that returns ".SFUIText" and gives the label the proper san-serif system font.In iOS 13.0, it returns ".SFUI-Regular" and shows up as some serifed, maybe Times New Roman font.I remember having issues with SpriteKit Label Nodes and fonts in the past. If you assigned it an invalid font, it would default to this serifed font.Anyone have any ideas how to fix this?
Replies
5
Boosts
0
Views
4.2k
Activity
Jul ’21
Pixelate transition in SpriteKit using CIFilter
Working from Apple documentation I wanted to apply the CIPixellate filter to a SpriteKit Node and found that SKEffectNode grants access to CoreGraphic's filters and could be a good bridge. I can get the image to pixelate, but I am looking for a smooth transition between the two stages and believe that I am having trouble with the "simd_smoothstep" function: let pixelateNode: SKEffectNode = SKEffectNode() let testSprite = SKSpriteNode(imageNamed: "testSprite") let filter = CIFilter(name: "CIPixellate")! let inputScale = simd_smoothstep(1, 0, abs(0.9)) let filter = CIFilter(name:"CIPixellate", parameters: [kCIInputImageKey: testSprite, kCIInputScaleKey: inputScale]) pixelateNode.filter = filter pixelateNode.shouldEnableEffects = true addChild(pixelateNode) pixelateNode.addChild(testSprite) ref: https://developer.apple.com/documentation/coreimage/customizing_image_transitions ref: https://developer.apple.com/documentation/coreimage/customizing_image_transitions nb. this is a repost in the SpriteKit forum. The old post can be removed: https://developer.apple.com/forums/thread/684687
Replies
1
Boosts
0
Views
820
Activity
Jul ’21
Pixelate transition in SpriteKit using CIFilter
Working from Apple documentation I wanted to apply the CIPixellate filter to a SpriteKit Node and found that SKEffectNode grants access to CoreGraphic's filters and could be a good bridge. I can get the image to pixelate, but I am looking for a smooth transition between the two stages and believe that I am having trouble with the "simd_smoothstep" function: let pixelateNode: SKEffectNode = SKEffectNode() let testSprite = SKSpriteNode(imageNamed: "testSprite") let filter = CIFilter(name: "CIPixellate")! let inputScale = simd_smoothstep(1, 0, abs(0.9)) let filter = CIFilter(name:"CIPixellate", parameters: [kCIInputImageKey: testSprite, kCIInputScaleKey: inputScale]) pixelateNode.filter = filter pixelateNode.shouldEnableEffects = true addChild(pixelateNode) pixelateNode.addChild(testSprite) ref: https://developer.apple.com/documentation/coreimage/customizing_image_transitions
Replies
1
Boosts
0
Views
705
Activity
Jul ’21