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

时间:2016-07-09 03:45:52

标签: sprite-kit swift2 sprite skshapenode

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

"无法分配SKShapeNode'输入Ball!

我不确定如何修复并且是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
}

}

1 个答案:

答案 0 :(得分:1)

您遇到此问题是因为您尝试将常规初始化和自定义SKShapeNode(由make()方法构建)分配给不同的Ball类型{ {1}}类型(使用特定的init进行子类化)。

您可以像这样自定义 Ball 类:

SKShapeNode

所以,当你在场景中时,你可以这样称呼它:

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