使用加速度计在cocos2d中旋转精灵

时间:2012-04-20 21:22:29

标签: iphone xcode cocos2d-iphone rotation accelerometer

我在cocos2d上,我有一个精灵,我想用加速度计旋转。

我听说过CMMotionManager。我想知道是否可以将它用于2D旋转,如果是,那该怎么办?

1 个答案:

答案 0 :(得分:1)

将其放在onEnter

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

你需要像UIAccelerometerDelegate一样遵守:

@interface MyClass:CCLayer <UIAccelerometerDelegate>

并在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;
}

编辑:差点忘了......将accelerometer.delegate = nil;放在onExit

请注意,每次加速度计在所有3个向量中改变值时都会调用该方法

桌子上的卡片......我没有使用加速度计......永远......但它应该看起来像这样......检查文档中的旋转属性并用它来玩一点

希望有所帮助

PS:喜欢“对不起我的英语我是法国人”的一部分...搞笑

编辑:这是我对该代码的测试...并进行了一些修改..它的工作相当顺畅..如果你不喜欢它,那就玩这些值。

#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