2016-11-09 43 views
1

我製作了一個不同級別的遊戲,但感覺它太大了。我用有限的知識創造了不同的層次,我認爲它可以更有效地編碼。我想根據當前的等級改變敵人的狀態。 這是我的代碼↓在Spritekit中更改多個級別的狀態

func addEnemy() { 
     if currentLevel == 1{ 
     let enemyGhost = SKSpriteNode(imageNamed: "enemy") 
     enemyGhost.setScale(random(min: 1.5, max: 2.5)) 
     enemyGhost.physicsBody = SKPhysicsBody(rectangleOf: enemyGhost.size) 
     enemyGhost.physicsBody?.isDynamic = true 
     enemyGhost.physicsBody?.categoryBitMask = PhysicsCategory.enemyGhost 
     enemyGhost.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile 
     enemyGhost.physicsBody?.collisionBitMask = PhysicsCategory.None 
     let actualY = random(min: enemyGhost.size.height/2, max: size.height - enemyGhost.size.height/2) 
     enemyGhost.position = CGPoint(x: size.width + enemyGhost.size.width/2, y: actualY) 
     addChild(enemyGhost) 
     let actualDuration = random(min: CGFloat(5.0), max: CGFloat(7.0)) 
     let actionMove = SKAction.move(to: CGPoint(x: -enemyGhost.size.width/2, y: actualY), duration: TimeInterval(actualDuration)) 
     let actionMoveDone = SKAction.removeFromParent() 
     let loseAction = SKAction.run() { 
     let reveal:SKTransition = SKTransition.flipHorizontal(withDuration: 0.5) 
     let gameOverScene = GameOverScene(size: self.size, won: false) 
     self.view?.presentScene(gameOverScene, transition: reveal) 
     } 
    enemyGhost.run(SKAction.sequence([actionMove, loseAction, actionMoveDone])) 
    } 
    else if currentLevel == 2 { 
     let enemyGhost = SKSpriteNode(imageNamed: "enemy") 
     enemyGhost.setScale(random(min: 1.0, max: 1.5)) 
     enemyGhost.physicsBody = SKPhysicsBody(rectangleOf: enemyGhost.size) 
     enemyGhost.physicsBody?.isDynamic = true 
     enemyGhost.physicsBody?.categoryBitMask = PhysicsCategory.enemyGhost 
     enemyGhost.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile 
     enemyGhost.physicsBody?.collisionBitMask = PhysicsCategory.None 
     let actualY = random(min: enemyGhost.size.height/2, max: size.height - enemyGhost.size.height/2) 
     enemyGhost.position = CGPoint(x: size.width + enemyGhost.size.width/2, y: actualY) 
     addChild(enemyGhost) 
     let actualDuration = random(min: CGFloat(3.0), max: CGFloat(5.0)) 
     let actionMove = SKAction.move(to: CGPoint(x: -enemyGhost.size.width/2, y: actualY), duration: TimeInterval(actualDuration)) 
     let actionMoveDone = SKAction.removeFromParent() 
     let loseAction = SKAction.run() { 
      let reveal:SKTransition = SKTransition.flipHorizontal(withDuration: 0.5) 
      let gameOverScene = GameOverScene(size: self.size, won: false) 
      self.view?.presentScene(gameOverScene, transition: reveal) 
     } 
     enemyGhost.run(SKAction.sequence([actionMove, loseAction, actionMoveDone])) 
     } 
    }else if currentLevel == 3 { 
     let enemyGhost = SKSpriteNode(imageNamed: "enemy") 
     enemyGhost.setScale(random(min: 0.5, max: 1.0)) 
     enemyGhost.physicsBody = SKPhysicsBody(rectangleOf: enemyGhost.size) 
     enemyGhost.physicsBody?.isDynamic = true 
     enemyGhost.physicsBody?.categoryBitMask = PhysicsCategory.enemyGhost 
     enemyGhost.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile 
     enemyGhost.physicsBody?.collisionBitMask = PhysicsCategory.None 
     let actualY = random(min: enemyGhost.size.height/2, max: size.height - enemyGhost.size.height/2) 
     enemyGhost.position = CGPoint(x: size.width + enemyGhost.size.width/2, y: actualY) 
     addChild(enemyGhost) 
     let actualDuration = random(min: CGFloat(2.0), max: CGFloat(3.0)) 
     let actionMove = SKAction.move(to: CGPoint(x: -enemyGhost.size.width/2, y: actualY), duration: TimeInterval(actualDuration)) 
     let actionMoveDone = SKAction.removeFromParent() 
     let loseAction = SKAction.run() { 
      let reveal:SKTransition = SKTransition.flipHorizontal(withDuration: 0.5) 
      let gameOverScene = GameOverScene(size: self.size, won: false) 
      self.view?.presentScene(gameOverScene, transition: reveal) 
     } 
     enemyGhost.run(SKAction.sequence([actionMove, loseAction, actionMoveDone])) 
     } 

