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
790
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
406
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
676
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
832
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
589
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
507
Aug ’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
790
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
406
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
406
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
676
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
433
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
832
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
589
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
507
Activity
Aug ’21