didAccelerate function not being called in CCLayer derived class.

I set up my layer to receive accelerometer input but my didAccelerate function is not being called.

This is how I set it up.

CCLayerDerivedClass.h

public: 
   //Declare did Accelerate function
    void didAccelerate(CCAcceleration* pAccelerationValue);

Then in the classes Init() function I write this:

     //Enable acceleraometer input for this layer
     this->setIsAccelerometerEnabled(true);

Then I define the class in the .cpp like so.
CCLayerDerivedClass.cpp:

void UserInterfaceLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{
    CCLog("Acceleration method called!"); //Display a message if this function gets called
}

I tried declaring the didAccelerate function as virtual but still does not work.

Any ideas?

Could you tell me your platform and sdk version?

Thank-you for your reply Walzer Wang.

I program using Win32 platform (Visual Studio) and then transfer my files over to IOS platfrom (XCode) for testing on Iphone and/or releasing.

I programmed the didAccelerate initially in XCode to test it, and it worked great.
Then I retyped my code in my identical Win32 version. When I ported it back to IOS for testing, it did not work anymore.

You want the SDK version of what?
And how do I check the SDK version?

I’m still stuck on this.

Do you have any ideas?

If not, do you know how I can make my own function to handle acceleration? I need to figure this out quickly.

I programmed the didAccelerate initially in XCode to test it, and it worked great.
Then I retyped my code in my identical Win32 version. When I ported it back to IOS for testing, it did not work anymor

What did you do on Win32 version?

I retyped my code in visual studio from what I had tested in XCode.
The two projects where Identical. I had just transfered my game to XCode on a mac to test and implement the accelerometer.
The test worked. So I retyped the code into my working copy in visual studio.

When I brought the game back into XCode to test it again, the didAccelerate was not working.

CCLayerDerivedClass.h:

public:    
   //Declare did Accelerate function
    void didAccelerate(CCAcceleration* pAccelerationValue);

In InitFunction of CCLayerDerivedClass:

 //Enable acceleraometer input for this layer
     this->setIsAccelerometerEnabled(true);

CCLayerDerivedClass.cpp

void UserInterfaceLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{
     //This didAccelerate Function is never called

     CCLog("Acceleration method called!");
}

Hi Marc,

I think it would be much easier to understand what is wrong if you attach your working (from XCode) and non-working (VS) code.

Geez, I think I found out what it was. When I retyped the code from Xcode to Visual Studio, I recoded it so that the function tested against the wrong timestamp so the function exited prematurely.

I don’t understand why my breakpoints above that line where not getting hit though. But it seems to be working now.

Thanks for the help.

Bad Code:

void UserInterfaceLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{
    //If the timestamp is 0.0 then we know that this is the first frame, so do nothing.
    if(m_accelerationLastFrame.timestamp == 0.0)
        return;        

        double ydif = m_accelerationLastFrame.y - pAccelerationValue->y;
        double xdif = m_accelerationLastFrame.x - pAccelerationValue->x;

        if(ydif > 0.50 || ydif < -0.50 || xdif > 0.50 || xdif < -0.50)
    {

           if(m_canReload)
              this->Reload();
    }   

    //SEt the aceleration last frame
    m_accelerationLastFrame = *pAccelerationValue;  
}

Good code:

void UserInterfaceLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{
    //If the timestamp is 0.0 then we know that this is the first frame, so do nothing.
    if(pAccelerationValue->timestamp == 0.0)
        return;

        double ydif = m_accelerationLastFrame.y - pAccelerationValue->y;
        double xdif = m_accelerationLastFrame.x - pAccelerationValue->x;

        if(ydif > 0.50 || ydif < -0.50 || xdif > 0.50 || xdif < -0.50)
    {

           if(m_canReload)
              this->Reload();
    }   

    //SEt the aceleration last frame
    m_accelerationLastFrame = *pAccelerationValue;  
}

Bad code:

//If the timestamp is 0.0 then we know that this is the first frame, so do nothing.
if(m_accelerationLastFrame.timestamp == 0.0)
    return; 

Good code:

//If the timestamp is 0.0 then we know that this is the first frame, so do nothing.
if(pAccelerationValue->timestamp == 0.0)
   return;