Cocos2D Study Notes-UIAccelerometer

Source: Internet
Author: User

Cocos2DStudy NotesUIAccelerometerThe accelerator is the content to be introduced in this article.UIAccelerometerAcceleration is counted as an instance. Let's see the content.UIAccelerometerThe accelerator is used to detect the acceleration of the iphone on the x. y. Z axis. To obtain such calls:

 
 
  1. UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; 

At the same time, you need to set its delegate.

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

Delegate method:

 
 
  1. -(Void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) UIAcceleration in acceleration

Is an acceleration class. Included from the acceleratorUIAccelerometerReal data. It has three attributes: x, y, and z. The iphone accelerator supports round-robin at a rate of up to 100 times per second. This is 60 times.

1. The application can detect shaking through the accelerator. For example, you can erase the drawing by shaking the iphone.

You can also shake the iphone several times in a row and execute some special code:

 
 
  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. The most common accelerator is used as a game controller. Use the accelerator to control the movement of objects in the game! In simple cases, you may just need to get the value of an axis, multiply the sensitivity of a certain number), and then add it to the coordinate system of the controlled object. In a complex game, because the created physical model is more realistic, you must adjust the speed of the controlled object based on the value returned by the accelerator.

InCocos2dReceives the input from the accelerometer to make it smooth. Generally, the position of the object is not directly changed. Pass:

 
 
  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. }  

The deceleration is the ratio of deceleration and the sensitiation is the sensitivity. MaxVelocity is the maximum speed. If there is no limit, it will be difficult to stop when it increases.

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

Playervelocity is a velocity vector. Is cumulative.

 
 
  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;  

Summary:Cocos2DStudy NotesUIAccelerometerThe content of the accelerator is complete. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.