Cocos2D學(xué)習(xí)筆記中UIAccelerometer加速計(jì)案例實(shí)現(xiàn)
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)用。
- UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
同時(shí),你需要設(shè)置它的delegate。
- UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
- accelerometer.delegate = self;
- 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í)行一些特殊的代碼:
- - (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) 加速計(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ò):
- - (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;
- }
- }
上面deceleration是減速的比率,sensitivity是靈敏度。maxVelocity是最大速度,如果不限制則一直加大就很難停下來(lái)。
- playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
中 playervelocity是一個(gè)速度向量。是累積的。
- - (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;
- }
小結(jié):Cocos2D學(xué)習(xí)筆記中UIAccelerometer加速計(jì)案例實(shí)現(xiàn)的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!