Larisa
Larisa

Reputation: 791

Error when trying to initialize a class as a SKSpriteNode subclass and use it in GameScene - Swift 2

All the tutorials I have followed, make sprites with all their functions and everything in the same class as everything else - GameScene class. Now I am trying to divide the code from the GameScene to different classes but am having troubles with initializing the first class. Because I have almost no code, I will copy and paste all of it.

Error message:

/Users/Larisa/Desktop/Xcode/Igranje/Poskus2/Poskus2/PlayerUros.swift:17:15: Cannot invoke 'SKSpriteNode.init' with an argument list of type '(texture: SKTexture, position: CGPoint, size: () -> CGSize)'

GameScene class:

import SpriteKit

class GameScene: SKScene {
  var player: PlayerUros!

  override func didMoveToView(view: SKView) {
    let pos = CGPointMake(size.width*0.1, size.height/2)
    player = PlayerUros(pos: pos)
    addChild(player)
    player.rotate()
    setupBackground()
  }

  func setupBackground() {
    let BImage = SKSpriteNode(imageNamed: "bg-sky")
    BImage.anchorPoint = CGPointMake(0, 0)
    BImage.position = CGPointMake(0, 0)
    BImage.size = CGSizeMake(self.size.width, self.size.height)
    addChild(BImage)
  }
}

Player class:

import Darwin
import SpriteKit

class PlayerUros: SKSpriteNode {

  init(pos: CGPoint) {
    let texture = SKTexture(imageNamed: "hero")

    super.init(texture: texture, position: pos, size: SKTexture.size(texture))
  }

  func rotate() {
    let rotate = SKAction.rotateByAngle(CGFloat(M_PI), duration: NSTimeInterval(1.5))
    let repeatAction = SKAction.repeatActionForever(rotate)
    self.runAction(repeatAction)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

and GameViewController class (not sure if it matters):

import UIKit
import SpriteKit

class GameViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let scene = GameScene(size: view.bounds.size)
    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .ResizeFill
    skView.presentScene(scene)
  }

  override func prefersStatusBarHidden() -> Bool {
    return true
  }
}

Upvotes: 1

Views: 594

Answers (1)

RoberRM
RoberRM

Reputation: 883

Try using the line

super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())

instead of

super.init(texture: texture, position: pos, size: SKTexture.size(texture))

in your Player class. It works for me. Does it work for you?

Upvotes: 2

Related Questions