views:

284

answers:

1

Hi, I am using the accelerometer to move a few UIImageViews around the screen, at the moment the accelerometer only works when a NSTimer is at 0, this is fine. I would also like to make the accelerometer stop again when a different function happens.

here is my code at the moment:

-(BOOL) accelerometerWorks {
 return time == 0;
}

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
 if(![self accelerometerWorks]) return;
 valueX = acceleration.x*25.5;



 int newX = (int)(ball.center.x +valueX);
 if (newX > 320-BALL_RADIUS)
  newX = 320-BALL_RADIUS;
 if (newX < 0+BALL_RADIUS)
  newX = 0+BALL_RADIUS;


 int XA = (int)(balloonbit1.center.x +valueX);
 if (XA > 320-BALL_RADIUS)
  XA = 320-BALL_RADIUS;
 if (XA < 0+BALL_RADIUS)
  XA = 0+BALL_RADIUS;


 int XB = (int)(balloonbit2.center.x +valueX);
 if (XB > 320-BALL_RADIUS)
  XB = 320-BALL_RADIUS;
 if (XB < 0+BALL_RADIUS)
  XB = 0+BALL_RADIUS;

 int XI = (int)(balloonbit3.center.x +valueX);
 if (XI > 320-BALL_RADIUS)
  XI = 320-BALL_RADIUS;
 if (XI < 0+BALL_RADIUS)
  XI = 0+BALL_RADIUS;

 int XJ = (int)(balloonbit4.center.x +valueX);
 if (XJ > 320-BALL_RADIUS)
  XJ = 320-BALL_RADIUS;
 if (XJ < 0+BALL_RADIUS)
  XJ = 0+BALL_RADIUS;


 int XE = (int)(balloonbit5.center.x +valueX);
 if (XE > 320-BALL_RADIUS)
  XE = 320-BALL_RADIUS;
 if (XE < 0+BALL_RADIUS)
  XE = 0+BALL_RADIUS;


 int XF = (int)(balloonbit6.center.x +valueX);
 if (XF > 320-BALL_RADIUS)
  XF = 320-BALL_RADIUS;
 if (XF < 0+BALL_RADIUS)
  XF = 0+BALL_RADIUS;


 int XH = (int)(balloonbit8.center.x +valueX);
 if (XH > 320-BALL_RADIUS)
  XH = 320-BALL_RADIUS;
 if (XH < 0+BALL_RADIUS)
  XH = 0+BALL_RADIUS;


 ball.center = CGPointMake (newX, 429);
 balloonbit1.center = CGPointMake (XA, 429);
 balloonbit2.center = CGPointMake (XB, 426);
 balloonbit3.center = CGPointMake (XI, 410);
 balloonbit4.center = CGPointMake (XJ, 415);
 balloonbit5.center = CGPointMake (XE, 409);
 balloonbit6.center = CGPointMake (XF, 427);
 balloonbit8.center = CGPointMake (XH, 417);

}

This all works fine, i would now like to stop the accelerometer from working, using the same

accelerometerWorks method or a new method, accelerometerWontWork.

This is the code that when it is called i would like the accelerometer to stop. (it is very long so i wont put all of it there, but its all the same throughout):

-(void) checkCollision{

 if (CGRectIntersectsRect(pinend.frame, balloonbit1.frame)){
  if (balloonbit1.frame.origin.y > pinend.frame.origin.y){
   [maintimer invalidate];
   levelfailed.center = CGPointMake (160, 235);
  }
 }
 if (CGRectIntersectsRect(pinend.frame, balloonbit2.frame)){
  if (balloonbit2.frame.origin.y > pinend.frame.origin.y){
   [maintimer invalidate];
   levelfailed.center = CGPointMake (160, 235);
  }
 }
 if (CGRectIntersectsRect(pinend.frame, balloonbit3.frame)){
  if (balloonbit3.frame.origin.y > pinend.frame.origin.y){
   [maintimer invalidate];
   levelfailed.center = CGPointMake (160, 235);
  }
 }
}

I understand that i may have to turn this into a BOOL. Thats fine.

If i haven't explained it well please say and i will re write it.

Thanks! Harry.

+1  A: 
accelManeger = [UIAccelerometer sharedAccelerometer];
accelManeger.delegate = nil;

that will stop you receiving events. Then set yourself as the delegate when you want to start again.

Spyker
THANKYOU! Works like a charm. Really cant thank you enough! Ledgend :)
Harry