views:

252

answers:

1

I am making a simple game app where you press a left/right button, and a UIImageView moves and animates. Here is the problem - I want the UIImageView to stop moving when it touches


self.view.bounds.size.width

How do I do this?

Here is the existing code :


    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == rightbutton){
 NSLog(@"touch rightbutton");
 player.animationImages = [NSArray arrayWithObjects:
         [UIImage imageNamed:@"linkr1.png"],
         [UIImage imageNamed:@"linkr2.png"],
         [UIImage imageNamed:@"linkr3.png"],
         [UIImage imageNamed:@"linkr4.png"],
         [UIImage imageNamed:@"linkr5.png"],
         [UIImage imageNamed:@"linkr6.png"],
         [UIImage imageNamed:@"linkr7.png"],
         [UIImage imageNamed:@"linkr8.png"],
         nil];
 player.animationRepeatCount = 0;
 [self.view addSubview:player];
 [player startAnimating];  
 rightbuttontimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rightrepeater) userInfo:nil repeats:YES];
}
if([touch view] == leftbutton){
 NSLog(@"touch leftbutton");
 player.animationImages = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"linkr1l.png"],
        [UIImage imageNamed:@"linkr2l.png"],
        [UIImage imageNamed:@"linkr3l.png"],
        [UIImage imageNamed:@"linkr4l.png"],
        [UIImage imageNamed:@"linkr5l.png"],
        [UIImage imageNamed:@"linkr6l.png"],
        [UIImage imageNamed:@"linkr7l.png"],
        [UIImage imageNamed:@"linkr8l.png"],
        nil];
 player.animationRepeatCount = 0;
 [self.view addSubview:player];
 [player startAnimating]; 
 leftbuttontimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(leftrepeater) userInfo:nil repeats:YES];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [[event allTouches] anyObject];
 if([touch view] == rightbutton){
  NSLog(@"let go of rightbutton");
  [rightbuttontimer invalidate];
  [player stopAnimating];
 }
  if([touch view] == leftbutton){
  NSLog(@"let go of leftbutton");
  [leftbuttontimer invalidate];
   [player stopAnimating];
 }
}

- (void)rightrepeater {

 CGPoint center = (CGPoint)[player center]; 
 center.x += 10; 
 [player setCenter:center];

}

- (void)leftrepeater {

 CGPoint center = (CGPoint)[player center]; 
 center.x += -10; 
 [player setCenter:center];

}

Any help will be appreciated. Thanks!

+1  A: 

In your rightRepeater method:

- (void)rightrepeater {
    CGPoint center = (CGPoint)[player center];
    center.x += 10;
    CGFloat rightEdge = center.x + [player width]/2;
    if (rightEdge > view.bounds.size.width) {
        center.x -= (rightEdge - view.bounds.size.width);
    }
    [player setCenter:center];
}

And, similarly in your leftRepeater method:

- (void)leftrepeater {
    CGPoint center = (CGPoint)[player center]; 
    center.x -= 10;
    CGFloat leftEdge = center.x - [player width]/2;
    if (leftEdge < 0) {
        center.x -= leftEdge;
    }
    [player setCenter:center];
}

Note that I used [player width] to obtain the width of each player. I'm not sure if such a method exists, but it should be enough to illustrate the concept.

e.James
Now XCode gives me the error : invalid operands to binary / (have 'id' and 'int') twice
Flafla2
That's probably because there is no `-width()` method on your players. See the note at the very end of my post. I'm not sure what to use for player width. I suppose you could hard code it if you knew how wide they were ahead of time.
e.James
Oh, sorry about that! the width of the picture was 91 px, so I replaced [player width]/2 with 45.5. Thanks!
Flafla2
You are welcome. Does it work now?
e.James
Yep! It worked perfectly!
Flafla2