tags:

views:

349

answers:

3

I am using the IPhone's Accelerometer to make a object move. I want to be able to make this function work and not work depending on different states.

I have my code for my Accelerometer function and i want to put it into a BOOL so i can call on it when i need it, but i am having problems. Can anyone Help me put this code into a BOOL named:

-(BOOL) accelerometerWorks

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
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 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, 415);
balloonbit1.center = CGPointMake (XA, 408);
balloonbit2.center = CGPointMake (XB, 395);
balloonbit5.center = CGPointMake (XE, 388);
balloonbit6.center = CGPointMake (XF, 413);
balloonbit8.center = CGPointMake (XH, 426);
}

Please help. i have been trying for ages with no success. Thanks. Harry.

A: 

Accident. Edit.

Harry
@Harry: Please note: this is not an answer ! It should be a comment, or an addition to your original question.
Paul R
That code is what i want the BOOL to be. Am i barking up the wrong tree here, should i be using a different type of method to validate the ude of the Accelerometer and Invalidate it? Thanks.Harry.
Harry
@Paul R I guess he gave you what you wanted there. heh
aehiilrs
ANYONE? please. is there a way to make that code so that when a if statement is true then the accelerometer works. and otherwise it doesn't. Please!
Harry
+3  A: 

Here's my best guess about what you're after, with nothing to verify it on.

If all you want to do is declare a BOOL value in your code, put

BOOL accelerometerWorks;

in your .h file.

Otherwise, if you want a function to check the state of the game as needed, do something like this:

-(BOOL) accelerometerWorks{
    //check conditions, return YES or NO...
    return time == 0;
}

and this:

-(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;
    //etc.
}

You can't mess around with the declaration of accelerometer:didAccelerate or you'll just stop receiving the messages, but you CAN check for an invalid state inside of it.

aehiilrs
Okay. i have implemented this code and it works, But when i try and return YES and NO it wont work. Here's what I've got. -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{ valueX = acceleration.x*25.5; if(![self accelerometerWorks]) return;}-(BOOL) accelerometerWorks{{ 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;return YES;}return NO;}then when i try putting it in if (time ==0) {acceleremeterWorks = YES // here Else maybe?NO
Harry
Whaaaaaaaaaaaaaaaat? I'll just dump more code for you in a minute...
aehiilrs
@Harry Does this make more sense?
aehiilrs
Yes, makes perfect sense now! thanks. Sorry for the trouble lol. Cheers, ill add rep or whatever.
Harry
WOOOOOO!!! IT WORKS. YOU ARE TRULY A LEGEND. I Cant thank you enough.
Harry
@Harry Glad to hear it works!
aehiilrs
+1  A: 

Harry - you also might want to make the 320 a constant, like you did with BALL_RADIUS (you're going to want this to be able to work on an iPad, right? maybe?) and you might also consider factoring out the code you repeat five times into its own method. aehilrs's code should do exactly what you want for the accelerometer.

-(int) limitTravel:(position) {
  if (position > MAX_POSITION-BALL_RADIUS)
     return MAX_POSITION-BALL_RADIUS;
  if (position < BALL_RADIUS)
     return BALL_RADIUS;
}

If you decide to change how the limiting code works, you'll only have to change it one place. Your CGPoint magic numbers would be better as constants too for the same reason as the 320.

Joe McMahon
I am, for some reason, fearing that BALL_RADIUS isn't a constant...
aehiilrs