我在 cocos2d 上,我有一个精灵,我想用加速度计旋转。
我听说过 CMMotionManager。我想知道是否可以将其仅用于 2D 旋转,如果可以,如何?
我在 cocos2d 上,我有一个精灵,我想用加速度计旋转。
我听说过 CMMotionManager。我想知道是否可以将其仅用于 2D 旋转,如果可以,如何?
把这个放在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