Currently we have resumed development of a mobile product.   The backbone of our program is being able to quickly and accurately detect shake events.  We are using cocos2d for iphone, a wonderful product as our game engine.  If you are trying to develop 2 dimensional games and/or visualizations, there are few products that beat cocos2d.

For all of the pluses that cocos2d provide, the main minus for us was that cocos2d does not provide shake detection in its api.  The layer class provided by cocos2d supports touch events and multi-touch events, but not shake events.  Therefore we had to come up with a way to handle and process shake events.  Here is how we solved this issue (note this code is for iphone 3.0 > os)

1. Created interface that handles shake events

@protocol ShakeEventListenerProtocol
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;


2. Created a class that holds shake event listener handlers

This class is a singleton that holds are class that handles shake events.

/********** Note this class is a singleton !!!! *******/
@interface ShakeDispatcher : NSObject {
NSMutableArray * shakeHandlers; 
BOOL processEvents;

-(void) addShakeListener:(id) protocolClass; ...

//ShakeDispatcher.m (just a code snippet) -(void) addShakeListener:(id) protocolClass
if([protocolClass conformsToProtocol:@protocol(ShakeEventListenerProtocol)])
if(shakeHandlers !=NULL)
[shakeHandlers addObject:protocolClass];

3. Overwrote UIWindow class to have implementation of shake events

//Note override all needed shake methods
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
if ( event.subtype == UIEventSubtypeMotionShake )
//let the shake dispatcher singleton handle motion events
ShakeDispatcher * dispatcher = [ShakeDispatcher sharedInstance];
[dispatcher motionEnded:motion withEvent:event];
[super motionEnded:motion withEvent:event];


/************ Note you should override this function and make sure to return true ********/
- (BOOL)canBecomeFirstResponder
{ return YES; }

4. Changed the UI Window to a ShakeEnabledUIWindow in your app delegate

Cocos2d creates and app delegate with a UIWindow. Change that to a a ShakeEnabledUIWindow

@interface myAppDelegate : NSObject {
ShakeEnabledUIWindow *window;


5. Change your class to implement ShakeEventListenerProtocol


@interface MyClass : Layer {

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
if ( event.subtype == UIEventSubtypeMotionShake )
//do whatever you need to do right here

6. Add this class as a listener

ShakeDispatcher * dispatcher = [ShakeDispatcher sharedInstance];
[dispatcher addShakeListener:self];

HERE are the files if you want to include them in your project.  We are opening up comments to this one. We would very much like to know what you think and we will try to post more blog entries.











Please login to comment

Comments (70774)

  • Aug 30, 2014 Adding Shake Recongnition to cocos2d IPhone Library...
  • psychic phone Aug 30, 2014
    psychic phone... Adding Shake Recongnition to cocos2d IPhone Library...
  • Discounted Professional Best Kodak Mini Hd Camcorder Reviews at Aug 30, 2014
    Discounted Professional Best Kodak Mini Hd Camcorder Reviews at Adding Shake Recongnition to cocos2d IPhone Library...
  • Aug 30, 2014 Adding Shake Recongnition to cocos2d IPhone Library...
  • dental implants tijuana Aug 30, 2014
    dental implants tijuana... Adding Shake Recongnition to cocos2d IPhone Library...

New Site Poll

Do you like our new site?

Get Social With Us

Get the latest company information and special deals by joining our social networks

Softvelopment Twitter
Softvelopment Facebook

Softvelopment Blog

We have created a blog that will allow for more access to/from our developers, management and staff. We hope your will subscribe to our blog feed and comment when necessary. Just click on the 'Blog' menu item on the top right of the page.

Softvelopment Partners With J.K. Jones LLC

We are pleased to announce that Softvelopment LLC has entered into a partnership with J.K. Jones LLC

click here for more details

Copyright 2010 © Softvelopment LLC.