我正在创建一个游戏,我创建了一个SKScene子类,它应该作为其他子类的父级,用作级别。我初始化SKScene类的成员变量中某些块的位置和大小,类型为[CGPoint]。后来我定义了一个实际创建并将它们存储在[SKSpriteNode]类型的另一个成员变量aerialBlocks中的函数。
class GameScene: SKScene, SKPhysicsContactDelegate {
var floatingBlockSize = [CGSize(width: 50.0, height: 10.0)]
var floatingBlockPositions: [CGPoint] = [CGPointMake(130.0, 70.0)]
func addPlatforms() {
var temp = 0
let holder = floatingBlockPositions
for _ in floatingBlockPositions {
floatingBlockPositions[temp].y = floatingBlockPositions[temp].y + positionsForFirstBlock.y + (groundBlockSize.height / 2)
let aerialBlock = SKSpriteNode(imageNamed: "aerialBlock")
aerialBlock.size = floatingBlockSize[temp]
aerialBlock.position = floatingBlockPositions[temp]
print(aerialBlock.position)
aerialBLockBody = SKPhysicsBody(rectangleOfSize: floatingBlockSize[temp])
temp++
aerialBlock.physicsBody = aerialBLockBody
aerialBlock.physicsBody?.dynamic = false
aerialBlock.physicsBody?.categoryBitMask = physicsBitMasks.aerialBlock
aerialBlock.physicsBody?.contactTestBitMask = physicsBitMasks.player | physicsBitMasks.rollingEnemy | physicsBitMasks.walkingEnemy
aerialBlock.physicsBody?.collisionBitMask = physicsBitMasks.player | physicsBitMasks.rollingEnemy | physicsBitMasks.walkingEnemy
addChild(aerialBlock)
aerialBlock.name = "aerialBlock" + String(temp)
aerialBlocks.append(aerialBlock)
}
floatingBlockPositions = holder
}
}
然后在子类中,我尝试覆盖两个矢量参数,同时改变存储的大小和值。
class StreetFight: GameScene {
override var floatingBlockSize: [CGSize] {
get {
return [CGSize(width: 50.0, height: 10.0), CGSize(width: 150.0, height: 10.0)]
}
set (newValue) {
newValue
}
}
override var floatingBlockPositions: [CGPoint] {
get {
return [CGPointMake(400.0, 70.0), CGPointMake(250.0, 95.0)]
}
set (newValue) {
newValue
}
}
}
我依靠父addPlatforms()方法来解释这个新数据并相应地放置块/平台。然而,这些平台的位置远低于应有的位置。切换ViewController中显示的SKView,将一个平台正确放置在父类中。