sprite kit - How can I get my SKNodes in different file from the gamescene swift -


i couldn't find on having sknodes in seperate files gamescene.swift. tried writing in new class swift file did not seem work. had make function did not know how call in gamescene.

let testlabel = sklabelnode(fontnamed: "applesdgothicneo-medium")     testlabel.text = "test"     testlabel.position = cgpoint(x: self.frame.midx, y: 300)     testlabel.fontsize = 90     testlabel.color = skcolor.blue     testlabel.horizontalalignmentmode = sklabelhorizontalalignmentmode.center 

i'm trying make in swift file , use testlabel in gamescene.

thanks in advance.

rather creating new file, consider creating property label inside gamescene.swift file. lets add label scene calling self.testlabel.

class gamescene: skscene {     lazy var testlabel: sklabelnode! = {         let testlabel = sklabelnode(fontnamed: "applesdgothicneo-medium")         testlabel.text = "test"         testlabel.position = cgpoint(x: self.frame.midx, y: 300)         testlabel.fontsize = 90         testlabel.color = skcolor.blue         testlabel.horizontalalignmentmode = sklabelhorizontalalignmentmode.center          return testlabel     }()      override func didmove(to view: skview) {         self.addchild(self.testlabel)     } } 

it doesn't make sense create new file, unless want create custom sklabelnode extended functionality.

another approach create function creates label input parameters.

class gamescene: skscene {     func maketestlabel(text: string, position: cgpoint) -> sklabelnode {         let testlabel = sklabelnode(fontnamed: "applesdgothicneo-medium")         testlabel.text = text         testlabel.position = position         testlabel.fontsize = 90         testlabel.color = skcolor.blue         testlabel.horizontalalignmentmode = sklabelhorizontalalignmentmode.center          return testlabel     }      override func didmove(to view: skview) {         self.addchild(self.maketestlabel(text: "test1", position: cgpoint(x: self.frame.midx, y: 300)))         self.addchild(self.maketestlabel(text: "test2", position: cgpoint(x: self.frame.midx, y: 400)))     } } 

another option, if want share between scenes create extension on skscene , move maketestlabel function. how used remains same. gives flexibility reuse logic between scenes , answers original question!

// skscene+extensions.swift import spritekit extension skscene {     func maketestlabel(text: string, position: cgpoint) -> sklabelnode {         let testlabel = sklabelnode(fontnamed: "applesdgothicneo-medium")         testlabel.text = text         testlabel.position = position         testlabel.fontsize = 90         testlabel.color = skcolor.blue         testlabel.horizontalalignmentmode = sklabelhorizontalalignmentmode.center          return testlabel     } }  // gamescene.swift class gamescene: skscene {     override func didmove(to view: skview) {         self.addchild(self.maketestlabel(text: "test1", position: cgpoint(x: self.frame.midx, y: 300)))         self.addchild(self.maketestlabel(text: "test2", position: cgpoint(x: self.frame.midx, y: 400)))     } } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -