Search code examples
iosobjective-ctouchsprite-kitparticles

How do I limit the touch input?


I am building a Sprite Kit game where the player shoots a particle whenever the screen is pressed. How can I limit the touch inputs (let's say 2 recognized touch inputs per second) so that the player can't just quickly tap the screen to get unlimited shots?


Solution

  • Another solution: Create a BOOL (I prefer to work with properties myself, so):

    @property (nonatomic, assign) BOOL touchEnabled;

    Set it to YES in the scene's init. Then it is fairly simple from thereon:

    - (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;
    }