swift - Why can not set name of SKSprite in its init function? -
i have extended skspritenode , want set name attribute in init function:
class ship: skspritenode { var id: int /** * image name of sprite image bubble. **/ var image: string //some other vars not name, because super class sknode has name init(ship: (name: string, <other vars>), position: cgpoint) { let texture = sktexture(imagenamed: self.image) super.init(texture: texture, color: uicolor.clearcolor(), size: texture.size()) self.name = name //here set name!!! self.position = position self.physicsbody = skphysicsbody(circleofradius: self.size.width / 2) self.physicsbody!.dynamic = true } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
but later, when create 'ship' (ship[1] values including name):
var titanic = ship(ship: ship[1], position: shipposition!) println(titanic.name) //nil self.addchild(titanic) titanic.name = 'titanic' println(titanic.name) //titanic
as can see - name not set on initialization - have set after created.
how can set name in init method? or have name after creation?
in context, self.name = name
means same thing self.name = self.name
.
you trying pass variable in tuple, want self.name = ship.name
. (or name = ship.name
.)
or, pass these arguments directly initializer:
init(named name: string, <other vars>, position: cgpoint) { // ... self.name = name } ship(named: "titanic", <other vars>, position: shipposition!)
Comments
Post a Comment