3

I am trying to add an image to my ball in my Swift 2 Sprite-kit game. I have a make() ball function and it there are no errors there however 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.

// ----- 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)
 }

And 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
}

}

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
N. Howey
  • 119
  • 7

1 Answers1

1

You have this issue because you try to assign a regular initialized and customized SKShapeNode (built by your make() method) to a type Ball that isn't the same SKShapeNode type (subclassed with a specific init).

You could customized your Ball class like this:

class Ball: SKShapeNode {
    var speedXFirst = CGFloat(0)
    var speedXSecond = CGFloat(0)
    var lastFloor = -1
    private var radius: CGFloat!

    var xSpeed: CGFloat {
        get {
            return speedXFirst + speedXSecond
        }
    }
    var lastBoardNumber = 0

    init(imageNamed: String){
        super.init()
        let textureBall : SKTexture! = SKTexture.init(imageNamed:imageNamed)
        let ballSize: CGSize = textureBall.size()
        self.radius = ballSize.width/2
        self.path = CGPathCreateWithEllipseInRect(CGRect(origin: CGPointZero, size: CGSize(width: radius*2, height: radius*2)), nil)
        self.strokeColor = UIColor.clearColor()
        self.name = "ball"
        self.zPosition = 1
        self.fillTexture = textureBall
        self.fillColor = UIColor.redColor() // use the color you want
    }
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func freezeX() {
        self.speedXFirst = 0
        self.speedXSecond = 0
    }
}

So when you are in your scene you can call it like this:

var ball: Ball!
ball = Ball(imageNamed:"colorBall.png")
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • thanks, with all of this code, in my game scene `ball = Ball(imageNamed:"colorBall.png")` throws error: Expected declaration. – N. Howey Jul 09 '16 at 08:43
  • i did, I also did the gamescene and it throws that expected declaration error – N. Howey Jul 09 '16 at 08:48
  • Very strange, probably you have some another issue. If it can be useful download the project from here http://s000.tinyupload.com/index.php?file_id=04551335866809243939 – Alessandro Ornano Jul 09 '16 at 08:54
  • Update: I opened that project and noticed a mistake I made, I fixed it and when I run it, it makes it to the launch screen and throws this error: fatal error: unexpectedly found nil while unwrapping an Optional value, tried deleting the ! after ball but that caused 'no initilizers' error – N. Howey Jul 09 '16 at 09:09
  • Sorry, I dont understand because I've the project in running correctly, you speak about my project or your project? Have you declare the var ball: Ball! ? – Alessandro Ornano Jul 09 '16 at 09:11
  • my project, i have it declared like you, `var ball: Ball!` – N. Howey Jul 09 '16 at 09:14
  • Ok, the unexpectedly found nil while unwrapping come from the physicsBody right? It's come because you haven't initialize the physicBody of your object, but in your code you try to directly fill the properties like categoryBitMask, i dont use these lines in my project , I've commented it – Alessandro Ornano Jul 09 '16 at 09:16
  • Proceed by steps, fix your Ball.swift, after that run and if it's ok after think to how to build the physicBody :) – Alessandro Ornano Jul 09 '16 at 09:19
  • ah yes, its from the physicsBody, is there a way to fix that? All of the physics in my game are a problem now, I commented out similar to you in ball, but when touches began it crashes. – N. Howey Jul 09 '16 at 09:24
  • Ok, so this issue is solved. If you have another issues like touchesBegan or PhysicsBody initialization please open new questions so the people who read this question title can see only all the answers about it. – Alessandro Ornano Jul 09 '16 at 09:26
  • Thank you for your help, your amazing. i opened a new question http://stackoverflow.com/questions/38280426/unwrapping-optional-values-physicsbody-swift-spritekit – N. Howey Jul 09 '16 at 09:40
  • @N.Howey seems anyone considered your new question in link as a duplicate link to a remote question that has nothing to do with Sprite-Kit and your request, change the question content to avoid closure. – Alessandro Ornano Jul 09 '16 at 12:39