0

I'm using CoreMotion to gather accelerometer data to change the position of a sprite. In order to do this you need to use a block and update to a queue. However, doing this conflics with the cocos2d scheduleUpdate. Here is the code for Core self.motionManager =

[[CMMotionManager alloc] init];
        if (motionManager.isAccelerometerAvailable) {
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [self.motionManager
             startAccelerometerUpdatesToQueue:queue
             withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                 // Deceleration
                 float deceleration = 0.4f;                         // Controls how fast velocity deceerates/low = quck to change dir
                 // sensitivity
                 float sensitivity=0.6f;
                 //how fast the velocity can be at most
                 float maxVelocity=100;

                 // adjust the velocity basedo n current accelerometer acceleration
                 playerVelocity.x = playerVelocity.x*deceleration+accelerometerData.acceleration.x*sensitivity;

                 // we must limit the maximum velocity of the players sprite, in both directions
                 if (playerVelocity.x>maxVelocity) {
                     playerVelocity.x=maxVelocity;
                 }else if (playerVelocity.x < -maxVelocity) {
                     playerVelocity.x = -maxVelocity;
                 }

                 // schedule the -(void)update:(ccTime)delta method to be called every frame

                 [self scheduleUpdateWithPriority:0];
             }];

        }

and this is the code for my schedule update:

/*************************************************************************/
//////////////////////////Listen for Accelerometer/////////////////////////
/*************************************************************************/
-(void) update:(ccTime)delta
{
    // Keep adding up the playerVelocity to the player's position
    CGPoint pos = player.position;
    pos.x += playerVelocity.x;

    // The player should also be stopped form going outside the screen
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    float imageWidthHalved = player.texture.contentSize.width * 0.5f;
    float leftBorderLimit = imageWidthHalved;
    float rightBorderLimit = screenSize.width - imageWidthHalved;

    // preventing the player sprite from moving outside of the screen
    if (pos.x < leftBorderLimit) {
        pos.x=leftBorderLimit;
        playerVelocity=CGPointZero;
    } else if (pos.x>rightBorderLimit)
    {
        pos.x=rightBorderLimit;
        playerVelocity = CGPointZero;
    }

    // assigning the modified position back
    player.position = pos;
}

I know that I can bypass using scheduleUpdate and put the update code inside of the first block, but I still want to be able to use scheduleUpdates. How do I get around this?

Thank you very much for your help.

P.S. This is my error

    *** Terminating app due to uncaught exception
 'NSInternalInconsistencyException', reason: 
'CCScheduler: You can't re-schedule an 'update' selector'. 
Unschedule it first'

1 Answer 1

0

The problem is that the accelerometer block is called every frame, so you can't schedule updates there.

Just schedule update once in init. Then use a BOOl iVar/property to track whether your update method should process whatever it's doing or not.

Alternatively move all the update code inside the block. After all, accelerometer values update every frame, so you won't actually miss an "update".

1
  • Thanks, it's always something obvious that I miss. By the way, thanks for writing your book Learn Cocos2D, its been a real joy for me to go through being new to the Cocoa environment and programming in general.
    – AlexHeuman
    Commented Dec 12, 2012 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.