我一直在使用以下代码开发基于加速度计的项目:
。H
{
CCSprite *donk;
float movement;
}
.m
-(id) init
{
if( (self=[super init]) ) {
donk = [CCSprite spriteWithFile:@"fe1.png" rect:CGRectMake(0, 0, 50, 50)];
donk.position = ccp (240,75);
[self addChild:donk];
[self scheduleUpdate];
self.isAccelerometerEnabled = YES;
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
}
return self;
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
float deceleration = .1f;
float sinsitivity = 10.0f;
float acelx = +acceleration.y;
movement = movement * deceleration + acelx * sinsitivity;
}
- (void)update:(ccTime)delta {
CCLOG(@"Position: %f", donk.position.x);
if ( donk.position.x > 0 && donk.position.x < 480) {
donk.position = ccp(donk.position.x + movement, donk.position.y);
}
if ( donk.position.x < -55 ) {
donk.position = ccp( 50, donk.position.y);
}
if (donk.position.x > 430 ) {
donk.position = ccp( 430, donk.position.y);
}
}
真正奇怪的是它在一个项目中工作,但我将它复制并粘贴到另一个项目中,现在它不起作用。精灵在屏幕上但没有移动,CCLOG 保持 x 值为 240。怎么了?我错过了什么吗?