views:

1134

answers:

3

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;

}

+1  A: 

There's no good way to know without seeing more code from you. How do you store "gordon"? What type is it? presumably some type of UIImageView?

However, your real problem (if I may go to a higher level) is your entire structure. Try something more like this:

// Interface file

NSMutableArray *pigImages;
int pigImageIndex;

// Implementation file

const int firstImageIndex = 4;
const int lastImageIndex = 15;
const int imageCount = (lastImageIndex + 1 - firstImageIndex);

- (void) initPigImages
{
    pigImages = [[NSMutableArray alloc] initWithCapacity: imageCount];
    for (int i = firstImageIndex; i <= lastImageIndex; ++i)
    {
        [pigImages addObject: [self makePigImageWithIndex: i]];
    }
    pigImageIndex = 0;
}

- (UIImage*) makePigImageWithIndex: (int) imageIndex
{
    NSString *imageName = [NSString stringWithFormat: @"gordonapple%4d.png", imageIndex]; // Formatting may be a little off.
    return [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: imageName ofType: nil]]];
}

- (void )animatePig
{
    if (pigImageIndex < imageCount)
    {
        gordon.image = pigImages[pigImageIndex++];
    }
}
Amagrammer
Yes, gordon is a UIImageView, and is moveable by touchesMoved.
+2  A: 

You're going about it the hard way.

  • Create a UIImageView and put it on the screen.
  • Load your images into an NSArray.
  • Set the UIImageView's animationImages property to the array.
  • You can adjust the animationDuration and animationRepeatCount properties as well.
  • When user presses the start button, call the startAnimating method of the image view.

You don't need the timer nor the manual frame advancing code.

Ramin
Hmm, I see, well, i have set it up now in a way that it works, however, i only need my timer to animate my images once, with out it repeating over and over again, so Iv'e invalidated my timer, but I'm having trouble instantiating it again, any ideas on how i'd do that?
Once invalidated, the timer is gone. Throw it away and start another one. Make sure it's properly released if it's stored as an instance variable.
Ramin
A: 

To restart your timer you can use -[NSRunLoop addTimer:forMode:] or just release and create a new timer each time.

Daniel Dickison