基本上我想要做的是编写一个计数器,每当两个对象(Wall和amp; Player)发生碰撞时加一个计数器。这是我的全部代码:
var PlayerTimer : NSTimer!
@IBOutlet var Wall: UIImageView!
@IBOutlet var Player: UIImageView!
@IBOutlet var CountLabel: UILabel!
var CountNumber : Int!
func Add() {
CountNumber = CountNumber + 1
CountLabel.text = "\(CountNumber)"
}
func PlayerMoving() {
Player.center = CGPointMake(Player.center.x, Player.center.y + 1)
if (CGRectIntersectsRect(Player.frame, Wall.frame)){
Add()
}
}
override func viewDidLoad() {
super.viewDidLoad()
CountNumber = 0
PlayerTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("PlayerMoving"), userInfo: nil, repeats: true)
// Do any additional setup after loading the view, typically from a override nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
但接下来发生的事情是,在玩家移动并撞到墙壁后,会添加1,但视图会重置。例如,当玩家击中墙壁并添加1时,玩家返回到视图顶部的初始位置。我想要发生的是让我的玩家在碰撞后继续朝着同一方向坠落。感谢。