Accelerometer in the iPhone Simulator
It can be a frustrating and time consuming task to work with the accelerometer feature in iPhone development. The iPhone Simulator does not come with any built-in capabilities to receive accelerometer events from factory.
Some developers accomplished a way to simulate it in the Simulator. One technique involves using a third party library that generate events and send them to your program. Another technique is kind of esoteric, as it reads your internal MacBook accelerometer and handle them as they would come from an iPhone. I just can’t imagine myself turning my MacBook upside-down just to test our game
I got a simple solution to simulate it. First I call the accelerometer from my update function (notice that I’m using a preprocessor macro), using nil as parameters:
#ifdef ACCELEROMETER_SIM [self accelerometer:nil didAccelerate:nil]; #endif
Then, on the accelerometer function, I test the program directly inputting the vectors that I need to test. In this case I want to test the Y axis:
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
// Use a basic low-pass filter to keep only the gravity component of each axis.
mAccelX = (acceleration.x * FILTER) + (mAccelX * (1.0 - FILTER));
mAccelZ = (acceleration.z * FILTER) + (mAccelZ * (1.0 - FILTER));
#ifdef ACCELEROMETER_SIM
mAccelY = (-1.0f * FILTER) + (mAccelY * (1.0 - FILTER));
#else
mAccelY = (acceleration.y * FILTER) + (mAccelY * (1.0 - FILTER));
#endif
}
It’s super simple, and you can have your tests right from your iPhone Simulator. No more uploading to device just to test
Dealing with NSCalendar and dates
If you need to work with dates on iPhoneOS, chances are you will need to use NSDate. Furthermore, if you need to work with timeframes, and also do some calculations for time differences, you should also know NSCalendar.
If you already saw the code below, you are right. Cashtown begins in 1958, that’s our creation time for the game. So day 1 in the game is Jan 1st, 1958. To set up this date, you should use a NSDateComponents, that only stores this value, look at line 33. It doesn’t mean anything, as a NSDateComponents must rely on some kind of calendar.
You can choose weird and crazy calendars, but for us, the Gregorian is good enough. As this function gives the default calendar to iPhoneOS, we are just getting the current calendar on line 31.
On line 38 we set mCreationDate (which is a NSDate) to the Component date, using a calendar. No problems, BUT there is a caveat here. You need to retain this value! First I thought that as this calendar comes from a NSCalendar class method (line 31) I would not need to retain. If you didn’t insert the retain keyword, you will find that mCreationDate is gone
Also, don’t forget to dealloc it on destruction, like we do on line 46.
@interface Sample : NSObject
{
NSDate* mCreationDate;
int mCreationDay;
int mCreationMonth;
int mCreationYear;
}
@property (retain) NSDate* mCreationDate;
@property (assign) int mCreationDay;
@property (assign) int mCreationMonth;
@property (assign) int mCreationYear;
@implementation Sample
@synthesize mCreationDate;
@synthesize mCreationDay;
@synthesize mCreationMonth;
@synthesize mCreationYear;
-(id)init
{
[super init];
if (self != nil)
{
mCreationDay = 1;
mCreationMonth = 1;
mCreationYear = 1958;
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:mCreationDay];
[components setMonth:mCreationMonth];
[components setYear:mCreationYear];
mCreationDate = [[currentCalendar dateFromComponents:components] retain];
[components release];
}
return self;
}
- (void) dealloc
{
[mCreationDate release];
[super dealloc];
}
See also
- Cashtown ·· All the colors and art of a time
- How we work ·· Our games are exclusive
- Cashtown ·· A bit of its way of thinking


