0

我正在我的游戏中编写撤消选项。这个游戏是一个简单的三消游戏,每个单元格都包含一个球,可以是 1-4 种颜色,所以我的想法是保存整个整数网格,我将它们全部放入 NSMutableArray 中,像这样...

NSMutableArray *currentGrid = [[NSMutableArray alloc] init];

    for (int i=0; i<(mapWidth*(mapHeight*2)); i++)
    {
        [currentGrid addObject:[NSNumber numberWithInt:0]];
    }


    for (int iy=(mapHeight*2)-1; iy>=0; iy--)
    {
        for (int ix=mapWidth-1; ix>=0; ix--)
        {
            int index = (iy*mapWidth)+ix;

            Ball *ball = [self valueAtRowWithArray:tilesArray andColumn:ix andRow:iy];

            [currentGrid setObject:[NSNumber numberWithInt:ball.ballType] atIndexedSubscript:index];
        }
    }

    [levelStateArray addObject:currentGrid];

这很好,但我想知道有更好/更有效的方法来做到这一点。我对序列化一无所知,但我想知道是否可以以某种方式使用它来保存数组?

4

1 回答 1

0

我不知道Objective C,所以我不能给出示例代码的方式(尽管有人可以编辑和添加)。

处理撤消方法的最佳方法是存储更改的内容而不是整个状态。因此,如果用户与网格单元 G 交互并将其颜色从 A 更改为 B,则仅存储 G 和 A。然后,如果他们点击撤消,则将网格单元 G 恢复为状态 A。

于 2013-06-30T01:44:30.593 回答