将图像分配给 SKShapeNode 作为球 - Spritekit 游戏

Assigning an image to a SKShapeNode as a Ball - Spritekit Game

我想在我的 Swift 2 Sprite-kit 游戏中为我的球添加图像。我有一个 make() 球函数,它没有错误,但是在游戏场景中,当调用该方法时,我不断收到此错误:

"无法将 SKShapeNode' 分配给类型球!

我不知道如何修复并且是 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)
 }

这是 Ball.swift 其中 make() 是:

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

}

您遇到此问题是因为您尝试将常规初始化和自定义 SKShapeNode(由您的 make() 方法构建)分配给不相同的类型 Ball SKShapeNode 类型(subclassed 与特定的初始化)。

您可以像这样自定义您的 class:

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

所以当你在你的场景中时,你可以这样调用它:

var ball: Ball!
ball = Ball(imageNamed:"colorBall.png")