Assigning an image to a SKShapeNode as a Ball - Sp

2019-05-21 18:30发布

问题:

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
}

}

回答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")