0

I made a SKShapeNode subclass called Player:

import UIKit
import SpriteKit

let MOVE_UP: CGFloat = 3
class Player: SKShapeNode{

    var lifePoints = 3

    init(circleOfRadius: CGFloat, fillColor: UIColor, strokeColor: UIColor) {
        super.init()
        self.fillColor = fillColor
        self.strokeColor = strokeColor
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    class func movePlayerUp(mPlayer: SKShapeNode){
        mPlayer.position.y += MOVE_UP
    }
}

in GameScene I'm trying to create a sprite of type Player and show it on the screen. I have no error and the build does not fail but the sprite won't show up on the screen. this is how I'm creating the sprite:

override func didMove(to view: SKView) {
   let sprite = Player(circleOfRadius: 45, fillColor: myColors.blue, strokeColor: myColors.yellow)
        sprite.position = CGPoint(x: frame.midX, y: frame.midY)

        self.addChild(sprite)
}
swiftnewbie
  • 201
  • 3
  • 12
  • unrelated, but a shapenode is not a sprite :) – Fluidity Feb 03 '17 at 20:54
  • @Fluidity lol, thanks :) It won't be called sprite it is just a test, which is not working o.O ... – swiftnewbie Feb 03 '17 at 20:56
  • 3
    You called `super.init()` instead of `super.init(circleOfRadius: circleOfRadius)` in your initializer. – NobodyNada Feb 03 '17 at 20:58
  • @NobodyNada I get an error that says: "Must call a designated initializer of the superclass SKShapeNode" – swiftnewbie Feb 03 '17 at 21:04
  • 1
    @swiftnewbie See http://stackoverflow.com/q/28223484/3476191 – NobodyNada Feb 03 '17 at 21:05
  • are you sure it is not displaying? set the position to 0,0 and I bet it will show in the center of the screen. your midX mid Y is probably moving it to the top right corner of the scene, and may be getting cropped – Knight0fDragon Feb 03 '17 at 21:07
  • I have posted a working solution showing the correct designated initializer + path modification. – Fluidity Feb 03 '17 at 21:09
  • oh yes, lol you do not actually create a circle. Unfortunately you can't access a convenience init via an override from a class inside of a library or framework, so you have to either set the path like people have mentioned, or create a secondary setup function after creating a shape using the existing convenience inits – Knight0fDragon Feb 03 '17 at 21:11
  • hey there, please upvote my answer that you accepted if you don't mind :) Thanks! – Fluidity Jun 12 '17 at 03:40

1 Answers1

0

The problem is your super.init()... it's blank... you made your own initializer with a pram circleOfRadius but this means nothing to the computer since you did nothing with it.

Worse, is that super.init(circleOfRadius: circleOfRadius) won't work either because it is not a designated initializer... it is a convenience initializer which you can't use when calling to a super.init

So, basically you must resort to using super.init() but then *change the path of the empty shape you just made`

Add this and you will get your circle:

self.path = CGPath.init(roundedRect: CGRect(x: 0, y: 0, width: circleOfRadius*2, height: circleOfRadius*2), cornerWidth: 45, cornerHeight: 45, transform: nil)

enter image description here

Fluidity
  • 3,985
  • 1
  • 13
  • 34