在切换条件下加载一些私有变量

时间:2019-04-09 16:37:22

标签: ios swift sprite-kit

我正在制作一个简单的iOS游戏,但遇到一些问题。 实际上,我为元素创建了不同的类:行星,子弹头,背景和小行星。 但是,当我尝试在切换条件下(从GameScene)初始化它们时,出现错误消息“期望的声明”。

变量“ lvlSelected”是全局变量,代表玩家(从另一个场景)要求的级别。

这是我的切换代码:

 switch lvlSelected {
case 1 :
private var planet = Planet(imageName: "Planet1")
private var bullet = Bullet(imageName: "Bullet1")
private var background = Background(imageName: "Background1")
private var asteroide = Asteroides(img: "Asteroid1")

case 2 :
private var planet = Planet(imageName: "Planet2")
private var bullet = Bullet(imageName: "Bullet2")
private var background = Background(imageName: "Background2")
private var asteroide = Asteroides(img: "Asteroid2")

default :
private var planet = Planet(imageName: "Planet")
private var bullet = Bullet (imageName: "Bullet")
private var background = Background(imageName : "Background")
private var asteroide = Asteroides(img."Asteroid")
}

感谢您的帮助


这是我星球的课程:

class Planet:SKSpriteNode {

init(imageName: String){

    let texture = SKTexture(imageNamed : imageName)
    super.init(texture: texture, color: .clear, size: texture.size())

    self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2, center: self.anchorPoint) 
    self.physicsBody!.affectedByGravity = false 
    self.physicsBody!.categoryBitMask = GameScene.physicsCategories.planet
    self.physicsBody!.collisionBitMask = GameScene.physicsCategories.none
    self.physicsBody!.contactTestBitMask = GameScene.physicsCategories.asteroid

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
 func addPlanet(parent: GameScene) {
    self.setScale(0.55)
    self.position = CGPoint(x: parent.size.width / 2, y: -self.size.height)
    self.zPosition = 2
    self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    parent.addChild(self)
}

2 个答案:

答案 0 :(得分:1)

这应该是您想要的方式。

private var planet = Planet(imageName: "Planet")
private var bullet = Bullet (imageName: "Bullet")
private var background = Background(imageName : "Background")
private var asteroide = Asteroides(img."Asteroid")

switch lvlSelected {
case 1 :
  planet = Planet(imageName: "Planet1")
  bullet = Bullet(imageName: "Bullet1")
  background = Background(imageName: "Background1")
  asteroide = Asteroides(img: "Asteroid1")
break
case 2 :
  planet = Planet(imageName: "Planet2")
  bullet = Bullet(imageName: "Bullet2")
  background = Background(imageName: "Background2")
  asteroide = Asteroides(img: "Asteroid2")
  break
default :
  break
}

如果您在switch case中声明var,则仅在该case中可用。

答案 1 :(得分:0)

您的情况无需切换,可以使用字符串插值:

class GameScene : SKScene
{
    private let planet : Planet
    private let bullet : Bullet
    private let background : Background
    private let asteroide : Asteroides
    convenience init(lvlSelected : Int){

        let level = 1 ... 2 ~= lvlSelected ? "\(lvlSelected)" : ""
        planet = Planet(imageName: "Planet\(level)")
        bullet = Bullet (imageName: "Bullet\(level)")
        background = Background(imageName: "Background\(level)")
        asteroid = Asteroides(img: "Asteroid\(level)")
        self.init() //whatever init you use here
    }
}

~=是模式匹配运算符,#a ... #b表示a和b之间的数字范围,包括a和b,因此您要询问数字是否在一定范围内

(condition) ? (statement 1) : (statement 2)是三元运算符,如果条件为真,则为语句1否则为语句2

"\(var)"是字符串内插,告诉字符串将var的值注入字符串