回答

2

你的功能可以簡化,因爲它充滿了冗餘代碼:

func addEnemy() { 
     let enemyGhost = SKSpriteNode(imageNamed: "enemy") 
     enemyGhost.physicsBody = SKPhysicsBody(rectangleOf: enemyGhost.size) 
     enemyGhost.physicsBody?.isDynamic = true 
     enemyGhost.physicsBody?.categoryBitMask = PhysicsCategory.enemyGhost.rawValue 
     enemyGhost.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile.rawValue 
     enemyGhost.physicsBody?.collisionBitMask = PhysicsCategory.None.rawValue 
     let actualY = random(min: enemyGhost.size.height/2, max: size.height - enemyGhost.size.height/2) 
     enemyGhost.position = CGPoint(x: size.width + enemyGhost.size.width/2, y: actualY) 
     addChild(enemyGhost) 
     var actualDuration = random(min: CGFloat(5.0), max: CGFloat(7.0)) 
     switch currentLevel { 
     case 1: 
      enemyGhost.setScale(random(min: 1.5, max: 2.5)) 
     case 2: 
      enemyGhost.setScale(random(min: 1.0, max: 1.5)) 
      actualDuration = random(min: CGFloat(3.0), max: CGFloat(5.0)) 
     case 3: 
      enemyGhost.setScale(random(min: 0.5, max: 1.0)) 
      actualDuration = random(min: CGFloat(2.0), max: CGFloat(3.0)) 
     default: 
      break 
     } 
     let actionMove = SKAction.move(to: CGPoint(x: -enemyGhost.size.width/2, y: actualY), duration: TimeInterval(actualDuration)) 
     let actionMoveDone = SKAction.removeFromParent() 
     let loseAction = SKAction.run() { 
      let reveal:SKTransition = SKTransition.flipHorizontal(withDuration: 0.5) 
      let gameOverScene = GameOverScene(size: self.size, won: false) 
      self.view?.presentScene(gameOverScene, transition: reveal) 
     } 
     enemyGhost.run(SKAction.sequence([actionMove, loseAction, actionMoveDone])) 
} 

關於你的敵人的狀態還記得你可以使用userDatadocs)來存儲和檢索特定的性能,是這樣的:

enemyGhost.userData? = NSMutableDictionary() 
enemyGhost.userData = ["status":"aggressive"] 
print(enemyGhost.userData?["status"] ?? "none") 
1

嗯,我會開始創建敵方一個單獨的類,讓你不重複這麼多的初始化代碼。然後你可以創建一個EnemyType的枚舉類型,根據他是什麼類型的敵人或者他處於什麼級別,爲敵人提供可變信息。然後你根據他是什麼類型創建敵人,敵方物體已經沒有所有必要的信息。

值得注意的是,您的PhysicsCategory名稱有不同的情況「enemyGhost」vs「Projectile」。他們應該是相同的情況。

struct EnemyScale { 
    var minScale: CGFloat = 0 
    var maxScale: CGFloat = 0 
} 

enum EnemyType: Int { 

    case level1, level2, level3 

    var textureImage: String { 

     switch self { 

      case .level1: return "enemy" 
      case .level2: return "enemy" 
      case .level3: return "enemy" 

     } 
    } 

