0

我一直在 xCode 中开发 cocos2D 中的游戏,并且有一个问题要解决,这似乎超出了我的掌握。

本质上,我使用的是 SneakyButton,它是一个自定义类,可以捕获触摸事件,因此您可以使用 PNG 文件或圆形半径来做某事。在我的情况下,发射子弹。

当我在设备上以 SD 模式(1x 比例)运行 iOS(对于 iPhone 4 前视网膜)的项目时,显示一切运行良好。

但是,当我在设备上以 Retina 显示模式(2x 比例)运行项目时......我在应用程序的委托中将比例设置为 2X。可以看到PNG。一切正常,除了触摸没有出现。

我已将其缩小到代码的这一部分。

'
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    if (active) return NO;

    CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
    location = [self convertToNodeSpace:location];
        //Do a fast rect check before doing a circle hit check:
    if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
        return NO;
    }else{
        float dSq = location.x*location.x + location.y*location.y;
        if(radiusSq > dSq){
            active = YES;
            if (!isHoldable && !isToggleable){
                value = 1;
                [self schedule: @selector(limiter:) interval:rateLimit];
            }
            if (isHoldable) value = 1;
            if (isToggleable) value = !value;
            return YES;
        }
    }
return NO;
}
'
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    if (!active) return;

    CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
    location = [self convertToNodeSpace:location];
        //Do a fast rect check before doing a circle hit check:
    if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
        return;
    }else{
        float dSq = location.x*location.x + location.y*location.y;
        if(radiusSq > dSq){
            if (isHoldable) value = 1;
        }
        else {
            if (isHoldable) value = 0; active = NO;
        }
    }
}
'
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    if (!active) return;
    if (isHoldable) value = 0;
    if (isHoldable||isToggleable) active = NO;
}

- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
    [self ccTouchEnded:touch withEvent:event];
}

@end
'

我认为我的问题出在代码的半径部分......以前有没有人遇到过这个问题,或者你对我需要修改这段代码的哪一部分以使其工作有任何想法?这是我完成这个为期 6 个月的项目之前的最后一个绊脚石!帮助!也提前谢谢!

4

1 回答 1

0

你在哪里设置半径?或者你怎么得到它?如果您从 png 文件本身获取半径,请确保您使用边界框作为半径而不是 contentsize,它在应用任何缩放之前给出精灵的半径。

于 2011-06-10T09:54:34.157 回答