Reuse same sprite node within multiple scenes in sprite kit using Swift -
i create sprite node in gamescene following. reuse createnodea1 or nodea1 in other skscene. how can that?
import spritekit class gamescene: skscene { var nodea1: sknode! required init?(coder adecoder: nscoder) { super.init(coder: adecoder) } override init(size: cgsize) { super.init(size: size) // add sprite node scene nodea1 = createnodea1() addchild(nodea1) } } // create dot 1 func createnodea1() -> sknode { let spritenode = sknode() spritenode.position = cgpointmake(cgrectgetmidx(self.frame)/1.5, cgrectgetmidy(self.frame)/2.0) let sprite = skspritenode(imagenamed: "dot_1") sprite.zposition = 3.0 sprite.name = "a1_broj" spritenode.addchild(sprite) return spritenode } }
there few ways this.
you subclass other scenes subclass of scene loadnode function gives scenes access function.
i asked question last year
another way might bit easier if not comfortable scene subclassing create subclass of node itself.
so create class
enum enemytype { case normal case special } class nodea1: skspritenode { init(imagenamed: string, enemytype: enemytype) { let texture = sktexture(imagenamed: imagenamed) if enemytype == .normal { super.init(texture: texture, color: skcolor.clearcolor(), size: texture.size()) else { // other init } self.zposition = 1 self.name = "" // add physics body, other properties or methods node } } than in skscenes can add node in init method so
nodea1 = nodea1(imagenamed: "imagename", enemytype: .normal) nodea1.position = .... addchild(nodea1) this way ever scene add node use subclass , therefore include properties, set etc node. benefit subclassing loop through nodes using
self.enumeratechildnodeswithname... and call custom methods on nodes.
if want subclass scenes create basescene
class basescene: skscene { // set shared stuff in didmovetoview // have node function here // touches began // physics word , contact collision // other stuff needs shared between level scenes } than subsequent level scenes this
class level1scene: basescene { override func didmovetoview(view: skview) { super.didmovetoview(view) // lines imports stuff in basescene didmovetoview // level 1 specific setups. // can call function or property basescene, e.g loadnode function. } you load level scenes usual, e.g transition level 1 scene , automatically use/have access superclass methods , sprites (basescene). never call basescene directly, gets called automatically.
this applies other methods in basescene too, have update method in basescene.
override func update(currenttime: cftimeinterval) {.... } this work across level scenes subclasses of basescene.
but happens if need add specific stuff update method relevant in 1 level scene , not level scenes? same process, create new update func in levelscene , call super.
override func update(currenttime: cftimeinterval) { super.update(currenttime) // calls basescene update method /// specific stuff level } super means super class of currentscene, basescene if scene subclass of it.
is helping?
Comments
Post a Comment