我正在尝试构建模仿康威生命游戏的简单游戏(在线教程的帮助下)。我把网格放在屏幕上。
当用户点击网格时,它将禁用/启用点,当用户点击播放按钮时,网格上的点将根据康威的生命游戏规则发展。
然而,在完成所有代码之后出现问题,我尝试运行模拟(我在网格上启用了一些点)。
然后点击播放按钮,什么也没发生,我点击播放按钮两次,Xcode注销了这条消息:
GameOfLife2[823:43706] -[CCNode schedule:interval:repeat:delay:] : Selector 'step1' was already scheduled on <MainScene = 0x7fca01e69590 | Name = >
有关我的一些与此错误相关的方法的详细信息(发布在Mainscene.m上)
#import "MainScene.h"
#import "Grid.h"
@implementation MainScene {
Grid *_grid;
CCTimer *_timer;
CCLabelTTF *_generationLabel;
CCLabelTTF *_populationLabel;
}
- (id)init
{
self = [super init];
if (self) {
_timer = [[CCTimer alloc] init];
}
return self;
}
- (void)play
{
[self schedule:@selector(step1) interval:0.05f];}
- (void)pause
{
[self unschedule:@selector(step1)];
}
- (void)step1
{
[_grid evolveStep];
_generationLabel.string = [NSString stringWithFormat:@"%d", _grid.generation];
_populationLabel.string = [NSString stringWithFormat:@"%d", _grid.totalAlive];
}
@end
在Grid.m上
-(void)evolveStep{
[self countNeighbors];
[self updateCreatures];
_generation++;
}
-(void)countNeighbors
{
for (int i = 0; i < [_gridArray count]; i++)
{
for (int j = 0; j < [_gridArray[i] count]; j++)
{
Creature *currentCreature = _gridArray[i][j];
currentCreature.livingNeighbors = 0;
for (int x = (i-1) ; x <= (i+1); x++)
{
for (int y = (j-1); y <= (j+1); y++)
{
BOOL isIndexValid;
isIndexValid = [self isIndexValidForX:x andY:y];
if (!((x == i) && (y == j)) && isIndexValid)
{
Creature *neighbor = _gridArray[x][y];
if (neighbor.isAlive)
{
currentCreature.livingNeighbors +=1;
}
}
}
}
}
}
}
-(void)updateCreatures{
_totalAlive = 0;
for (int l = 0; l <= [_gridArray count]; l++) {
for (int k = 0; [_gridArray[l] count]; k++) {
Creature *currentCreature = _gridArray[l][k];
if (currentCreature.livingNeighbors == 3) {
currentCreature.isAlive = YES;
} else if ( (currentCreature.livingNeighbors <= 1) || (currentCreature.livingNeighbors >= 4) ) {
currentCreature.isAlive = NO;
}
if (currentCreature.isAlive) {
_totalAlive++;
}
}
}
}
尝试谷歌解决这个问题,但还没有解决方案。