Cocos2d 中的 CGRect 逻辑

发布于 2024-12-23 06:45:02 字数 2573 浏览 1 评论 0原文

我正在将 2d 游戏从 UIKit 移植到 Cocos2d。其中一部分,为五个不同的方块设置框架,当我使用 UIKit 时,看起来像这样:

- (void)setFrames {
    [playerSquare setCenter:CGPointMake(160, 240)];

    for(int iii = 0; iii < [squares count]; iii++) {
        int randX = arc4random() % 321;
        int randY = arc4random() % 481;

        [(UIButton*)[squares objectAtIndex:iii] setFrame:CGRectMake(randX, randY, [(UIButton*)[squares objectAtIndex:iii] frame].size.width, [(UIButton*)[squares objectAtIndex:iii] frame].size.height)];

        CGRect playerRect = CGRectMake(80, 160, 160, 160);

        for(UIButton* b in squares) {
            if((CGRectIntersectsRect(b.frame, [(UIButton*)[squares objectAtIndex:iii] frame]) && ![b isEqual:[squares objectAtIndex:iii]])) {
                //make sure no two squares touch
                iii--;
                break;
            } else if(CGRectIntersectsRect(playerRect, [(UIButton*)[squares objectAtIndex:iii] frame])) {
                //no square is close to center of the screen
                iii--;
                break;
            } else if(!CGRectContainsRect(CGRectMake(10, 10, 300, 460), [(UIButton*)[squares objectAtIndex:iii] frame])) {
                //no square is close to the edges of the screen
                iii--;
                break;
            }
        }
    }
}

到目前为止,我试图在 cocos2d 中完成同样的事情:

- (void)setFrames {
    for(int idx = 0; idx < [squares count]; idx--) {
        int randX = arc4random() % 321;
        int randY = arc4random() % 481;

        [(CCSprite*)[squares objectAtIndex:idx] setPosition:ccp(randX, randY)];

        CGRect playerRect = CGRectMake(80, 160, 160, 160);

        for(CCSprite* b in squares) {
            if( (CGRectIntersectsRect(b.boundingBox, [(CCSprite*)[squares objectAtIndex:idx] boundingBox]) && ![b isEqual:[squares objectAtIndex:idx]]) ) {
                //make sure no two squares touch
                idx--;
                break;
            } else if(CGRectIntersectsRect(playerRect, [(CCSprite*)[squares objectAtIndex:idx] boundingBox])) {
                //no square is close to center of the screen
                idx--;
                break;
            } else if(!CGRectContainsRect(CGRectMake(10, 10, 300, 460), [(CCSprite*)[squares objectAtIndex:idx] boundingBox])) {
                //no square is close to the edges of the screen
                idx--;
                break;
            }
        }
    }
}

但这种修改后的方法不起作用。其中一个方块被放置在正确的位置,但其他四个总是在 cocos2d 点 0, 0 处生成。(屏幕左下角)有人能给我指出正确的方向吗?

I am porting a 2d game from UIKit to Cocos2d. One part of it, which sets the frame for five different squares, looked like this when I used UIKit:

- (void)setFrames {
    [playerSquare setCenter:CGPointMake(160, 240)];

    for(int iii = 0; iii < [squares count]; iii++) {
        int randX = arc4random() % 321;
        int randY = arc4random() % 481;

        [(UIButton*)[squares objectAtIndex:iii] setFrame:CGRectMake(randX, randY, [(UIButton*)[squares objectAtIndex:iii] frame].size.width, [(UIButton*)[squares objectAtIndex:iii] frame].size.height)];

        CGRect playerRect = CGRectMake(80, 160, 160, 160);

        for(UIButton* b in squares) {
            if((CGRectIntersectsRect(b.frame, [(UIButton*)[squares objectAtIndex:iii] frame]) && ![b isEqual:[squares objectAtIndex:iii]])) {
                //make sure no two squares touch
                iii--;
                break;
            } else if(CGRectIntersectsRect(playerRect, [(UIButton*)[squares objectAtIndex:iii] frame])) {
                //no square is close to center of the screen
                iii--;
                break;
            } else if(!CGRectContainsRect(CGRectMake(10, 10, 300, 460), [(UIButton*)[squares objectAtIndex:iii] frame])) {
                //no square is close to the edges of the screen
                iii--;
                break;
            }
        }
    }
}

So far, I have tried to accomplish the same thing in cocos2d:

- (void)setFrames {
    for(int idx = 0; idx < [squares count]; idx--) {
        int randX = arc4random() % 321;
        int randY = arc4random() % 481;

        [(CCSprite*)[squares objectAtIndex:idx] setPosition:ccp(randX, randY)];

        CGRect playerRect = CGRectMake(80, 160, 160, 160);

        for(CCSprite* b in squares) {
            if( (CGRectIntersectsRect(b.boundingBox, [(CCSprite*)[squares objectAtIndex:idx] boundingBox]) && ![b isEqual:[squares objectAtIndex:idx]]) ) {
                //make sure no two squares touch
                idx--;
                break;
            } else if(CGRectIntersectsRect(playerRect, [(CCSprite*)[squares objectAtIndex:idx] boundingBox])) {
                //no square is close to center of the screen
                idx--;
                break;
            } else if(!CGRectContainsRect(CGRectMake(10, 10, 300, 460), [(CCSprite*)[squares objectAtIndex:idx] boundingBox])) {
                //no square is close to the edges of the screen
                idx--;
                break;
            }
        }
    }
}

But this modified method doesn't work. One of the squares gets put in the proper position, but the other four always spawn at cocos2d point 0, 0. (bottom left corner of the screen) Can anyone point me in the right direction?

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

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

发布评论

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

评论(1

笑着哭最痛 2024-12-30 06:45:02

我刚刚发现了问题——不敢相信我做到了这一点。在该方法中的某一时刻,我将 for 循环设置为:

for(int idx = [squares count] - 1; idx >= 0; idx--)

而不是

for(int idx = 0; idx < [squares count]; idx++)

如您在上面的代码中看到的那样,我忘记将循环的减量部分更改为从减量增加。因此,仅设置第一个精灵的原因是循环从 0 开始,然后 idx 递减,直到超出 int 的下限并转到 int > 的上限,大于[平方数]。所以,我的新代码的唯一问题是 idx++ 和 idx-- 之间的差异。

令人沮丧的错误。

I just figured out the problem- can't believe I did this. At one point in this method, I had the for loop set up as:

for(int idx = [squares count] - 1; idx >= 0; idx--)

instead of

for(int idx = 0; idx < [squares count]; idx++)

As you can see in the code I have above, I forgot to change the decrement part of the loop to increment from decrement. So, the reason only the first sprite was being set was that the loop started at 0, then idx was decremented until it overran the lower bound of an int and went to an ints upper bound, which was more than [squares count]. So, the only problem with my new code was the difference between idx++ and idx--.

Frustrating mistake.

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