0

So im working on a game and with a ball in my swift 2 spritekit game. I have a make ball function and in the game scene, when the method is called, I keep receiving this error : "Cannot assign SKShapeNode' to type Ball! I am not sure how to fix and am new to swift. Thank you!

// ----- GameScene.Swift ----
class GameScene: SKScene, GameDelegate, SKPhysicsContactDelegate {
   // ball part one
   var ball: Ball!
   let ballSpeedX = CGFloat(500)
}

 override func didMoveToView(view: SKView) {
    // ball
    ball = Ball.make() // error!!!
    ball.position.x = CGRectGetMidX(self.frame)
    ball.position.y = CGRectGetMidY(self.frame)
    ball.physicsBody!.categoryBitMask = CollideType.Ball.toMask()
    ball.physicsBody!.collisionBitMask = CollideType.toMask([.Scene, .Ceil, .Floor]) | boards.usedCollideMasks
    ball.physicsBody!.contactTestBitMask = CollideType.toMask([.Scene, .Ceil, .Floor]) | boards.usedCollideMasks
    ball.hidden = true
    self.addChild(ball)
 }

Then this is ball.swift where the make() is //Ball. Swift

//imports...

class Ball: SKShapeNode {

  var speedXFirst = CGFloat(0)
var speedXSecond = CGFloat(0)
var lastFloor = -1

var xSpeed: CGFloat {
    get {
        return speedXFirst + speedXSecond
    }
}

var lastBoardNumber = 0

private override init() {
    super.init()
}

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

static func make()-> SKShapeNode {

    let textureBall : SKTexture! = SKTexture(imageNamed:"colorBall.png")
    let ballSize: CGSize = textureBall.size()
    var ball = SKShapeNode.init(circleOfRadius: ballSize.width/2)
    ball.fillTexture = textureBall
    ball.strokeColor = UIColor.clearColor()
    ball.name = "ball"
    ball.zPosition = 1
    ball.fillColor = UIColor.redColor() // use the color you want
    return ball
 }

 func freezeX() {
    self.speedXFirst = 0
    self.speedXSecond = 0
}
}
SwiftNewb
  • 9
  • 2
  • Duplicate of [this](http://stackoverflow.com/questions/38278089/assigning-an-image-to-a-skshapenode-as-a-ball-spritekit-game) – A Tyshka Jul 09 '16 at 23:58

1 Answers1

1

To execute ball = Ball.make() successfully, the return type of make() method needs to be Ball, not SKSpriteNode.

And to return a value of Ball from your make(), you need to create an instance of Ball, not SKSpriteNode.

static func make() -> Ball {  //the return type of `make()` method needs to be `Ball`

    let textureBall : SKTexture! = SKTexture(imageNamed:"colorBall.png")
    let ballSize: CGSize = textureBall.size()
    let ball = Ball(circleOfRadius: ballSize.width/2) //you need to create an instance of `Ball`
    ball.fillTexture = textureBall
    ball.strokeColor = UIColor.clearColor()
    ball.name = "ball"
    ball.zPosition = 1
    ball.fillColor = UIColor.redColor()
    return ball
}
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • thanks I adjusted the code to reflect your advice however the program crashes on launch screen and the log throws this error: fatal error: unexpectedly found nil while unwrapping an Optional value – SwiftNewb Jul 09 '16 at 08:14
  • First, check all forced unwrapping `!` when you get "unexpectedly found nil". Generally, you should not use `!` only when you are 100% sure that nil is not found there. Check all `!` in your code. – OOPer Jul 09 '16 at 08:52
  • I tried removing the ! in 'var ball: Ball!` but it breaks saying gamescene has no initilizers – SwiftNewb Jul 09 '16 at 09:00
  • You cannot solve your issue with just removing `!` without understanding why you need it and why you use it. – OOPer Jul 09 '16 at 09:09