i have a problem.
I made swift custom class test.swift and want to add a sprite from this Class to the the GameScene.swift
This is my custom Class
import SpriteKit
import GameplayKit
var scene = GameScene()
class test: SKSpriteNode{
func test_function(){
let mysprite = SKSpriteNode(imageNamed: "spark3")
mysprite.name="sparkle4"
mysprite.position = CGPoint(x: frame.midX, y: frame.midY)
mysprite.setScale(8)
mysprite.zPosition=2
self.scene?.addChild(mysprite)
}
}
In the GameScene i call this function with
var new_class = test()
new_class.test_function()
but the sprite is not displayed in the app and i see no errors.
If i copy the function direct in the GameScene it worked.
Can anyone help me what i do wrong?
Thanks alot
Because
self.scene?
isnil
inside the subclass , while it's not insideGameScene
, a node gets it'sscene?
assigned after you add it as a child to a scene//
So you may do it like this