I'm just trying to schedule an update method once in ccTouchesMoved, but when I add this
if (isRotating == YES)
{
gTimer = 20;
[self schedule:@selector(g:) interval:1];
}
in to ccTouchesMove it calls it every time the touch is moved. Is there a way to keep it in ccTouchesMoved and have it called just once?
isRotating
is set to YES in ccTouchesMoved
.
Code:
- (void)g:(ccTime)delta
{
gTimer--;
if (gTimer == 0 && isRotating == YES)
{
[self unschedule:@selector(g:)];
}
}
All I want 开发者_开发知识库to do is rotate a sprite for a fixed interval then remove it when the timer is 0, but I only want to remove it if the touch is currently rotating the sprite.
Here is where pausing/resuming the timer comes in. If the player starts rotating the sprite then lifts their finger, the timer should pause and when they touch/rotate the sprite again the timer should resume.
If my explaining isn't clear please feel free to ask me to elaborate.
Timer methods:
In .h
ccTimer gTimer;
In .m
- (void)g:(ccTime)delta
{
gTimer--;
if (gTimer == 0 && isRotating == YES)
{
[self unschedule:@selector(g:)];
}
}
Also, I set the timer with
if (isRotating == YES)
{
gTimer = 20;
[self schedule:@selector(g:) interval:1];
}
Any help is greatly appreciated!
Put a BOOL touched in your class.. and float myTimer as well..
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
touched = YES;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
isRotating = YES;
if(touched && isRotating)
{
gTimer = 20;
[self schedule:@selector(g:) interval:0.03];
}
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
touched = NO;
[self unschedule:@selector(g:)];
}
- (void)g:(ccTime)delta
{
myTimer +=delta;
if (myTimer >= gTimer && isRotating == YES)
{
[self unschedule:@selector(g:)];
}
}
To pause the timer I just used
[self unschedule:@selector(g:)];
then to resume it I used
[self schedule:@selector(g:)];
Also, for scheduling update in ccTouchesMoved I just checked to see if the finger and stopped moving without being lifted.
精彩评论