How to detect shaking on iPhone using Cocos2D?

I know how to make shaking for the iPhone, it has been asked a million times here, but I can not find anything useful regarding the accelerometer with Cocos2D. All I found involves using views, and I don't think I use any views in Cocos2D, if I hide them from me, I think. I want to say when there was some kind of shake in the CCLayer class?

+3
source share
1 answer

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

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

Then we 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 registering more than once.

+11

Source: https://habr.com/ru/post/1758232/


All Articles