-2

I'm trying to make a sort of free runner game. I make the character jump by setting an integer to 30 or so every time the screen is tapped, and moving the character up the screen using CGPointMake(). Then I have a timer start when the player taps the screen which pulls the character down until he's reached a certain point (y=260). Then the timer invalidates and the character's downward movement stops.

This goes on and on and works perfectly well until you tap on the screen before the character reaches y=260. This makes him jerk up and come speeding down very fast and he doesn't stop at y=260 like he should. Here's the code that I think is causing the problem:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    playerMoving = 33;

    gravity = [NSTimer scheduledTimerWithTimeInterval:.2 
                                               target:self 
                                             selector:@selector(playerJump) 
                                             userInfo:nil 
                                              repeats:YES];


}

Every time I double tap on the screen the timer gets activated again, even though it is already activated, and this is what I think is causing the problem.

Is there any way to not let the player tap on the screen a second time until the character has reached y=260 (so basically the player won't be able to double jump)?

jscs
  • 63,694
  • 13
  • 151
  • 195
Supert165
  • 9
  • 5
  • 1
    If you're just starting out, asking questions on Stack Overflow is not the place you need to be. You should find a good book or a series of online tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Aug 21 '14 at 00:33
  • well...thanks but the reason I was on here in the first place was because I can't really learn from books. I learn much better from hands on experience and that's why I was trying to make my own really simple app. People here have been very helpful people and have helped me out a lot but I guess some people are nicer than others. Thanks! – Supert165 Aug 21 '14 at 00:37
  • Any decent book -- such as the BNR books -- will give you hands-on experience from the very beginning. That's the nature of a coding tutorial. Further, SO is _not_ the place to get experience. It's a place to get specific answers to your concrete questions. – jscs Aug 21 '14 at 00:40

1 Answers1

0

Don't create the timer if it already exists. Nil out the variable when you're done with the timer, and check it before you create it again.

- (void)playerJump:(NSTimer *)tim
{
    // Do stuff...

    if( /* Done repeating */ ){
        [gravity invalidate];
        gravity = nil;
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   playerMoving = 33;
   if( !gravity ){
        gravity = [NSTimer scheduledTimerWithTimeInterval:.2 
                                                   target:self 
                                                 selector:@selector(playerJump:)
                                                 userInfo:nil 
                                                  repeats:YES];
   }
}
jscs
  • 63,694
  • 13
  • 151
  • 195
  • I think I know what you're doing but not sure. So here's what I did (edited it in the original) – Supert165 Aug 21 '14 at 00:07
  • 1
    I don't understand. Are you saying you were _already_ doing this? If not, please don't edit your question to incorporate my answer. Further, if your program is crashing, you need to include the error information. – jscs Aug 21 '14 at 00:17
  • No I wasn't already doing this but the code wouldn't fit in the comment so I didn't know what else to do. and here is the error info :2014-08-20 17:05:01.760 free Runner[17709:60b] -[ViewController playerJump]: unrecognized selector sent to instance 0x8c50390 2014-08-20 17:05:01.764 free Runner[17709:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController playerJump]: unrecognized selector sent to instance 0x8c50390' – Supert165 Aug 21 '14 at 00:22
  • http://stackoverflow.com/questions/11145333/nstimer-causes-unrecognized-selector-crash-when-it-fires – jscs Aug 21 '14 at 00:23
  • oookay. It's not the timer causing the crash it's the code you gave me because it was working perfectly fine before – Supert165 Aug 21 '14 at 00:25
  • yes but like I said before you edited my post I'm very new to Objective-C and don't understand half the stuff they're saying – Supert165 Aug 21 '14 at 00:29