【发布时间】:2014-02-12 02:46:55
【问题描述】:
我最近开始使用sprite-kit。我知道touchesBegan 只需轻按一下即可,但我可以使用什么东西来识别按住的触摸吗?
【问题讨论】:
标签: ios iphone ipad sprite-kit
我最近开始使用sprite-kit。我知道touchesBegan 只需轻按一下即可,但我可以使用什么东西来识别按住的触摸吗?
【问题讨论】:
标签: ios iphone ipad sprite-kit
如果你想实现射击之类的东西,那么你需要在touchesBegan方法中开始射击并在touchesEnded方法中停止射击:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self startShooting];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self stopShooting];
}
对于其他用途,您可以将UILongPressGestureRecognizer 添加到SKScene
【讨论】:
您也可以在更新方法中使用布尔值:
bool isTouching = false;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
isTouching = true;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
isTouching = false;
}
-(void)update:(CFTimeInterval)currentTime {
if(isTouching){
//shooting!
}
}
如果您愿意,您可以轻松地将您的方法组合到 isTouching 块中,并使用 touchesBegan 来同时瞄准子弹。
您无需担心计时器,因为只要 isTouching == true,update 方法就会继续执行代码块
【讨论】:
var isTouching = false
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
handleTouches(touches)
isTouching = true;
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
handleTouches(touches)
isTouching = false;
}
override func update(currentTime: NSTimeInterval) {
if isTouching{
//Shoot CODE!
}
}
【讨论】: