IPHONE SDK,如何将Void函数放入我的BOOL中???请!
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我对您所追求的内容的最佳猜测,没有任何证据可以验证。
如果您只想在代码中声明 BOOL 值,请放入
.h 文件中。
否则,如果您想要一个函数根据需要检查游戏的状态,请执行以下操作:
以及:
您不能乱搞加速度计的声明: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
in your .h file.
Otherwise, if you want a function to check the state of the game as needed, do something like this:
and this:
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 - 你可能还想将
320
设置为常量,就像你对BALL_RADIUS
所做的那样(你会希望它能够在 iPad 上工作,对吧) ?也许?)并且您还可以考虑将重复五次的代码分解到自己的方法中。 aehilrs 的代码应该完全满足您对加速度计的要求。如果您决定更改限制代码的工作方式,则只需更改一处即可。出于与 320 相同的原因,您的 CGPoint 幻数作为常量也会更好。
Harry - you also might want to make the
320
a constant, like you did withBALL_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.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.