如果没有Cocos2D中的CCLOG,代码将无效

时间:2013-04-03 23:01:56

标签: iphone ios objective-c cocos2d-iphone

我正在开发一款可以随机为玩家生成地牢的游戏。它由从不同文件调用的一大堆不同方法组成。我好像有代码可以计算出门的另一边有多少空间。

然而有一个问题;当我使用CCLOG输出到控制台时,代码似乎只能工作。我在没有测试的情况下编写了很多代码,然后决定然后逐步完成它以查看代码是如何工作的(我想我会让它运行时没有错误,然后我会检查我的值)。

在我确定代码成功检查每个方向的可用空间后,在列出已检查的位置时,我决定删除CCLOG输出。不幸的是,这样做导致代码停止工作。

//First check "forward"
bottom = CGPointMake(startLocation.x + forwardDirectionModifier.x, startLocation.y + forwardDirectionModifier.y);

top = bottom;

do
{
    currentLocation = CGPointMake(top.x + forwardDirectionModifier.x, top.y + forwardDirectionModifier.y);
    tileType = [tileCache getTileType:currentLocation];
    if (tileType == TileTypeFiller)
    {
        top = currentLocation;
        CCLOG(@"Top location is %@", NSStringFromCGPoint(top));
    }

} while (tileType == TileTypeFiller || top.y != 63);

这是代码中的一小段代码;这是我认为很可能是问题的代码部分。基本上问题是,如果我注释掉或删除行CCLOG(@"Top location is %@", NSStringFromCGPoint(top));,它将停止工作。

以下是一些更多细节:

  • 我有几个像这样的小块代码,只要我在每个CCLOG中,就可以移动到下一个。如果我要评论其中的任何一个,它将停止进入下一个块。
  • 我可以更改CCLOG以输出任何内容,它仍然有效,只需要在那里。
  • 我试过清理这个项目。
  • 有更多的CCLOG未在任何循环中使用,可以删除它们而不会产生任何后果。

有没有人有解决方案?据我所知,无论是否向控制台输出内容,它都不会对代码是否执行产生影响。

提前致谢。

编辑:在Ben Trengrove的建议中,我在进一步举例说明CCLOG的使用地点。

此代码紧跟先前列出的代码。

//Check left relative to door
farLeft = CGPointMake(startLocation.x + leftDirectionModifier.x, startLocation.y + leftDirectionModifier.y);

do
{
    currentLocation = CGPointMake(farLeft.x + leftDirectionModifier.x, farLeft.y + leftDirectionModifier.y);
    tileType = [tileCache getTileType:currentLocation];
    if (tileType == TileTypeFiller)
    {
        farLeft = currentLocation;
        CCLOG(@"Far Left location is %@", NSStringFromCGPoint(farLeft));
    }

} while (tileType == TileTypeFiller || farLeft.x !=0);

//Check forwards from far left
top2 = farLeft;

do
{
    currentLocation = CGPointMake(top2.x + forwardDirectionModifier.x, top2.y + forwardDirectionModifier.y);
    tileType = [tileCache getTileType:currentLocation];
    if (tileType == TileTypeFiller)
    {
        top2 = currentLocation;
        CCLOG(@"Top2 location is %@", NSStringFromCGPoint(top2));
    }

} while ((tileType == TileTypeFiller)|| (top2.y != 63));

2 个答案:

答案 0 :(得分:0)

当currentLocation没有tile时,tile缓存作为tileType返回什么? TileTypeFiller有机会吗?如果是的话,在适当的情况下,你有一个几乎肯定永无止境的循环..

编辑:

请在循环后添加一行

CCLOG(@"");

并在该行上放置一个断点。看看程序是否停在那里。

答案 1 :(得分:0)

所以这个问题现在已经解决了,似乎我的逻辑部分出现了问题。 以下是我现在所做的工作:

do
{
    currentLocation = CGPointMake(top.x + forwardDirectionModifier.x, top.y + forwardDirectionModifier.y);
    tileType = [tileCache getTileType:currentLocation];
    if (tileType == TileTypeFiller)
    {
        top = currentLocation;
    }
    locationCount++;

} while ((tileType == TileTypeFiller) && (locationCount != 15));
CCLOG(@"Top location is %@", NSStringFromCGPoint(top));

改变是我已经切换到AND而不是OR。我也改变了第二部分,因为我意识到由于我的最大房间大小,我从来不需要在任何给定方向上检查超过15个位置。

非常感谢所有帮助过的人。我仍然不完全确定CCLOG的使用是如何以某种方式帮助解决逻辑错误但至少它现在正在运作。