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