Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:00:44+00:00 2026-06-02T05:00:44+00:00

I’m on cocos2d and I have a sprite that I would like to rotate

  • 0

I’m on cocos2d and I have a sprite that I would like to rotate with the accelerometer.

I’ve heard about CMMotionManager. I would like to know if it is possible to use it just for 2D rotation, and, if yes, how?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:00:45+00:00Added an answer on June 2, 2026 at 5:00 am

    put this in onEnter:

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.updateInterval = 1.0/50.0; //update interval in sec...so 1/50= 20 ms
    accelerometer.delegate = self;
    

    you need to conform to UIAccelerometerDelegate like so:

    @interface MyClass:CCLayer <UIAccelerometerDelegate>
    

    and implement this in MyClass.m:

    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration    *)acceleration {
    CCLOG(@"x = %f y = %f z = %f",acceleration.x,acceleration.y,acceleration.z);
    mysprite.rotation=acceleration.x*20;
    }
    

    edit: almost forgot…put accelerometer.delegate = nil; in onExit

    note that the method is called every time the accelerometer changes value..in all 3 vectors

    cards on the table..i didnt use accelerometer …ever..but it should look something like this…check the rotation property in the documentation and play a little with it

    hope it helps

    PS: loved the “sorry for my english i’m french” part…hilarious

    edit: here is my test of that code..and made a few modifications..it works fairly smooth..if you dont like it play around with the values.

    #import "cocos2d.h"
    
    // HelloWorldLayer
    UIAccelerationValue accelerationX;
    UIAccelerationValue accelerationY;
    float currentRawReading;
    float calibrationOffset;
    
    @interface HelloWorldLayer : CCLayer <UIAccelerometerDelegate>
    {
        CCLabelTTF *label;
    }
    
    // returns a CCScene that contains the HelloWorldLayer as the only child
    +(CCScene *) scene;
    
    @end
    
    
    
    
    
    #import "HelloWorldLayer.h"
    
    // HelloWorldLayer implementation
    @implementation HelloWorldLayer
    
    +(CCScene *) scene
    {
        // 'scene' is an autorelease object.
        CCScene *scene = [CCScene node];
    
        // 'layer' is an autorelease object.
        HelloWorldLayer *layer = [HelloWorldLayer node];
    
        // add layer as a child to scene
        [scene addChild: layer];
    
        // return the scene
        return scene;
    }
    
    #define kFilteringFactor .05
    
    
    CGFloat RadiansToDegrees(CGFloat radians) {return radians *180/M_PI;};
    
    
    
    // on "init" you need to initialize your instance
    -(id) init
    {
        // always call "super" init
        // Apple recommends to re-assign "self" with the "super" return value
        if( (self=[super init])) {
    
            UIAccelerometer *accel= [UIAccelerometer sharedAccelerometer];
            accel.delegate=self;
            accel.updateInterval=1/60;
    
    
    
            // create and initialize a Label
            label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
    
            // ask director the the window size
            CGSize size = [[CCDirector sharedDirector] winSize];
    
            // position the label on the center of the screen
            label.position =  ccp( size.width /2 , size.height/2 );
            label.flipY=YES; //i have absolutly no idea why the label is fliped :/
            label.flipX=YES;
            label.rotation=0;
            // add the label as a child to this Layer
            [self addChild: label];
        }
        return self;
    }
    
    -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
    
        CCLOG(@"acc called");
        accelerationX=acceleration.x *kFilteringFactor +accelerationX *(1-kFilteringFactor);
        accelerationY=acceleration.y*kFilteringFactor +accelerationY *(1-kFilteringFactor);
        currentRawReading=atan2(accelerationY, accelerationX);
    
        label.rotation=-RadiansToDegrees(currentRawReading);
    
    }
    
    
    // on "dealloc" you need to release all your retained objects
    - (void) dealloc
    {
        // in case you have something to dealloc, do it in this method
        // in this particular example nothing needs to be released.
        // cocos2d will automatically release all the children (Label)
    
        // don't forget to call "super dealloc"
    
        [super dealloc];
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.