-2

Sorry for a newbie question but can someone help with translating this to Swift?

-(instancetype)init
{
  self = [super initWithImageNamed:@"character.png"];
  {.
    self.name = playerName;
    self.zPosition = 10;
  }
  return self;
}

it's for a child of SKSpriteNode

When I try to call super.init(imageNamed: "character.png") I get an error saying `Must call a designated Initialiser of the superclass SKSpriteNode.

If I try to just write it like this:

init() {
    super.init()
    self.name = playerName
    self.zPosition = 10
}

I get an error in my GameScene when I call:

var player : Player = Player(childNodeWithName(playerName))

I get an error about converting the type to string.

Sawyer05
  • 1,604
  • 2
  • 22
  • 37

1 Answers1

3

issue 1

a subclass initializer MUST call the superclass's designated Initializer.

for SKSpritenode that's initWithTexture: color: size:

SO

a SKTexture can be made from an image directly:
let texture = SKTexture(imageName: "character.png")

a color:
let color = UIColor.clearColor()

then:
super.init(texture: texture color:color size:texture.size)

issue 2:

class player needs an initialiser that takes a SKNode:
init(node: SKNode) { ... }

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • disclaimer: written inline, might contain typos ;) – Daij-Djan Jul 12 '14 at 20:33
  • Thanks Daij-Djan that did it for the most part, however, how would you recommend I go bout this part here: `var player : Player = Player(childNodeWithName(playerName))` from my GameScene. I'm following a book on iOS 7 so want to keep with the syntax as best I can. playerName is just a string "player" – Sawyer05 Jul 12 '14 at 20:40
  • player needs a valid constructor – Daij-Djan Jul 12 '14 at 20:53