Cocos2D學(xué)習(xí)筆記之UIAccelerometer加速計(jì)
Cocos2D學(xué)習(xí)筆記之UIAccelerometer加速計(jì)是本文要介紹的內(nèi)容,以UIAccelerometer加速計(jì)為實(shí)例,來看內(nèi)容。UIAccelerometer加速計(jì)是用來檢測iphone手機(jī)在x.y.z軸三個軸上的加速度。要獲得此類調(diào)用:
- UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
同時,你需要設(shè)置它的delegate。
- UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
- accelerometer.delegate = self;
- accelerometer.updateInterval = 1.0/60.0;
委托方法:
- - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration中的UIAcceleration
是表示加速度類。包含了來自加速計(jì)UIAccelerometer的真是數(shù)據(jù)。它有3個屬性的值x、y、z。iphone的加速計(jì)支持***以每秒100次的頻率進(jìn)行輪詢。此時是60次。
1、應(yīng)用程序可以通過加速計(jì)來檢測搖動,如:用戶可以通過搖動iphone擦除繪圖。
也可以用戶連續(xù)搖動幾次iphone,執(zhí)行一些特殊的代碼:
- - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
- {
- static NSInteger shakeCount = 0;
- static NSDate *shakeStart;
- NSDate *now = [[NSDate alloc] init];
- NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];
- if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil)
- {
- shakeCount = 0;
- [shakeStart release];
- shakeStart = [[NSDate alloc] init];
- }
- [now release];
- [checkDate release];
- if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0)
- {
- shakeCount++;
- if (shakeCount > 4)
- {
- // -- DO Something
- shakeCount = 0;
- [shakeStart release];
- shakeStart = [[NSDate alloc] init];
- }
- }
- }
2、加速計(jì)最常見的是用作游戲控制器。在游戲中使用加速計(jì)控制對象的移動!在簡單情況下,可能只需獲取一個軸的值,乘上某個數(shù)(靈敏度),然后添加到所控制對象的坐標(biāo)系中。在復(fù)雜的游戲中,因?yàn)樗⒌奈锢砟P透诱鎸?shí),所以必須根據(jù)加速計(jì)返回的值調(diào)整所控制對象的速度。
在cocos2d中接收加速計(jì)輸入input.使其平滑運(yùn)動,一般不會去直接改變對象的position.通過:
- - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
- {
- // -- controls how quickly velocity decelerates(lower = quicker to change direction)
- float deceleration = 0.4;
- // -- determins how sensitive the accelerometer reacts(higher = more sensitive)
- float sensitivity = 6.0;
- // -- how fast the velocity can be at most
- float maxVelocity = 100;
- // adjust velocity based on current accelerometer acceleration
- playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
- // -- we must limit the maximum velocity of the player sprite, in both directions
- if (playerVelocity.x > maxVelocity)
- {
- playerVelocity.x = maxVelocity;
- }
- else if (playerVelocity.x < - maxVelocity)
- {
- playerVelocity.x = - maxVelocity;
- }
- }
上面deceleration是減速的比率,sensitivity是靈敏度。maxVelocity是***速度,如果不限制則一直加大就很難停下來。
- playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
中 playervelocity是一個速度向量。是累積的。
- - (void) update: (ccTime)delta
- {
- // -- keep adding up the playerVelocity to the player's position
- CGPoint pos = player.position;
- pos.x += playerVelocity.x;
- // -- The player should also be stopped from going outside the screen
- CGSize screenSize = [[CCDirector sharedDirector] winSize];
- float imageWidthHalved = [player texture].contentSize.width * 0.5f;
- float leftBorderLimit = imageWidthHalved;
- float rightBorderLimit = screenSize.width - imageWidthHalved;
- // -- preventing the player sprite from moving outside the screen
- if (pos.x < leftBorderLimit)
- {
- pos.x = leftBorderLimit;
- playerVelocity = CGPointZero;
- }
- else if (pos.x > rightBorderLimit)
- {
- pos.x = rightBorderLimit;
- playerVelocity = CGPointZero;
- }
- // assigning the modified position back
- player.position = pos;
- }
小結(jié):Cocos2D學(xué)習(xí)筆記之UIAccelerometer加速計(jì)的內(nèi)容介紹完了,希望本文對你有所幫助!