2012-08-27 120 views
0

如何將精靈移動到x軸上的觸摸點增加velocity.x。觸摸時間越長,速度越高,那麼當觸摸到某個範圍或用戶鬆開手指時,再次減慢速度?將精靈移動到觸摸點增加速度觸摸得越長

我有一個播放器類設置的速度值,它是在更新方法中更新,不知道如何獲得觸摸方法所需的行爲?

乾杯,

劉易斯

回答

2

這應該讓你在球場(實例變量,並添加它是可觸摸將在init,還是讓我知道,如果你需要的片段以及):

- (BOOL) ccTouchBegan: (UITouch *) touch 
      withEvent: (UIEvent *) event 
{ 
    _touchBeganAt = [self convertTouchToNodeSpace:touch]; 
    _velocityChangeSpeed = 1; 
} 


- (void) ccTouchEnded: (UITouch *) touch 
      withEvent: (UIEvent *) event 
{ 
    _velocityChangeSpeed = -1; 
} 

- (void) update:(ccTime)delta 
{ 
    velocityThreshold = 1; //? You can tune this 
    distanceThreshold = 1; //? Same 

    _sprite.velocity += _velocityChangeSpeed; 

    //So it comes to a complete stop, as opposed to moving backwards 
    if(_sprite.velocity < velocityThreshold) 
     _velocityChangeSpeed = 0; 

    float distanceFromTouchedPoint = ABS(_sprite.position.x - _touchBeganAt.x); 
    if(distanceFromTouchedPoint < distanceThreshold) 
     _velocity = 0; 
}