Simulating accelerometer movement with the mouse on Mac OS X

Simulating accelerometer movement with the mouse on Mac OS X
0.0 0


So I want to have “cross-platform accelerometers” in that the mouse on Mac represents how you interact with the accelerometer for my game.

They way I’m doing this is getting the mouse events from my extension of the EAGLView (which performs a few other related tasks for me) and passing
those values along to my “accelerometer” function. This portion works great. And with a little math, I have the values normalized for my purposes.

But I have dynamically added sprites in my layer, and I’m trying to access them by tags or by reference and I’m consistently getting bad access errors.

Here’s some code. This first portion is how I add all my sprites dynamically from a set of assets that can or can not have a bunch of options.

void HelloWorld::addSpriteWith(string filename, int zIndex, bool andMiddle) {
    CCSprite *sprite = CCSprite::create(filename.c_str());


    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    sprite->setPosition(ccp(winSize.width/2, winSize.height/2));

    this->frontSprite = sprite; // <---- this is a class variable that will bad access

    this->addChild(sprite, zIndex);

Now for my “accelerometer” function:

void HelloWorld::didAccelerate(double x) {
    double accelX = x;

    CCSize size = cocos2d::CCDirector::sharedDirector()->getWinSize();
    double moveTo = ( (accelX * ((double)(50.0f/320.0f) * size.width )) + size.width / 2);
    this->frontSprite->setPosition(ccp(moveTo, frontSprite->getContentSize().height / 2));

This is called every time the mouse moves on the axis I’m interested in, which is the x axis, and only after the difference is signifiant
enough. It’s called from my AppController, which is Objective-C++.

I know that my CCSprites are not null because I see them! Yet I can’t touch them in my accelerometer function. Why is that?