1

我正在使用 ccTouchBegan 和 ccTouchEnded,由于某种原因,有一段代码可以在 iPhone 模拟器上完美运行,而在 iPhone 4 上根本无法运行。这就是我的方法的样子:

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

firstTouch = [self convertTouchToNodeSpace:touch];

if (!self.currentFootball.footballHasEmbarked) {

    //arrow.position = ccp(fPos.x, fPos.y);
    float newAnc = (120 + (self.currentFootball.contentSize.width * .5f) + (arrow.contentSize.width * .5f)) / arrow.contentSize.width;
    arrow.anchorPoint = ccp(-newAnc, .5);//self.currentFootball.position;
    arrow.position = self.currentFootball.position;//ccp(fPos.x-120, fPos.y);
arrow.rotation = 180;
arrow.visible = YES;
arrow.scale = 0.5f;

    //powerLevel.position = fPos;
    powerLevel.position = self.currentFootball.position;
    powerLevel.rotation = 180;
powerLevel.percentage = 0;
powerLevel.visible = YES;

    outLine.position = self.currentFootball.position;
    outLine.rotation = 180;
    outLine.visible = YES;

    CCProgressFromTo *powerBarGoUp = [CCProgressFromTo actionWithDuration:1.0f from:0.0f to:100.0f];

    CCProgressFromTo *powerBarGoDown = [CCProgressFromTo actionWithDuration:1.0f from:100.0f to:0.0f];

    id action = [CCRepeatForever actionWithAction:[CCSequence actions:powerBarGoUp, powerBarGoDown,  nil]];
    [powerLevel runAction:action];

    return YES;
}
else {
    return NO;
}

}

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

const float max = 100;

CGPoint endTouch = [self convertTouchToNodeSpace:touch];

if (endTouch.x > firstTouch.x) {
endTouch = ccp(firstTouch.x, endTouch.y);
    //CCLOG(@"I ran");
}

arrow.visible = NO;
powerLevel.visible = NO;
    outLine.visible = NO;
self.currentFootball.footballHasEmbarked = YES;
self.currentFootball.spiraling = YES;

if (self.currentFootball) {

    [smgr addBody:self.currentFootball.body];

}

 if (CGPointEqualToPoint(endTouch, firstTouch)) {
    CCLOG(@" I have run");
    float   anchorPointDist = ccpLength(endTouch);

    float   distFactor = distFromFb + anchorPointDist;

    projectionAnchorPoint = ccp(firstTouch.x + distFactor,firstTouch.y);

}

    CGPoint diff = ccpSub(endTouch, projectionAnchorPoint);

    float len = powerLevel.percentage;

CGPoint norm = ccpNormalize(diff);

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

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

pos = self.currentFootball.position.y;

[self schedule:@selector(newFootball)]; 

}

这是不会在我的 iPhone 上运行的代码块。

if (CGPointEqualToPoint(endTouch, firstTouch)) {
    CCLOG(@" I have run");
    float   anchorPointDist = ccpLength(endTouch);

    float   distFactor = distFromFb + anchorPointDist;

    projectionAnchorPoint = ccp(firstTouch.x + distFactor,firstTouch.y);

}

我做的不对吗?

4

2 回答 2

2

打印出 endTouch 和 firstTouch 的值。它们在设备上的差异可能很小,因为与鼠标指针相比,将手指保持在同一位置更难。如果是这种情况,您可能希望在第一次触摸的范围内接受结束触摸。

于 2012-06-14T16:11:21.140 回答
2

也许您正在比较的点有一些细微的差别,使得比较的结果总是错误的。尝试使用此方法而不是 CGPointEqualToPoint。

BOOL ccpFuzzyEqual(CGPoint a, CGPoint b, float variance);
于 2012-06-14T16:12:33.157 回答