2011-04-19 45 views
0

我不知道如何最好的方法是解釋我想要做的,但我會給它一個鏡頭。 所以我在屏幕上有一個足球,當我觸摸屏幕時,我可以通過屏幕拖動我的手指,通過使用CCProgressTimer給我一個投擲力量的表示。換句話說,就像在屏幕上有一個音量條,當我觸摸屏幕並從音量條上拖走時,音量條開始增加,並且向音量條拖動時會減少它。所以我的問題是,當我觸摸屏幕時,我正在記錄我的觸摸,並且觸摸是所有計算都基於的,但我想要一些如何能夠無論我在屏幕上的哪個位置都能夠減少或增加。現在它的工作方式是,我不能開始減少投擲的力量,直到我達到記錄的觸摸。當我在屏幕上的任何位置時,如果我拖回足球,在屏幕上達到原始觸摸之前將其減少,是否有可能使其發揮作用?這是我的代碼。幫助移動CGPoint(cocos2d)

footballPath = [CCProgressTimer progressWithFile:@"footballPath.png"]; 
      footballPath.visible = NO; 
      footballPath.percentage = 0; 
      footballPath.type = kCCProgressTimerTypeHorizontalBarLR; 
      footballPath.anchorPoint = ccp(0, 0.5); 
      [self addChild:footballPath]; 

-(BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event { 

    cachedTouchPt = [self convertTouchToNodeSpace:touch]; 

    if (!self.currentFootball.footballHasEmbarked) { 
     footballPath.percentage = 0; 
     [self updateArrowWithTouch:touch]; 
     arrow.visible = YES; 
     footballPath.visible = YES; 

     return YES; 
    } 
    else { 
     return NO; 
    } 

} 

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { 

    [self updateArrowWithTouch:touch]; 

} 

-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event { 

    const float max = 100; 
    CGPoint touchPt = [self convertTouchToNodeSpace:touch]; 
    if (touchPt.x > self.currentFootball.position.x) { 
     touchPt = ccp(self.currentFootball.position.x, touchPt.y); 
    } 

    arrow.visible = NO; 
    footballPath.visible = NO; 
    self.currentFootball.footballHasEmbarked = YES; 
    self.currentFootball.spiraling = YES; 

    if (self.currentFootball) { 
     [smgr morphShapeToActive:self.currentFootball.shape mass:25]; 
    } 

    diff = ccpSub(touchPt, self.currentFootball.position); 

    if (diff.x == 0 && diff.y == 0) { 
     diff = ccp(1, 1); 
    } 
    float len = footballPath.percentage; 
    CGPoint norm = ccpNormalize(diff); 

    if (len > max){ 
     len = max; 
    } 

    [self.currentFootball applyImpulse:ccpMult(norm, (len * 245))]; 

    pos = self.currentFootball.position.y; 

    [self schedule:@selector(newFootball)]; 

} 

- (void) updateArrowWithTouch: (UITouch*) touch { 

    const float distFromFb = 100; 
    CGPoint touchPt = [self convertTouchToNodeSpace:touch]; 
    if (touchPt.x > self.currentFootball.position.x) { 
     touchPt = ccp(self.currentFootball.position.x, touchPt.y); 
    } 

    CGPoint fpt = self.currentFootball.position; 

    CGPoint vect = ccpSub(touchPt, fpt); 
    float dist = ccpLength(vect); 
    CGPoint vect2 = ccpSub(touchPt, cachedTouchPt); 
    float dist2 = ccpLength(vect2); 
    float degrees = -CC_RADIANS_TO_DEGREES(ccpToAngle(vect)); 
    float factor = dist2; 
    CGPoint normalVect = ccpMult(vect, 1/dist); 

    factor = distFromFb; 

    CGPoint newPoint = ccpAdd(fpt, ccpMult(normalVect, factor)); 
    if (newPoint.x < self.currentFootball.position.x+1) { 
     arrow.rotation = degrees; 
     arrow.position = newPoint; 

     self.currentFootball.rotation = -CC_RADIANS_TO_DEGREES (ccpToAngle(ccpSub(touchPt, self.currentFootball.position))); 
     footballPath.rotation = self.currentFootball.rotation; 
     footballPath.position = fpt; 
    } 

    float percentage = dist - ccpLength(ccpSub(cachedTouchPt, self.currentFootball.position)); 

    if (percentage < 0.0f){ 

     percentage = 0.0f; 
    } 

// CCLOG(@"cachedDist = %f", cachedDist); 
    CCLOG(@"percentage = %f", percentage); 

     diff = vect2; 
     footballPath.percentage = percentage; 

} 

cachedTouchPt = [self convertTouchToNodeSpace:touch];是我談論的原始觀點。所以當我觸摸屏幕時,它會創建一個新點並將其存儲在cachedTouchPt中,因此我無法減少power/CCProgressTimer,直到達到cachedTouchPt的X和Y位置。如果我沒有理解我想說的話。我需要能夠減少功率/ CCProgressTimer而不需要在原始點上。有沒有一種方法可以重置這一點,以便無論我在屏幕上的哪個位置,我都可以向足球方向拖拽,讓它減少,與增加相同。

回答

0

我會通過向您顯示數學來回答您的問題,但您需要自己將其轉換爲代碼。

首先,你需要決定(您可能沒有)的持續時間(分)不觸摸需要移動,以增加從0到1的投擲力量讓我們將此值D.

然後,讓我們參考touch和Tx開始的座標。

然後,當觸摸移動到新的座標UX和UY,你觸摸已經從(TX,泰),使用式移動的距離:

E = sqrt(pow(Ux - Tx, 2) + pow(Uy - Ty, 2)) 

你使用公式計算功率:

P = E/D 

到目前爲止,我認爲你的代碼已經在做所有這些計算。但是接下來會發生的是如果玩家仍然將觸摸從觸摸開始點移動超過距離D,那就是E > D

首先,把一個IF塊:

if (E > D) { ... } 

所以,現在要修改Tx和Ty值(即觸摸開始點),因此,從目前的觸摸位置決定該座標的距離d這裏是神奇的公式:

angle = atan2(Uy - Ty, Ux - Tx) 
Ty = Uy - D * sin(angle) 
Tx = Ux - D * cos(angle) 

在這一點上,你可能想的功率值修改爲1:

P = 1 
E = D 

就是這樣!該公式將觸摸開始點移向當前觸摸位置,以保持E <= D的條件。當然,玩家將觸摸移動到觸摸開始點的情況會被照顧。

祝你好運!

+0

我很感激幫助,但我不是數學天才。我希望我和你一樣聰明! – Stephen 2011-04-20 14:36:42