gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:42:09 27 4
gpt4 key购买 nike

我正在尝试在我的 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
}

最佳答案

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

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

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

关于sprite-kit - 将图像分配给 SKShapeNode 作为球 - Spritekit 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38278089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com