如何使用Cocos2D检测iPhone的震动?

发布于 2024-09-13 05:08:56 字数 156 浏览 3 评论 0原文

我知道如何为 iPhone 摇动,这里已经被问了一百万次,但我似乎找不到任何关于 Cocos2D 加速度计的有用信息。我发现的所有内容都涉及使用视图,我不认为我在 Cocos2D 中使用任何视图,如果我使用的话,我想它们对我来说是隐藏的。我希望能够判断 CCLayer 类中何时发生任何形式的震动?

I know how to do shake for the iPhone has been asked a million times on here, but I can't seem to find anything useful regarding the accelerometer with Cocos2D. Everything i have found involves using views and I don't think i am using any views in Cocos2D, if I am they are hidden from me I think. I want to be able to tell when any sort of shake has occured within a CCLayer class?

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

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

发布评论

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

评论(1

洒一地阳光 2024-09-20 05:08:56

我想通了。在图层类中,您需要放置这些线;

self.isAccelerometerEnabled = YES;
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
shake_once = false;

然后在layer类中实现这个函数;

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

float THRESHOLD = 2;

if (acceleration.x > THRESHOLD || acceleration.x < -THRESHOLD || 
    acceleration.y > THRESHOLD || acceleration.y < -THRESHOLD ||
    acceleration.z > THRESHOLD || acceleration.z < -THRESHOLD) {

    if (!shake_once) {
        int derp = 22/7;
        shake_once = true;
    }

}
else {
    shake_once = false;
}

}

shake_once 只是一个布尔值,用于阻止多次注册一次摇动。

I figured it out. In the layer class you need to put these lines;

self.isAccelerometerEnabled = YES;
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
shake_once = false;

Then implement this function in the layer class;

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

float THRESHOLD = 2;

if (acceleration.x > THRESHOLD || acceleration.x < -THRESHOLD || 
    acceleration.y > THRESHOLD || acceleration.y < -THRESHOLD ||
    acceleration.z > THRESHOLD || acceleration.z < -THRESHOLD) {

    if (!shake_once) {
        int derp = 22/7;
        shake_once = true;
    }

}
else {
    shake_once = false;
}

}

shake_once is just a boolean to stop one shake from being registered more than once.

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