0

私はcocos2dを使用していて、加速度計で回転させたいスプライトがあります。

CMMotionManagerについて聞いたことがあります。2D回転だけに使用できるかどうか知りたいのですが、使用できる場合はどうすればよいですか?

4

1 に答える 1

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
于 2012-04-20T21:58:19.240 に答える