2012-04-20 10 views
0

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

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

答えて

1

@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; 
} 

編集:ほとんど忘れてしまったの

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

をあなたはとても似UIAccelerometerDelegateに準拠する必要があります。 .. accelerometer.delegate = nil;をに入力

メソッドは毎回加速度計の変更がすべて3つのベクトルtable..iのdidntの使用加速度計の

カードをvalue..inと呼ばれていることに注意してください... ever..butそれは次のようになります。 ...陽気な

を一部「私はフランス人だ私の英語のため申し訳ありませんが、」愛して:...ドキュメントの回転プロパティをチェックし、それを少しを果たし

は、それが

PS役に立てば幸い編集:ここでそのタラの私のテストですe..andいくつかの変更を加えました.Itはかなり平滑に動作します。

#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

lol :)しかしCCMotionManagerを使用していますか? –

+0

うーん。あなただけが加速度計が必要ですか?これはより直接的なアプローチです:) – skytz

+0

はい、しかし加速度計の回転は滑らかではなく、kfilterFactorを追加しても別の問題があります。大きな遅延があります。私はiPhoneを回転させ、画像は2秒後に回転します。私は何をすべきかわからないので、私はこの質問にCCMotionManagerについて聞いた。 –

関連する問題