bernard langue
bernard langue

Reputation: 147

Problem with uiimageView animation: animation stops

Here is my code:

-(void) createNewBall {

  UIImage * image = [UIImage imageNamed:@"bulle_03.png"];
  bulleBouge = [[UIImageView alloc] initWithImage:image];

  [bulleBouge setCenter:[self randomPointSquare]];
  [[self view] addSubview:bulleBouge];

}

-(void)moveTheBall{

  bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);

}

createNewBall is called every two seconds. My problem is that every bulleBouge that is created stops moving after two seconds. I don't know why.

How can I solve this please?

Upvotes: 0

Views: 335

Answers (2)

Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

Cyprian is correct. In a simpler form, you're creating a new bulleBouge every two seconds, under the same variable. The program already has one, but since you told it to make a new one under the same ivar it forgets the old one, and thus doesn't move it. You need an array so that each ball can be remembered separately, and thus moved separately as seen in the example code that he posted.

Upvotes: 0

Cyprian
Cyprian

Reputation: 9453

It stops moving b/c u are initializing new bulleBouge every two seconds. You are also leakin memory since you never releasy it before assigning new value to it. so what happens is that after u create the imageView you only keep the reference to the lasts instance, hence only the last one is changing position. To fix this store all your new uiImageViews in an array and move them randomly after two seconds.

-(void) createNewBall {

  UIImage * image = [UIImage imageNamed:@"bulle_03.png"];
  UIImageView *bulleBouge = [[UIImageView alloc] initWithImage:image];
  [bulleBouge setCenter:[self randomPointSquare]];

  [bulleBougeArray addObject:bulleBouge];

  [[self view] addSubview:bulleBouge];

  [bulleBouge release];

}

-(void)moveTheBall{

  for(int i=0; i< [bulleBougeArray count];i++){
    UIImageView *bulleBouge = [bulleBougeArray objectAtIndex:i];
    bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
   }

}

Upvotes: 3

Related Questions