Cocos2D 中的 Retina 和 ccTouchBegan 问题

发布于 2024-11-15 04:58:28 字数 2349 浏览 1 评论 0原文

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

本质上,我使用的是 SneakyButton,它是一个捕获触摸事件的自定义类,因此您可以使用 PNG 文件或 Circle Radius 来执行某些操作。就我而言,发射子弹。

当我在 iOS(适用于 iPhone 4 视网膜之前)的设备上以 SD 模式(1 倍缩放)运行该项目时,显示所有运行正常。

但是,当我在设备上以 Retina 显示模式(2 倍缩放)运行项目时...我在应用程序的委托中将缩放设置为 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 个月的项目之前的最后一个绊脚石!帮助!也提前致谢!

I have been working in xCode on a game in cocos2D and have one problem to resolve which seems beyond my grasp.

Essentially I am using SneakyButton which is a custom class that captures touch events so you can use a PNG file or Circle Radius to do something. In my case fire bullets.

When I run the project on the device in SD mode (1x scale) for iOS (For pre iPhone 4 retina) displays all runs fine.

However when I run the project in in Retina display mode (2x scale) on the device... I set the scale to 2X in the delegate for the app. The PNG can be seen. All works fine except the touches are not showing up.

I have narrowed it down to this part of the code.

'
- (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
'

I think my problem is in the radius part of the code... Has anyone ran into this before or do you have any ideas of what part of this code I need to mod to make it work? It's my last stumbling block before completion of this 6 month project! help! Thanks in advance too!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2024-11-22 04:58:28

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

where are you setting the radius? or how do you get it? if ur getting radius from the png file itself make sure your using the boundingbox for radius rather then contentsize which gives the radius of the sprite before any scaling is applied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文