我一直在学习如何在Swift中编写代码并遵循一些教程。在按照教程制作类似Z字形的游戏时,我遇到了一个我不知道如何解决的错误。
if (y > 720) {
low = true
}
上面的代码给了我一个错误Expected Declaration
。
以下是我的其余代码
import UIKit
class ViewController: UIViewController {
@IBAction func Play(sender: AnyObject) {
TapsValid = true
BallChange = true
self.GameOver.hidden = true
self.Retry.hidden = true
self.Ball.hidden = false
self.Logo.hidden = true
self.Play.hidden = true
self.ScoreBoard.hidden = true
self.Ball.center.x = 178.0
self.Ball.center.y = 390.0
self.Pillar1.center = CGPointMake(175.0 , 436.0)
self.Pillar2.center = CGPointMake(214.0 , 407.0)
timer = NSTimer.scheduledTimerWithTimeInterval(0.045, target: self , selector: Selector("movement"), userInfo: nil, repeats: true)
self.Pillar3.center = pillarPlacement(Pillar2.center.x, y: Pillar2.center.y)
self.Pillar4.center = pillarPlacement(Pillar3.center.x, y: Pillar3.center.y)
self.Pillar5.center = pillarPlacement(Pillar4.center.x, y: Pillar4.center.y)
self.Pillar6.center = pillarPlacement(Pillar5.center.x, y: Pillar5.center.y)
self.Pillar7.center = pillarPlacement(Pillar6.center.x, y: Pillar6.center.y)
self.Pillar8.center = pillarPlacement(Pillar7.center.x, y: Pillar7.center.y)
self.Pillar9.center = pillarPlacement(Pillar8.center.x, y: Pillar8.center.y)
self.Pillar10.center = pillarPlacement(Pillar9.center.x, y: Pillar9.center.y)
self.Pillar1.hidden = false
self.Pillar2.hidden = false
self.Pillar3.hidden = false
self.Pillar4.hidden = false
self.Pillar5.hidden = false
self.Pillar6.hidden = false
self.Pillar7.hidden = false
self.Pillar8.hidden = false
self.Pillar9.hidden = false
self.Pillar10.hidden = false
self.PillarTop.hidden = false
self.PillarTop2.hidden = false
self.PillarTop3.hidden = false
}
@IBAction func Retry(sender: AnyObject) {
}
@IBOutlet weak var Pillar10: UIImageView!
@IBOutlet weak var Pillar9: UIImageView!
@IBOutlet weak var Pillar8: UIImageView!
@IBOutlet weak var Pillar7: UIImageView!
@IBOutlet weak var Pillar6: UIImageView!
@IBOutlet weak var Pillar5: UIImageView!
@IBOutlet weak var Pillar4: UIImageView!
@IBOutlet weak var Pillar3: UIImageView!
@IBOutlet weak var Pillar2: UIImageView!
@IBOutlet weak var Pillar1: UIImageView!
@IBOutlet weak var PillarTop3: UIImageView!
@IBOutlet weak var PillarTop2: UIImageView!
@IBOutlet weak var PillarTop: UIImageView!
@IBOutlet weak var Play: UIButton!
@IBOutlet weak var ScoreBoard: UIImageView!
@IBOutlet weak var Retry: UIButton!
@IBOutlet weak var Logo: UIImageView!
@IBOutlet weak var GameOver: UIImageView!
@IBOutlet weak var Ball: UIImageView!
var timer = NSTimer()
var TapsValid:Bool?
var BallRight:Bool?
var BallChange:Bool?
override func viewDidLoad() {
super.viewDidLoad()
self.GameOver.hidden = true
self.Retry.hidden = true
self.Ball.hidden = true
self.Logo.hidden = false
self.Play.hidden = false
self.ScoreBoard.hidden = true
self.Pillar1.hidden = true
self.Pillar2.hidden = true
self.Pillar3.hidden = true
self.Pillar4.hidden = true
self.Pillar5.hidden = true
self.Pillar6.hidden = true
self.Pillar7.hidden = true
self.Pillar8.hidden = true
self.Pillar9.hidden = true
self.Pillar10.hidden = true
self.PillarTop.hidden = true
self.PillarTop2.hidden = true
self.PillarTop3.hidden = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if TapsValid == true {
if BallRight == true {
BallChange = false
} else {
BallChange = true
}
}
}
func movement() {
if BallChange == false{
BallRight = false
} else {
BallRight = true
}
if BallRight == true {
Ball.center.x += 6.5
Ball.center.y -= 0.5
} else {
Ball.center.x -= 6.5
Ball.center.y -= 0.5
}
Ball.center.y += 0.5
Pillar1.center.y += 5
Pillar2.center.y += 5
Pillar3.center.y += 5
Pillar4.center.y += 5
Pillar5.center.y += 5
Pillar6.center.y += 5
Pillar7.center.y += 5
Pillar8.center.y += 5
Pillar9.center.y += 5
Pillar10.center.y += 5
}
func movePillarUp(floatx: CGFloat, floaty: CGFloat, pillarNumber: Int) -> (CGPoint) {
var center = CGPointMake(floatx, floaty)
if checkPillarPosition(floaty) == true {
switch pillarNumber{
case 1:
center = pillarPlacement(self.Pillar10.center.x, y: self.Pillar10.center.y)
break
case 2:
center = pillarPlacement(self.Pillar1.center.x, y: self.Pillar2.center.y)
break
case 3:
center = pillarPlacement(self.Pillar2.center.x, y: self.Pillar2.center.y)
break
case 4:
center = pillarPlacement(self.Pillar3.center.x, y: self.Pillar3.center.y)
break
case 5:
center = pillarPlacement(self.Pillar4.center.x, y: self.Pillar4.center.y)
break
case 6:
center = pillarPlacement(self.Pillar5.center.x, y: self.Pillar5.center.y)
break
case 7:
center = pillarPlacement(self.Pillar6.center.x, y: self.Pillar6.center.y)
break
case 8:
center = pillarPlacement(self.Pillar7.center.x, y: self.Pillar7.center.y)
break
case 9:
center = pillarPlacement(self.Pillar8.center.x, y: self.Pillar8.center.y)
break
case 10:
center = pillarPlacement(self.Pillar9.center.x, y: self.Pillar9.center.y)
break
default:
break
}
}
return(center)
}
func checkPillarPosition(y: CGFloat) -> Bool {
var low = false}
if (y > 720) {
low = true
}
//This function moves pillars arround randomally
func pillarPlacement(x: CGFloat,y: CGFloat) -> (CGPoint) {
var PillarNewX: CGFloat
var PillarNewY: CGFloat
let random: Int = Int(arc4random() % 2)
if random == 1 {
PillarNewX = x + 39
PillarNewY = y - 29
if PillarNewX >= 319 {
PillarNewX = x - 40
PillarNewY = y - 30
}
} else {
PillarNewX = x - 40
PillarNewY = y - 30
if PillarNewX <= 17 {
PillarNewX = x + 39
PillarNewY = y - 29
}
}
let NewPillarCenter = CGPointMake(PillarNewX, PillarNewY)
return(NewPillarCenter)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
if语句不在函数中,我想你可能想把它添加到checkPillarPosition中。更新后的代码如下所示:
func checkPillarPosition(y: CGFloat) -> Bool {
var low = false
if (y > 720) {
low = true
return true
} else {
return false
}
}
这会删除您的错误并解决您的问题。希望它有所帮助!