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