Cocos2D学习笔记之UIAccelerometer加速计

移动开发 iOS 游戏开发
本文介绍的是Cocos2D学习笔记之UIAccelerometer加速计,对于Cocos2D估计友们不是很陌生,本文主要讲解UIAccelerometer加速计的实例,来看详细内容。

Cocos2D学习笔记之UIAccelerometer加速计是本文要介绍的内容,以UIAccelerometer加速计为实例,来看内容。UIAccelerometer加速计是用来检测iphone手机在x.y.z轴三个轴上的加速度。要获得此类调用:

  1. UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; 

同时,你需要设置它的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 

是表示加速度类。包含了来自加速计UIAccelerometer的真是数据。它有3个属性的值x、y、z。iphone的加速计支持***以每秒100次的频率进行轮询。此时是60次。

1、应用程序可以通过加速计来检测摇动,如:用户可以通过摇动iphone擦除绘图。

也可以用户连续摇动几次iphone,执行一些特殊的代码:

  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、加速计最常见的是用作游戏控制器。在游戏中使用加速计控制对象的移动!在简单情况下,可能只需获取一个轴的值,乘上某个数(灵敏度),然后添加到所控制对象的坐标系中。在复杂的游戏中,因为所建立的物理模型更加真实,所以必须根据加速计返回的值调整所控制对象的速度。

cocos2d中接收加速计输入input.使其平滑运动,一般不会去直接改变对象的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;  

小结:Cocos2D学习笔记之UIAccelerometer加速计的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-17 15:04:48

Cocos2DUIAccelerom加速计

2011-08-11 18:00:18

Cocos2d动作Action

2011-08-11 17:52:01

Cocos2d游戏对象

2011-08-02 15:47:28

Cocos2D Animation

2011-08-09 16:08:58

IOS游戏Cocos2d

2011-07-08 16:27:52

Cocoa Cocos2d 动作

2011-07-08 16:09:54

Cocoa Cocos2d 动作

2011-07-29 18:02:06

2011-07-27 10:13:23

Cocos2D iPhone

2012-06-01 10:27:44

Cocos2d触摸分发原理

2011-08-11 14:32:04

iPhone游戏Cocos2dActions

2011-08-11 14:22:47

iPhone游戏Cocos2D

2011-08-04 17:01:16

iPhone游戏开发 Cocos2d

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2011-08-08 11:26:39

Cocos2d 游戏 Class类

2011-07-27 14:48:21

iPhone Cocos2D 坐标

2011-08-08 17:17:55

Cocos2D 坐标 OpenglES

2011-08-09 16:25:16

Cocos2d视图坐标

2011-07-20 14:04:46

Cocos2d iPhone 游戏

2011-07-27 13:44:08

点赞
收藏

51CTO技术栈公众号