【发布时间】:2014-10-17 00:39:40
【问题描述】:
我正在构建一个 Sprite Kit 游戏,玩家在按下屏幕时会发射一个粒子。如何限制触摸输入(假设每秒 2 个可识别的触摸输入),以使玩家不能只是快速点击屏幕来获得无限次射击?
【问题讨论】:
标签: ios objective-c touch sprite-kit particles
我正在构建一个 Sprite Kit 游戏,玩家在按下屏幕时会发射一个粒子。如何限制触摸输入(假设每秒 2 个可识别的触摸输入),以使玩家不能只是快速点击屏幕来获得无限次射击?
【问题讨论】:
标签: ios objective-c touch sprite-kit particles
另一种解决方案: 创建一个 BOOL(我更喜欢自己使用属性,所以):
@property (nonatomic, assign) BOOL touchEnabled;
在场景的初始化中将其设置为 YES。那么从此就相当简单了:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.touchEnabled){
self.touchEnabled = NO;
[self shootParticle];
[self performSelector:@selector(enableTouch) withObject:nil afterDelay:2.0];
}
...
- (void)shootParticle{
// whatever...
}
- (void)enableTouch{
self.touchEnabled = YES;
}
【讨论】:
多种可能性之一:
@interface YourSceneName (){
int _amountBullets; //increase every time you shot and just shoot when _fireStop = NO
BOOL _fireStop; // init as NO at start
BOOL _needStartTime; // init as YES at start
CFTimeInterval _startTime;
}
@end
-(void)update:(CFTimeInterval)currentTime {
//set starttime
if(_needStartTime){
_startTime = currentTime;
_needStartTime = NO;
}
//timeinterval if 2 seconds, renew everything
if(currentTime - _startTime > 2){
_startTime = currentTime;
_amountBullets = 0
_fireStop = NO;
}
//set firestop to yes, method should be executed
if(_amountBullets = 2){
_fireStop = YES;
}
}
我是 SpriteKit 的新手,但这应该可以。我敢打赌还有更好的可能性。我也没有测试代码。它向您展示了如何做到这一点的逻辑,希望我能提供帮助。 这是一个很棒的Tutorial,用于在 SpriteKit 中处理时间。
【讨论】: