2013-01-08 124 views
0

林在我的應用程序移動精靈與此代碼:Cocos2d:如何在觸摸移動精靈時增加動量?

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

    UITouch *touch = [touches anyObject]; 

    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view]; 
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation]; 
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation]; 

    CGPoint translation = ccpSub(touchLocation, oldTouchLocation); 
    CGPoint newPos = ccpAdd(self.position, translation); 

    self.position = newPos; 
} 

它工作正常,但精靈作爲移動取景器是在屏幕上。

我想要實現的是刷卡後的某種動力, 就像iOS中應用程序的內部版本一樣。

任何人都可以給我一個如何做到這一點的暗示嗎?

+0

嘗試b2Mousejoint .. – Guru

回答

0

你可以簡單地得到速度(每秒像素)當你讓物體走,

(thisTouchPosition.x - lastTouchPosition.x)/(thisTouchTime - lastTouchTime) 

然後做一個比例因子「減速」(也以每秒0.0-1.0 PX)你想要對象在發佈時擁有。慢速下降速度越慢。而在你的渲染循環中,這樣的位置應該起作用。 FPS是你的週期利率。

SPEED *= SLOWDOWN 
x = x + SPEED/FPS 

如果你沒有一個渲染循環(你應該雖然),那麼你就需要做一個NSTimer和無效它的完成時或產生另一個線程來做到這一點。

這是未經測試的,但應該讓你去。

+0

此外,確保在低於某個(小)閾值時將SPEED重置爲0,以避免以低於像素速度的鋸齒狀動畫。 –

+0

thx但:((thisTouchTime - lastTouchTime)第一個是手指向上的時間?另一個當手指按在設備上? – mirzahat

+0

對不起,我有一陣子沒有在這裏。如果這是在您的渲染循環中,那麼thisTouchTime - lastTouchTime將只是您刷新之間的時間間隔。 – Dmacpro