How to detect shake Gesture in your iPhone Cocos2D?

By | February 26, 2011

For detecting shake in a cocos2D program copy these lines to your layer class

bool shaked_once; //default false

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

Then copy this function to the same file…. and you are done……..

-(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 (!shaked_once) {
                           shaked_once = true;
                   }
         }
         else {
                          shaked_once = false;
         }
}

Leave a Reply

Your email address will not be published. Required fields are marked *