我正在尝试为我的小鸟风格游戏添加一个简单的关卡。我想方设法使用2个不同的场景,但更喜欢它在同一个场景中。我只是希望一旦达到15分,墙壁会更频繁地出现!这是我的代码
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if gameStarted == false{
gameStarted = true
body.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])
body.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
body.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 68))
run(flap)
}
我尝试添加一个if语句,之前是if语句,如
如果得分= 20 {
但这只会延迟施加冲动,直到玩家获得20分。
有没有办法可以说
if score is between 0 and 20 then run my first spawning action
然后另一个说法得分&gt; 20运行我的第二次产卵动作会产生较慢的产卵时间吗?
希望这是有道理的!感谢
答案 0 :(得分:1)
您可以将spawnDelayForever
逻辑移动到一个函数中并以2秒延迟作为参数调用它,然后当游戏分数达到20时,再次调用spawnDelayForever
函数传递5作为输入参数。您需要使用密钥添加操作。
self.run(spawnDelayForever(withDelay: 2.0), withKey: "spawnDelayForever")
然后在再次添加之前删除正在运行的操作。
self.removeAction(forKey: "spawnDelayForever")
self.run(spawnDelayForever(withDelay: 5.0), withKey: "spawnDelayForever")
您的代码可能看起来像这样。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if gameStarted == false {
gameStarted = true
body.physicsBody?.affectedByGravity = true
self.run(spawnDelayForever(withDelay: 2.0), withKey: "spawnDelayForever")
// other code...
} else if score > 20 && spawnDelayApplied == false {
self.removeAction(forKey: "spawnDelayForever")
self.run(spawnDelayForever(withDelay: 5.0), withKey: "spawnDelayForever")
spawnDelayApplied = true
}
}
func spawnDelayForever(withDelay sec: TimeInterval) -> SKAction {
let spawn = SKAction.run({
() in
self.createWalls()
})
let delay = SKAction.wait(forDuration: sec)
let spawnDelay = SKAction.sequence([delay, spawn])
return SKAction.repeatForever(spawnDelay)
}