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

Cocos2D學(xué)習(xí)筆記中UIAccelerometer加速計(jì)案例實(shí)現(xiàn)

移動(dòng)開(kāi)發(fā) iOS 游戲開(kāi)發(fā)
Cocos2D學(xué)習(xí)筆記中UIAccelerometer加速計(jì)案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來(lái)了解UIAccelerometer加速器的實(shí)現(xiàn),來(lái)看本文詳解。

Cocos2D學(xué)習(xí)筆記中UIAccelerometer加速計(jì)案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來(lái)了解UIAccelerometer加速計(jì)的實(shí)現(xiàn)。UIAccelerometer加速計(jì)是用來(lái)檢測(cè)iphone手機(jī)在x.y.z軸三個(gè)軸上的加速度。要獲得此類(lèi)調(diào)用。

  1. UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; 

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

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

委托方法:- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration中的UIAcceleration是表示加速度類(lèi)。包含了來(lái)自加速計(jì)UIAccelerometer的真是數(shù)據(jù)。它有3個(gè)屬性的值x、y、z。iphone的加速計(jì)支持最高以每秒100次的頻率進(jìn)行輪詢(xún)。此時(shí)是60次。

(1) 應(yīng)用程序可以通過(guò)加速計(jì)來(lái)檢測(cè)搖動(dòng),如:用戶(hù)可以通過(guò)搖動(dòng)iphone擦除繪圖。

也可以用戶(hù)連續(xù)搖動(dòng)幾次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ì)最常見(jiàn)的是用作游戲控制器。在游戲中使用加速計(jì)控制對(duì)象的移動(dòng)!在簡(jiǎn)單情況下,可能只需獲取一個(gè)軸的值,乘上某個(gè)數(shù)(靈敏度),然后添加到所控制對(duì)象的坐標(biāo)系中。在復(fù)雜的游戲中,因?yàn)樗⒌奈锢砟P透诱鎸?shí),所以必須根據(jù)加速計(jì)返回的值調(diào)整所控制對(duì)象的速度。

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

  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是最大速度,如果不限制則一直加大就很難停下來(lái)。

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

中 playervelocity是一個(gè)速度向量。是累積的。

  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ì)案例實(shí)現(xiàn)的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-02 15:37:48

Cocos2D UIAccelero

2011-08-11 17:52:01

Cocos2d游戲對(duì)象

2011-08-11 18:00:18

Cocos2d動(dòng)作Action

2011-08-09 16:08:58

IOS游戲Cocos2d

2011-07-08 16:09:54

Cocoa Cocos2d 動(dòng)作

2011-08-02 15:47:28

Cocos2D Animation

2011-07-08 16:27:52

Cocoa Cocos2d 動(dòng)作

2011-07-27 10:13:23

Cocos2D iPhone

2011-07-29 18:02:06

2012-06-01 10:27:44

Cocos2d觸摸分發(fā)原理

2011-08-08 17:17:55

Cocos2D 坐標(biāo) OpenglES

2011-08-16 17:13:02

Cocos2DFruit Ninja

2011-08-11 14:22:47

iPhone游戲Cocos2D

2011-08-08 11:40:42

Cocos2d CCLayer Touch

2011-08-11 14:32:04

iPhone游戲Cocos2dActions

2011-08-22 10:49:42

Cocos2d 開(kāi)發(fā)CCLayerTouch事件

2011-08-08 15:40:47

Cocos2d

2011-07-27 13:44:08

2011-07-20 14:04:46

Cocos2d iPhone 游戲

2011-08-09 16:25:16

Cocos2d視圖坐標(biāo)
點(diǎn)贊
收藏

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