IPHONE SDK,如何将Void函数放入我的BOOL中???请!

发布于 2024-08-26 18:38:33 字数 1547 浏览 14 评论 0原文

我正在使用 iPhone 的加速计来移动物体。我希望能够使这个功能根据不同的状态起作用而不起作用。

我有我的加速度计函数的代码,我想将它放入 BOOL 中,这样我就可以在需要时调用它,但我遇到了问题。谁能帮我把这段代码放入一个名为:

-(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);
}

请帮忙的 BOOL 中。我已经尝试了很多年但没有成功。谢谢。 哈利.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-柠檬树下少年和吉他 2024-09-02 18:38:33

这是我对您所追求的内容的最佳猜测,没有任何证据可以验证。

如果您只想在代码中声明 BOOL 值,请放入

BOOL accelerometerWorks;

.h 文件中。

否则,如果您想要一个函数根据需要检查游戏的状态,请执行以下操作:

-(BOOL) accelerometerWorks{
    //check conditions, return YES or NO...
    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;
    //etc.
}

您不能乱搞加速度计的声明:didAccelerate,否则您将停止接收消息,但是您可以检查其内部的无效状态。

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.

落日海湾 2024-09-02 18:38:33

Harry - 你可能还想将 320 设置为常量,就像你对 BALL_RADIUS 所做的那样(你会希望它能够在 iPad 上工作,对吧) ?也许?)并且您还可以考虑将重复五次的代码分解到自己的方法中。 aehilrs 的代码应该完全满足您对加速度计的要求。

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

如果您决定更改限制代码的工作方式,则只需更改一处即可。出于与 320 相同的原因,您的 CGPoint 幻数作为常量也会更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文