Game Development Community

Tutorial: Using the shake functionality in the SDK

by Craig Fortune · in iTorque 2D · 11/01/2009 (2:55 pm) · 11 replies

I said last week that I'd get a tutorial written up this week for utilising the shake detection functionality that is within the iPhone SDK. It is Sunday night as I write this... so I guess I've just about kept my promise :D

I've noticed people talking about checking for a shake by seeing if an axis on the accelerometer goes above a certain threshold. The problem with this is that it isn't a true "shake", that is merely a rapid acceleration. Another thing of note is that by using the shake functionality within the SDK (3.0+ I believe, I haven't checked) you are adhering a design principle of Apple, that is consistency across their platform where possible. This shake will work the same as any other apps that utilise the shake functionality etc. Not exactly crucial, but if directional information is not important to you for a shake then what have you got to lose? :)

As an aside, I use this shake functionality to pause and unpause my game.

With that done, let's begin...


Create a new file called MotionWindow.h. Drop it into /platformiPhone/
#import <UIKit/UIKit.h>

@interface MotionWindow : UIWindow
{
}

- (void) motionBegan: (UIEventSubtype)motion withEvent: (UIEvent *)event;

@end

Another new file… MotionWindow.mm, again in /platformiPhone/
#include "platformiPhone/platformiPhone.h"
#import "MotionWindow.h"

extern bool createShakeEvent();

@implementation MotionWindow

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
	if(motion == UIEventSubtypeMotionShake)
	{
		createShakeEvent();
	}
}

@end


The above two files are creating a new window we will utilise instead of the standard UIWindow that Torque uses. This window responds to the motionBegan delegate. Within this delegate we check what event occurred (a shake) and tell Torque to handle it via createShakeEvent().

In IphoneInput.mm, at the bottom add:
bool createShakeEvent()
{
	Con::executef(1, "onShake");
	return true;
}
This will throw a callback to script every time a shake is detected. This could be linked in with the Torque input system properly, but isn't necessary for a simple integration.

Example of the script...
function onShake()
{
   echo("Shake!");
}

And in TGBAppDelegate.h make the following changes:

Add another import...
#import "MotionWindow.h"

Change
IBOutlet UIWindow *window;
to
IBOutlet MotionWindow *window;

Also, change
@property (nonatomic, retain) UIWindow *window;
to
@property (nonatomic, retain) MotionWindow *window;


Now go into InterfaceBuilder and open MainWindow.xib and change the class to MotionWindow.

img101.imageshack.us/img101/7917/screenshot20091101at185.png



Enjoy.
PS: Let me know if you spot any errors etc. I haven't had time to fully proof read this yet :/

#1
11/02/2009 (11:00 am)
Very cool. I'll have to try it out.
#2
11/02/2009 (3:45 pm)
awesome, Craig. needed this for my game. thanks.
#3
11/17/2009 (4:16 am)
Hey Craig, after implementing your changes, everytime I shake the phone I get "onShake: Unknown Command" in the debugger.

Do you know what could be wrong?
#4
11/17/2009 (5:16 am)
Johnny, it would appear that you missed creating the onShake function script side.

Make sure that you declare onShake somewhere, and that that script is loaded at the time of shaking.
#5
11/17/2009 (5:40 am)
Hey Sven, it works now. Thats weird I swore i had onShake() in my scripts. I probably didn't save in JEdit or didn't run the builder first. Newbie mistake!

Anyway, thanks for your help Sven. And Craig, your code is confirmed working!

#6
11/17/2009 (7:54 am)
Great to hear :)
#7
11/17/2009 (8:27 am)
Cool.

This was posted and resolved before I even got a chance to read it! Go Go Mighty Sven support! :)
#8
03/12/2010 (7:33 pm)
I know this is kind of an old post, but I just wanted to say that this is an excellent resource. You saved me a huge amount of time. Thanks!
#9
03/13/2010 (7:55 am)
No problem :)
#10
05/27/2010 (12:28 pm)
Thanks Craig - have implemented this to unjam the gun in my game - works a treat :)
#11
05/27/2010 (2:35 pm)
Good stuff :)