Cocos2DLearning notesUIAccelerometerCase implementation is the content to be introduced in this article, mainly to understandUIAccelerometer.UIAccelerometerIt is used to detect the acceleration of the iphone on the x. y. Z axis. To obtain such calls.
- UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
At the same time, you need to set its delegate.
- UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
- accelerometer.delegate = self;
- accelerometer.updateInterval = 1.0/60.0;
Delegate method:-(void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) UIAcceleration in acceleration indicates the acceleration class. Contains real data from the accelerator's UIAccelerometer. 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, the user can erase the drawing by shaking the iphone.
You can also shake the iphone several times in a row and execute some special code:
- - (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) 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.
In cocos2d, The system receives the input from the accelerator to smooth the motion. Generally, the position of the object is not directly changed. Pass:
- - (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;
- }
- }
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.
- playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
Playervelocity is a velocity vector. Is cumulative.
- - (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;
- }
Summary:Cocos2DLearning notesUIAccelerometerThe implementation of the case is complete. I hope this article will help you!