    var scale: EnemyScale { 

     switch self { 

      case .level1: return EnemyScale(minScale: 1.5, maxScale: 2.5) 
      case .level2: return EnemyScale(minScale: 1.0, maxScale: 1.5) 
      case .level3: return EnemyScale(minScale: 0.5, maxScale: 1.0) 

     } 
    } 
} 

class Enemy: SKSpriteNode { 

    init(type: EnemyType) { 

     let enemyTexture = SKTexture(imageNamed: type.textureImage) 

     super.init(texture: enemyTexture, color: .clear, size: enemyTexture.size()) 

     self.setScale(random(min: type.scaleMin, max: type.scaleMax)) 

     self.physicsBody = SKPhysicsBody(rectangleOf: self.size) 
     self.physicsBody?.isDynamic = true 
     self.physicsBody?.categoryBitMask = PhysicsCategory.enemyGhost 
     self.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile 
     self.physicsBody?.collisionBitMask = PhysicsCategory.None 
    } 
} 

func addEnemy() { 

    if currentLevel == 1 { 

     let enemyGhost = Enemy(type: .level1) 
     let actualY = random(min: enemyGhost.size.height/2, max: size.height - enemyGhost.size.height/2) 
     enemyGhost.position = CGPoint(x: size.width + enemyGhost.size.width/2, y: actualY) 
     self.addChild(enemyGhost) 

     let actualDuration = random(min: CGFloat(5.0), max: CGFloat(7.0)) 
     let actionMove = SKAction.move(to: CGPoint(x: -enemyGhost.size.width/2, y: actualY), duration: TimeInterval(actualDuration)) 
     let actionMoveDone = SKAction.removeFromParent() 
     let loseAction = SKAction.run() { 
      let reveal:SKTransition = SKTransition.flipHorizontal(withDuration: 0.5) 
      let gameOverScene = GameOverScene(size: self.size, won: false) 
      self.view?.presentScene(gameOverScene, transition: reveal) 
     } 
     enemyGhost.run(SKAction.sequence([actionMove, loseAction, actionMoveDone])) 
    } 
    else if currentLevel == 2 { 

     let enemyGhost = Enemy(type: .level2) 
     let actualY = random(min: enemyGhost.size.height/2, max: size.height - enemyGhost.size.height/2) 
     enemyGhost.position = CGPoint(x: size.width + enemyGhost.size.width/2, y: actualY) 
     self.addChild(enemyGhost) 

     let actualDuration = random(min: CGFloat(3.0), max: CGFloat(5.0)) 
     let actionMove = SKAction.move(to: CGPoint(x: -enemyGhost.size.width/2, y: actualY), duration: TimeInterval(actualDuration)) 
     let actionMoveDone = SKAction.removeFromParent() 
     let loseAction = SKAction.run() { 
      let reveal:SKTransition = SKTransition.flipHorizontal(withDuration: 0.5) 
      let gameOverScene = GameOverScene(size: self.size, won: false) 
      self.view?.presentScene(gameOverScene, transition: reveal) 
     } 
     enemyGhost.run(SKAction.sequence([actionMove, loseAction, actionMoveDone])) 
    } 
    else if currentLevel == 3 { 

     let enemyGhost = Enemy(type: .level3) 
     let actualY = random(min: enemyGhost.size.height/2, max: size.height - enemyGhost.size.height/2) 
     enemyGhost.position = CGPoint(x: size.width + enemyGhost.size.width/2, y: actualY) 
     self.addChild(enemyGhost) 

     let actualDuration = random(min: CGFloat(2.0), max: CGFloat(3.0)) 
     let actionMove = SKAction.move(to: CGPoint(x: -enemyGhost.size.width/2, y: actualY), duration: TimeInterval(actualDuration)) 
     let actionMoveDone = SKAction.removeFromParent() 
     let loseAction = SKAction.run() { 
      let reveal:SKTransition = SKTransition.flipHorizontal(withDuration: 0.5) 
      let gameOverScene = GameOverScene(size: self.size, won: false) 
      self.view?.presentScene(gameOverScene, transition: reveal) 
     } 
     enemyGhost.run(SKAction.sequence([actionMove, loseAction, actionMoveDone])) 
    } 
}