I have created a new iOS SWIFT file in XCode and wish to create a very simple class which holds a SKSpriteNode. When doing this, autocomplete and coloring (as set in preferences) is not working on my variables. Sample code:
import Foundation
import SpriteKit
class dots {
var dot = SKSpriteNode(imageNamed: "funnyFace.jpg")
dot.position = CGPoint(x: 100.0, y:100.0)
addChild(dot)
}
Any help would be greatly appreciated.
You have to put the code – except the variable declaration line – into a function.
import Foundation
import SpriteKit
class Dots {
var dot = SKSpriteNode(imageNamed: "funnyFace.jpg")
func positionAndAddTheDot() {
dot.position = CGPoint(x: 100.0, y:100.0)
addChild(dot)
}
}
Related
XCode Version 12.4 (12D4e)
The first SKScene class in Functions.swift included the buildBackground function which I want to call in different SKScene class.
class Functions: SKScene {
func buildBackground(backgroundPath: String){
let texture = SKTexture(imageNamed: backgroundPath)
let background = SKSpriteNode(texture: texture)
background.size = CGSize(width: 512, height: 512)
background.position = CGPoint(x: 0, y: 0)
background.name = "BACKGROUND"
addChild(background)
print("Background loaded")
}
}
I'm trying to call this function in the second SKScene class in the Room.swift file
I created an instance of Functions in Room and called my function just like that:
public class Room: SKScene {
let functions = Functions()
override public func didMove(to view: SKView) {
functions.buildBackground(backgroundPath: "roomBackground")
}
}
I got "Cannot find 'Functions' in scope" on the "let functions = Functions()" line. After running a build, I got my message "Background loaded", so I guess, the function was called, however, my wallpaper SKSpriteNode has not appeared in my scene. After I tried to move buildBackground method inside Room.swift class, it all works, but there are a lot of different scenes in my project (Room1, Room2, etc.) so I don't want to copy-paste this method each file.
since you are overriding a public function, you probably need to use self :
self.functions.buildBackground(...)
I have a SpriteKit scene in a swift playground (a SKView class with all the functions that come with it such as didMove, touchesBegan and Update), and when the live view is running it always responds super slow to touches and interactions. My question is would moving the whole source code from the swift playground to a separate swift file and import it into the "Sources" folder help with the live view response, and regardless of if it helps or not, how do I do it?
Here is what my swift playground looks like:
import PlaygroundSupport
import SpriteKit
import AVFoundation
import UIKit
class GameScene: SKScene {
//my source code
}
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 720, height: 540))
if let scene = GameScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
sceneView.presentScene(scene)
}
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
I also have about ten png and a couple .mp3 in the "Resources" folder, as well as Action.sks and GameScene.sks.
Found out a little earlier that when I move the GameScene class into a separate swift file everything would render very smoothly.
I want to be able to make the background of my application blurry so you can almost see through the window like this
rather than the standard window
Thank you!
I Found out the answer I was looking for and thought this might be useful for other people look for something similar. Heres my code :
import Cocoa
class window: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
let view = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: 1500, height: 800))
view.blendingMode = NSVisualEffectBlendingMode.BehindWindow
view.material = NSVisualEffectMaterial.Dark
view.state = NSVisualEffectState.Active
self.window!.contentView!.addSubview(view)
}
}
I made a NSWindowController class and added the code above to it. Then connected the class to the window controller in my main.storyboard.
I am currently working on a game where enemies get spawned from the left of the screen and move right. I want to give these enemies their own attributes (health, strength, etc). So I am working on creating a Basic_fighter class. I also have a user sniper scope that the user uses to hit the enemies. The problem I have is how to access the enemies attributes in the DidBeginContact Function since the function only returns two nodes, and not the class information. I will put my code below
Basic_Fighter_Class
import Foundation
import SpriteKit
class Basic_Fighter {
var health = Int()
var type = SKSpriteNode()
init(sk:SKSpriteNode){
self.type = sk
self.health = 3
}
}
func spawn_enemies(){
let enemynode = SKSpriteNode(imageNamed: "Shooter")
enemynode.size = CGSize(width: 100, height: 40)
enemynode.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
enemynode.physicsBody = SKPhysicsBody(rectangleOfSize: enemynode.size)
enemynode.physicsBody?.affectedByGravity = false
enemynode.physicsBody?.categoryBitMask = BodyType.enemy
enemynode.physicsBody?.contactTestBitMask = BodyType.bullet
let enemy = Basic_Fighter(sk: enemynode)
addChild(enemynode)
}
I am able to detect the contact made between the user scope and the enemy in the DidBeginContact function, but I do not know how to access the information of the enemy, such as its health.
I think you can access it with enemynode.health, I'm not sure though.
I've just picking up Swift and playing around with SceneKit. I created a simple "Smoke.scnp" particle emitter in the xcode 6 editor, and I'm trying to load that using the SCNParticleSystem class. I have included the SceneKit frame work with the project. When running without the particle system, I could load the camera and other nodes just fine, but including the particle system failed during linking time. The error I got was symbol not found for SCNParticleSystem.
I don't have much experience with SceneKit, so this may not be something specific to xcode6 or Swift. Just wondering if I have to set up anything else?
Below is the snippet of my code:
import SceneKit
import QuartzCore
class GameViewController: NSViewController {
#IBOutlet var gameView: GameView
override func awakeFromNib(){
// create a new scene
let scene = SCNScene()
// Add camera to scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
// Place camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
// Add particle emitter
let bgSmokeNode = SCNNode()
var particleSystem = SCNParticleSystem(named: "Smoke", inDirectory: "")
bgSmokeNode.addParticleSystem(particleSystem)
scene.rootNode.addChildNode(bgSmokeNode)
}
}