我正试图在游戏中无限滚动背景。当我尝试这样做时,我的应用程序崩溃了:
@IBAction func levelClicked(sender: AnyObject) {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenHeight = screenSize.height
let imageName = "levelBackground"
let image = UIImage(named: imageName)
imageView = UIImageView(image: image!)
imageView.frame = CGRect (x: 0, y: 0, width: 5500, height: screenHeight)
view.addSubview(imageView)
timer = NSTimer.scheduledTimerWithTimeInterval(0.5,
target: self,
selector: Selector("moveBackground"),
userInfo: nil,
repeats: true)
}
func moveBackground() {
imageView.frame = CGRect(x: imageView.frame.origin.x - 5,
y: imageView.origin.y,
width: 5500,
height: UIScreen.mainScreen().bounds.height)
}
这是我的崩溃日志:
2015-01-03 15:57:56.579 zombieSurvival [3395:708290] - [zombieSurvival.firstBuildingViewController moveBackground]:无法识别的选择器发送到实例0x15e7e150
2015-01-03 15:57:56.585 zombieSurvival [3395:708290] ***终止 应用程序由于未捕获的异常' NSInvalidArgumentException',原因: ' - [zombieSurvival.firstBuildingViewController moveBackground]: 无法识别的选择器发送到实例0x15e7e150'
***首先抛出调用堆栈:
(0x29dad5f7 0x374e7c77 0x29db2abd 0x29db0999 0x29ce1b88 0x2aac1a45 0x29d73f07 0x29d73a89 0x29d71cf3 0x29cbeb31 0x29cbe943 0x31077051 0x2d2b46f1 0xccc04 0xccc40 0x37a83aaf)
libc ++ abi.dylib:以未捕获的类型异常终止 NSException
答案 0 :(得分:1)
我不是斯威夫特的专家,但似乎这没有成功:
您将moveBackground
声明为levelClicked
内的函数,但在您的计时器上,您引用self
作为目标。类本身没有方法moveBackground
。它仅在您的函数levelClicked
中可见。
尝试将整个moveBackground
移出levelClicked
。
为了让moveBackground
有效,我想你必须给imageView
一个属性。
@IBAction func levelClicked(sender: AnyObject) {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenHeight = screenSize.height
let imageName = "levelBackground"
let image = UIImage(named: imageName)
self.imageView = UIImageView(image: image!)
self.imageView.frame = CGRect (x: 0, y: 0, width: 5500, height: screenHeight)
view.addSubview(self.imageView)
timer = NSTimer.scheduledTimerWithTimeInterval(0.5,
target: self,
selector: Selector("moveBackground"),
userInfo: nil,
repeats: true)
}
func moveBackground() {
self.imageView.frame = CGRect(x: self.imageView.frame.origin.x - 5,
y: self.imageView.origin.y,
width: 5500,
height: UIScreen.mainScreen().bounds.height)
}