自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Cocos2D學(xué)習(xí)筆記之UIAccelerometer加速計(jì)

移動開發(fā) iOS 游戲開發(fā)
本文介紹的是Cocos2D學(xué)習(xí)筆記之UIAccelerometer加速計(jì),對于Cocos2D估計(jì)友們不是很陌生,本文主要講解UIAccelerometer加速計(jì)的實(shí)例,來看詳細(xì)內(nèi)容。

Cocos2D學(xué)習(xí)筆記之UIAccelerometer加速計(jì)是本文要介紹的內(nèi)容,以UIAccelerometer加速計(jì)為實(shí)例,來看內(nèi)容。UIAccelerometer加速計(jì)是用來檢測iphone手機(jī)在x.y.z軸三個軸上的加速度。要獲得此類調(diào)用:

  1. UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; 

同時,你需要設(shè)置它的delegate。

  1. UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];  
  2. accelerometer.delegate = self;  
  3. accelerometer.updateInterval = 1.0/60.0; 

委托方法:

  1. - (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í)行一些特殊的代碼:

  1. - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration  
  2. {  
  3. static NSInteger shakeCount = 0;  
  4. static NSDate *shakeStart;  
  5. NSDate *now = [[NSDate alloc] init];  
  6. NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];  
  7. if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil)  
  8. {  
  9. shakeCount = 0;  
  10. [shakeStart release];  
  11. shakeStart = [[NSDate alloc] init];  
  12. }  
  13. [now release];  
  14. [checkDate release];  
  15. if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0)  
  16. {  
  17. shakeCount++;  
  18. if (shakeCount > 4)  
  19. {  
  20. // -- DO Something  
  21. shakeCount = 0;  
  22. [shakeStart release];  
  23. shakeStart = [[NSDate alloc] init];  
  24. }  
  25. }  

2、加速計(jì)最常見的是用作游戲控制器。在游戲中使用加速計(jì)控制對象的移動!在簡單情況下,可能只需獲取一個軸的值,乘上某個數(shù)(靈敏度),然后添加到所控制對象的坐標(biāo)系中。在復(fù)雜的游戲中,因?yàn)樗⒌奈锢砟P透诱鎸?shí),所以必須根據(jù)加速計(jì)返回的值調(diào)整所控制對象的速度。

cocos2d中接收加速計(jì)輸入input.使其平滑運(yùn)動,一般不會去直接改變對象的position.通過:

  1. - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration  
  2. {  
  3. // -- controls how quickly velocity decelerates(lower = quicker to change direction)  
  4. float deceleration = 0.4;   
  5. // -- determins how sensitive the accelerometer reacts(higher = more sensitive)  
  6. float sensitivity = 6.0;  
  7. // -- how fast the velocity can be at most  
  8. float maxVelocity = 100;  
  9. // adjust velocity based on current accelerometer acceleration  
  10. playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;  
  11. // -- we must limit the maximum velocity of the player sprite, in both directions  
  12. if (playerVelocity.x > maxVelocity)  
  13. {  
  14. playerVelocity.x = maxVelocity;  
  15. }  
  16. else if (playerVelocity.x < - maxVelocity)  
  17. {  
  18. playerVelocity.x = - maxVelocity;  
  19. }  

上面deceleration是減速的比率,sensitivity是靈敏度。maxVelocity是***速度,如果不限制則一直加大就很難停下來。

  1. playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity; 

中 playervelocity是一個速度向量。是累積的。

  1. - (void) update: (ccTime)delta  
  2. {  
  3. // -- keep adding up the playerVelocity to the player's position  
  4. CGPoint pos = player.position;  
  5. pos.x += playerVelocity.x;  
  6. // -- The player should also be stopped from going outside the screen  
  7. CGSize screenSize = [[CCDirector sharedDirector] winSize];  
  8. float imageWidthHalved = [player texture].contentSize.width * 0.5f;  
  9. float leftBorderLimit = imageWidthHalved;  
  10. float rightBorderLimit = screenSize.width - imageWidthHalved;  
  11. // -- preventing the player sprite from moving outside the screen  
  12. if (pos.x < leftBorderLimit)  
  13. {  
  14. pos.x = leftBorderLimit;  
  15. playerVelocity = CGPointZero;  
  16. }  
  17. else if (pos.x > rightBorderLimit)  
  18. {  
  19. pos.x = rightBorderLimit;  
  20. playerVelocity = CGPointZero;  
  21. }  
  22. // assigning the modified position back  
  23. player.position = pos;  

小結(jié):Cocos2D學(xué)習(xí)筆記之UIAccelerometer加速計(jì)的內(nèi)容介紹完了,希望本文對你有所幫助!

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2011-08-17 15:04:48

Cocos2DUIAccelerom加速計(jì)

2011-08-11 18:00:18

Cocos2d動作Action

2011-08-11 17:52:01

Cocos2d游戲對象

2011-08-02 15:47:28

Cocos2D Animation

2011-07-08 16:09:54

Cocoa Cocos2d 動作

2011-08-09 16:08:58

IOS游戲Cocos2d

2011-07-08 16:27:52

Cocoa Cocos2d 動作

2011-07-27 10:13:23

Cocos2D iPhone

2011-07-29 18:02:06

2012-06-01 10:27:44

Cocos2d觸摸分發(fā)原理

2011-08-04 17:01:16

iPhone游戲開發(fā) Cocos2d

2011-08-11 14:22:47

iPhone游戲Cocos2D

2011-08-11 14:32:04

iPhone游戲Cocos2dActions

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2011-07-27 13:44:08

2011-07-20 14:04:46

Cocos2d iPhone 游戲

2011-08-09 16:25:16

Cocos2d視圖坐標(biāo)

2011-07-27 14:48:21

iPhone Cocos2D 坐標(biāo)

2011-08-08 11:26:39

Cocos2d 游戲 Class類

2011-08-08 17:17:55

Cocos2D 坐標(biāo) OpenglES
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號