0
我正在使用CoreMotion收集加速計數據以更改精靈的位置。爲了做到這一點,你需要使用一個塊並更新到一個隊列。但是,這樣做與cocos2d scheduleUpdate衝突。這裏是核心self.motionManager代碼=Updatoqueue和Cocos2d scheduleUpdate衝突
[[CMMotionManager alloc] init];
if (motionManager.isAccelerometerAvailable) {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[self.motionManager
startAccelerometerUpdatesToQueue:queue
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
// Deceleration
float deceleration = 0.4f; // Controls how fast velocity deceerates/low = quck to change dir
// sensitivity
float sensitivity=0.6f;
//how fast the velocity can be at most
float maxVelocity=100;
// adjust the velocity basedo n current accelerometer acceleration
playerVelocity.x = playerVelocity.x*deceleration+accelerometerData.acceleration.x*sensitivity;
// we must limit the maximum velocity of the players sprite, in both directions
if (playerVelocity.x>maxVelocity) {
playerVelocity.x=maxVelocity;
}else if (playerVelocity.x < -maxVelocity) {
playerVelocity.x = -maxVelocity;
}
// schedule the -(void)update:(ccTime)delta method to be called every frame
[self scheduleUpdateWithPriority:0];
}];
}
,這是對我的日程表更新代碼:
/*************************************************************************/
//////////////////////////Listen for Accelerometer/////////////////////////
/*************************************************************************/
-(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 form 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 of 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;
}
我知道我可以繞過使用scheduleUpdate,把更新代碼的內部第一塊,但我仍然希望能夠使用scheduleUpdates。我如何解決這個問題?
非常感謝您的幫助。
P.S.這是我的錯誤
*** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason:
'CCScheduler: You can't re-schedule an 'update' selector'.
Unschedule it first'
謝謝,這總是顯而易見的,我想念。順便說一下,感謝您編寫您的書籍Learn Cocos2D,對於我通過對Cocoa環境和編程的全新掌握而感到欣喜。 – AlexHeuman