-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
self.accelerationY = acceleration.y * 10;
CCLOG(@"acceleration.y: %f",acceleration.y);
CCLOG(@"accelerationY: %f",self.accelerationY);
}
-(void) update:(ccTime)deltaTime
{
CCLOG(@"__accelerationY: %f",self.accelerationY);
CGFloat x = __human.position.x;
if (self.accelerationY != 0) {
x += self.accelerationY;
}
__human.position = ccp( x , __human.position.y);
}
本以為可以正常的執行,卻發現__accelerationY的值一直是0,非常奇怪,找了下資料才發現要在OnEnter中再設定delegate
-(void) onEnter
{
[super onEnter];
if( self.isAccelerometerEnabled )
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
這樣就ok了參考網址:
weird issue with accelerometer
沒有留言:
張貼留言