我使用swift在xcode 7中编写游戏,这是一个无尽的游戏,你可以避免掉落物体。我有一个NSTimer:
@IBAction func startNow(sender: UIButton) {
player.center.x = 160
explosion.center.x = 1000
explosion.center.y = 1000
for f in r {
random = Int(arc4random_uniform(350))
f.image = UIImage(named: "blueSQ1")
if random == 20 {
f.image = UIImage(named: "moneyRain")
}
}
random1 = Int(arc4random_uniform(5))
if random1 <= 3 {
r10.image = UIImage(named: "blackSQ1")
}
slider.value = Float(player.center.x + 122)
shieldImg.image = UIImage(named: "nil")
ad = 1.5
moveDown.invalidate()
speed1 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed2 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed3 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed4 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed5 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed6 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed7 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed8 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed9 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed10 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed11 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed12 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed13 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed14 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
speed15 = ad + CGFloat(Float(arc4random()) / Float(UINT32_MAX))
for x in r {
x.center.y = 55
x.center.x = CGFloat(20+arc4random_uniform(285))
}
moveDown = NSTimer.scheduledTimerWithTimeInterval(0.015, target:self, selector: Selector("updatePos"), userInfo: nil, repeats: true)
timeyWimey = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector: Selector("incrementTime"), userInfo: nil, repeats: true)
checkShield = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector: Selector("ifShielded"), userInfo: nil, repeats: true)
}
func incrementTime() {
timer+=0.1
scoreProgress.setProgress(Float(timer/highscore), animated: false)
//time.text = "Time: \(timer)"
}
func updatePos() {
start.center.y = 10000
startOut.center.y = 10000
coinsLabel.center.y = 10000
buyShieldOut.center.y = 10000
ifDone()
r1.center.y += speed1
r2.center.y += speed2
r3.center.y += speed3
r4.center.y += speed4
r5.center.y += speed5
r6.center.y += speed6
r7.center.y += speed7
r8.center.y += speed8
r9.center.y += speed9
r10.center.y += speed10
r11.center.y += speed11
r12.center.y += speed12
r13.center.y += speed13
r14.center.y += speed14
r15.center.y += speed15
for q in r {
if ((q.center.x)+3 > player.center.x - 15) && ((q.center.x)-3 < player.center.x + 15) && ((q.center.y)+3 > player.center.y - 15) && ((q.center.y)-3 < player.center.y + 15)
{
if (shield == 0) && ((q.image == UIImage(named: "blueSQ1")) || (q.image == UIImage(named: "blackSQ1")))
{
explosion.center.y = 10000
explosion.center.x = 10000
start.text = "RESTART"
start.center.y = 289
timeyWimey.invalidate()
y = Float(round(10*timer)/10)
timeOut.text = "Previous time: \(y)"
coins += Int(y*10)
coinsLabel.text = "Coins: \(coins)"
if y > highscore {
highscore = y
}
highTime.text = "Best time: \(highscore)"
startOut.center.y = 289
coinsLabel.center.y = 130
buyShieldOut.center.y = 167
moveDown.invalidate()
timer = 0
}
if shield == 1 && q.image == UIImage(named: "blueSQ1")
{
activated = true
}
if q.image == UIImage(named: "moneyRain") {
coins += 500
q.image = UIImage(named: "whiteSQ")
}
if q.image == UIImage(named: "green2") {
}
}
}
if ((explosion.center.x)+18 > player.center.x - 15) && ((explosion.center.x)-18 < player.center.x + 15) {
if shield == 1
{
shield = 0
activated = false
}
start.text = "RESTART"
start.center.y = 289
timeyWimey.invalidate()
y = Float(round(10*timer)/10)
timeOut.text = "Previous time: \(y)"
coins += Int(y*10)
coinsLabel.text = "Coins: \(coins)"
if y > highscore {
highscore = y
}
highTime.text = "Best time: \(highscore)"
startOut.center.y = 289
coinsLabel.center.y = 130
buyShieldOut.center.y = 167
moveDown.invalidate()
explosion.center = player.center
timer = 0
}
}
这很好用。但是我已经注释掉time.text行,因为当它处于活动状态时,坠落的物体会反复返回到屏幕顶部并再次开始下降,然后再次快速返回到屏幕顶部等等 - 一小部分一秒。这似乎毫无意义。任何帮助将不胜感激。