Saturday, 1 June 2013

How to detect shake using accelerometer: cocos2d/Xcode


    // in init //
        bool shake_once = false;
        self.isAccelerometerEnabled = YES;

    // accelerometer method //

    -(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){
                    shake_once =true;
                    NSLog(@"shaked shaked...");
           
                }
            }
            else{
                shake_once = false;
            }
   
   
        }

No comments:

Post a Comment