2012-04-20 68 views

回答

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

說明該方法被調用每一個加速度的變化value..in所有3個向量

在table..i沒有使用加速計

卡... ever..but它應該是這個樣子的時間...查看文檔中的旋轉特性,發揮一點點吧

希望它有助於

PS:喜歡的「對不起,我的英語我是法國人」的一部分......熱鬧

編輯:這是我對那個鱈魚的測試e ..並做了一些修改..它的工作相當順利..如果你不喜歡它玩的價值觀。

#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? – 2012-04-21 16:22:04

+0

...你只需要加速度計對不對?這是一個更直接的方法:) – skytz 2012-04-21 18:45:37

+0

是的,但與加速度計的旋轉不平穩,即使我添加kfilterFactor還有一個問題,有一個很大的延遲:我旋轉的iPhone,圖像旋轉2秒後。我不知道該怎麼做,所以我聽說了關於CCMotionManager,這就是爲什麼我問這個問題 – 2012-04-21 18:48:56