开发者

IPHONE SDK, How to put Void function into my BOOL??? PLEASE!

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 > 开发者_如何学Python320-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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