我不知道如何最好的方法是解釋我想要做的,但我會給它一個鏡頭。 所以我在屏幕上有一個足球,當我觸摸屏幕時,我可以通過屏幕拖動我的手指,通過使用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而不需要在原始點上。有沒有一種方法可以重置這一點,以便無論我在屏幕上的哪個位置,我都可以向足球方向拖拽,讓它減少,與增加相同。
我很感激幫助,但我不是數學天才。我希望我和你一樣聰明! – Stephen 2011-04-20 14:36:42