这可能有点令人困惑。
这是我的覆盖功能ATM,我的墙每2秒产生一次。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if gameStarted == false{
gameStarted = true
mexican.physicsBody?.affectedByGravity = true
let spawn = SKAction.run({
() in
self.createWalls()
})
let delay = SKAction.wait(forDuration: 2.0)
let spawnDelay = SKAction.sequence([delay, spawn])
let spawnDelayForever = SKAction.repeatForever(spawnDelay)
self.run(spawnDelayForever)
let distance = CGFloat(self.frame.width + wallPair.frame.width)
let movePipes = SKAction.moveBy(x: -distance - 50, y: 0, duration: TimeInterval( 0.01 * distance))
let removePipes = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePipes,removePipes])
mexican.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
mexican.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 65))
我找到了一个教程,其中墙的生成持续时间更快,但我不确定如何将它合并到我的代码中,因为他们使用了新的功能。
我的围墙正在覆盖功能中产生,这使我在添加if语句时很难调用它。
func startNewLevel(){
levelNumber += 1
if self.actionForKey("spawningEnemies") != nil{
self.removeActionForKey("spawningEnemies")
}
var levelDuration = timeInterval()
switch levelNumber {
case 1: levelDuration = 2.0
case 2: levelDuration = 1.8
case 3: levelDuration = 1.6
default: levelDuration = 0.5
print("Cannot find level into")
}
let spawn = SKAction.run(spawnEnemy)
let waitToSpawn = SKAction.waitForDuration(levelDuration)
let spawnSequence = SKAction.sequence([waitToSpawn, spawn])
let spawnForever = SKAction.repeatActionForever(spawnSequence)
self.runAction(spawnForever, withKey: "spawningEnemies")
这是我将调用函数startNewLevel()的地方,但是我的墙生成是在override func的touchBegan中设置的。
func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == physicsCategory.bird && secondBody.categoryBitMask == physicsCategory.wall{
score += 1
scoreLabel.text = "\(score)"
firstBody.node?.removeFromParent()
if score == 10 || score == 15 || score == 20{
startNewLevel()
if score > UserDefaults().integer(forKey: "HIGHSCORE") {
saveHighScore()
}
}
else if firstBody.categoryBitMask == physicsCategory.bird && secondBody.categoryBitMask == physicsCategory.wall{
score += 1
scoreLabel.text = "\(score)"
secondBody.node?.removeFromParent()
if score > UserDefaults().integer(forKey: "HIGHSCORE") {
saveHighScore()
}
}
这可能真的令人困惑,但如果有人得到我HOLA,你将成为我在地球上最喜欢的人。