将图像作为球分配给SKShapeNode-Spritekit游戏 [英] Assigning an image to a SKShapeNode as a Ball - Spritekit Game

查看:101
本文介绍了将图像作为球分配给SKShapeNode-Spritekit游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :

无法分配SKShapeNode'来输入Ball!

我不确定该如何解决,并且是Swift的新手.

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

这是 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类型不同(具有特定的init子类).

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).

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

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

这篇关于将图像作为球分配给SKShapeNode-Spritekit游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