最近,我刚刚开始从事iOS游戏编程的工作,发现一些令人困惑的地方。 (仅供参考,我正在使用在makegamewith.us上提供的代码来开发一个简单的游戏)

首先,我发现仅执行main函数。我的意思是,我们使用主要功能来激活iOS模拟器,以便能够加载游戏。然后我意识到断点仅适用于主要功能。尽管我在游戏中使用了一个函数来创建生物对象,但是当我在其他文件(例如游戏组件bio.m)中放置断点时,Xcode并不会停止在该函数上。将调用iOS模拟器,然后将自动加载游戏。

所以这是一个问题:那我该如何调试?

我假设在运行游戏时会调用该函数,但Xcode只会忽略其他文件中的其他函数(main.m中的main函数除外)。

另外,我遇​​到了几种“找不到成员变量”的情况。我想知道如何防止这种情况的发生。整个发布到Xcode的精灵生成器看起来很模糊。如果有人可以解释整个过程,我将不胜感激。

更新:

我意识到我没有明确调用我在其他文件中拥有的任何功能(例如,如下所示的Grid.m)。通过main函数,我的意思是main.m中的int main函数。所以问题可能是我没有在main中显式调用该函数? (但我认为main.m负责启动程序。)

在main.m中:

int main(int argc, char *argv[]) {

@autoreleasepool //if I put a breakpoint here this will definitely work
{
    int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
    return retVal;
    }
}


网格

#import "Grid.h"
#import "Creature.h"

// these are variables that cannot be changed
static const int GRID_ROWS = 8;
static const int GRID_COLUMNS = 10;

@implementation Grid {
    NSMutableArray *_gridArray;
    float _cellWidth;
    float _cellHeight;
}

- (void)onEnter
{
    [super onEnter];

    [self setupGrid];

    // accept touches on the grid
    self.userInteractionEnabled = YES;
}

- (void)setupGrid //****if I put breakpoint here, it doesn't work****
{
    // divide the grid's size by the number of columns/rows to figure out the right width and height of each cell
    _cellWidth = self.contentSize.width / GRID_COLUMNS;
    _cellHeight = self.contentSize.height / GRID_ROWS;

    float x = 0;
    float y = 0;

    // initialize the array as a blank NSMutableArray
    _gridArray = [NSMutableArray array];

    // initialize Creatures
    for (int i = 0; i < GRID_ROWS; i++) {
        // this is how you create two dimensional arrays in Objective-C. You put arrays into arrays.
    _gridArray[i] = [NSMutableArray array];
    x = 0;

    for (int j = 0; j < GRID_COLUMNS; j++) {
        Creature *creature = [[Creature alloc] initCreature];
            creature.anchorPoint = ccp(0, 0);
            creature.position = ccp(x, y);
            [self addChild:creature];

            // this is shorthand to access an array inside an array
            _gridArray[i][j] = creature;

            // make creatures visible to test this method, remove this once we know we have filled the grid properly
            creature.isAlive = YES;

            x+=_cellWidth;
        }

        y += _cellHeight;
    }
}

@end

最佳答案

看一下您的main()函数-几乎唯一要做的就是调用UIApplicationMain()。对于任何iOS应用程序都是如此。因此,实际的工作是由UIApplicationMain()完成的,我们应该对此进行了解。这是the description from the docs


  此函数从主体实例化应用程序对象
  类并实例化给定类中的委托(如果有),并且
  设置应用程序的委托。它还设置了主要事件
  循环,包括应用程序的运行循环,并开始处理
  事件。如果应用程序的Info.plist文件指定了主nib文件
  通过包含NSMainNibFile键和有效的nib文件来加载
  值的名称,此函数加载该nib文件。


因此,如果您的应用程序有问题,则可能与应用程序委托(您的AppController类)有关。在-[AppController application:willFinishLaunchingWithOptions:]方法中设置一个断点-当应用程序准备好运行时,这就是应用程序本身将对其委托进行调用的地方。您在UIApplicationMain()中的main()调用看起来不错,因此调试器应该在...didFinishLaunching...方法中命中一个断点。逐步执行该方法,并确保您正在设置窗口,设置根视图控制器等。如果不确定需要执行什么操作,请尝试创建一个新的单视图项目并查看提供的代码。那里。

07-28 02:36
查看更多