与我们一起制作游戏:生命游戏:Objective-C属性错误

时间:2014-05-26 23:27:04

标签: objective-c properties instance-variables conways-game-of-life spritebuilder

我一直试图通过“与我们一起制作游戏”来制作如何制作John Conway的“生命游戏”。我能够遵循大部分教程,直到我到达MainScene.m的步骤方法(这里是网站的link):

- (void)step
{
    [_grid evolveStep]
    _generationLabel.string = [NSString stringWithFormat:@"%d", _grid.generation];
    _populationLabel.string = [NSString stringWithFormat:@"%d", _grid.totalAlive];
}

错误属于同一类型;他们出现在_grid.generation和_grid.totalAlive。错误如下:

Property 'generation' not found on object of type 'Grid *'
Property 'totalAlive' not found on object of type 'Grid *'

我看过this link关于如何修复同一个问题,但我在SpriteBuilder中保存并正确发布了所有内容;用户显然已经解决了它,但我无法找到它。

更新:缺少财产声明(Grid.m):

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

// 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;
    int _generation; // This one
    int _totalAlive; // This one
}

/*Rest of the methods go here*/

@end

提前谢谢!

2 个答案:

答案 0 :(得分:2)

遗憾的是,我们的教程中存在错误。

确实,您需要向Grid.h添加两个属性:

@property (nonatomic, assign) int totalAlive;
@property (nonatomic, assign) int generation;

而不是将实例变量添加到Grid.m

该教程现已更新:https://www.makegameswith.us/tutorials/game-of-life-spritebuilder/game-of-life-code/

您还可以在GitHub上找到解决方案的完整代码: https://github.com/MakeGamesWithUs/GameOfLife.spritebuilder

很抱歉给您带来不便!

答案 1 :(得分:0)

错误消息告诉您,类generation没有名为Grid的属性。也许你对直接使用领先的" _" vs" self。"。

确保存在此类财产。更新问题以显示缺失属性的声明。

只需使用属性setter和getter,而不是直接访问它们。将所有ivars声明为属性。这可以创造一致性并减少错误。