Hello, I have been trying to animate using an NSTimer and I've set up my code like this:
- (IBAction)startClick:(id)sender{
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(.1/25.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
}
- (void)tick{
[self animatePig];
}
- (void)animatePig{
UIImage *pigImage1 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0004.png" ofType:nil] ];
UIImage *pigImage2 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0005.png" ofType:nil] ];
UIImage *pigImage3 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0006.png" ofType:nil] ];
UIImage *pigImage4 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0007.png" ofType:nil] ];
UIImage *pigImage5 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0008.png" ofType:nil] ];
UIImage *pigImage6 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0009.png" ofType:nil] ];
UIImage *pigImage7 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0010.png" ofType:nil] ];
UIImage *pigImage8 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0011.png" ofType:nil] ];
UIImage *pigImage9 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0012.png" ofType:nil] ];
UIImage *pigImage10 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0013.png" ofType:nil] ];
UIImage *pigImage11 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0014.png" ofType:nil] ];
UIImage *pigImage12 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"gordonapple0015.png" ofType:nil] ];
if(gordon.image == pigImage1)
gordon.image = pigImage2;
else if(gordon.image == pigImage2)
gordon.image = pigImage3;
else if(gordon.image == pigImage3)
gordon.image = pigImage4;
else if(gordon.image == pigImage4)
gordon.image = pigImage5;
else if(gordon.image == pigImage6)
gordon.image = pigImage7;
else if(gordon.image == pigImage7)
gordon.image = pigImage8;
else if(gordon.image == pigImage8)
gordon.image = pigImage9;
else if(gordon.image == pigImage9)
gordon.image = pigImage10;
else if(gordon.image == pigImage10)
gordon.image = pigImage11;
else if(gordon.image == pigImage11)
gordon.image = pigImage12;
else
gordon.image = pigImage12;
}
Does anyone know why this only animates the first frame? The problem's probably very simple to fix, i'm just too much of a noob in iPhone development to notice. ----------Edit----------
Okay, I have got my NSTimer to work correctly, and my next step was to stop my animation repeating over and over, so I invalidated my timer like this:
[animationTimer invalidate];
And now, when the timer is called again it doesn't work, so how would I validate it again? (re-instantiate it I think)
(BTW I Have set up another SO question for this)
My code as of now:
- (IBAction)startClick:(id)sender{
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick) userInfo:nil repeats:YES];
} - (void)tick{ [self animatePig]; } - (void)animatePig{
UIImage *pigImage1=[UIImage imageNamed:@"gordonapple0004.png"];
UIImage *pigImage2=[UIImage imageNamed:@"gordonapple0005.png"];
UIImage *pigImage3=[UIImage imageNamed:@"gordonapple0006.png"];
UIImage *pigImage4=[UIImage imageNamed:@"gordonapple0007.png"];
UIImage *pigImage5=[UIImage imageNamed:@"gordonapple0008.png"];
UIImage *pigImage6=[UIImage imageNamed:@"gordonapple0009.png"];
UIImage *pigImage7=[UIImage imageNamed:@"gordonapple0010.png"];
UIImage *pigImage8=[UIImage imageNamed:@"gordonapple0011.png"];
UIImage *pigImage9=[UIImage imageNamed:@"gordonapple0012.png"];
UIImage *pigImage10=[UIImage imageNamed:@"gordonapple0013.png"];
UIImage *pigImage11=[UIImage imageNamed:@"gordonapple0014.png"];
UIImage *pigImage12=[UIImage imageNamed:@"gordonapple0015.png"];
UIImage *pigImage13=[UIImage imageNamed:@"gordonapple0016.png"];
if(gordon.image == pigImage1)
gordon.image = pigImage2;
else if(gordon.image == pigImage2)
gordon.image = pigImage3;
else if(gordon.image == pigImage3)
gordon.image = pigImage4;
else if(gordon.image == pigImage4)
gordon.image = pigImage5;
else if(gordon.image == pigImage5)
gordon.image = pigImage6;
else if(gordon.image == pigImage6)
gordon.image = pigImage7;
else if(gordon.image == pigImage7)
gordon.image = pigImage8;
else if(gordon.image == pigImage8)
gordon.image = pigImage9;
else if(gordon.image == pigImage9)
gordon.image = pigImage10;
else if(gordon.image == pigImage10)
gordon.image = pigImage11;
else if(gordon.image == pigImage11)
[animationTimer invalidate];
else
gordon.image = pigImage1;
}