【问题标题】:memory game - define different amount of cards for each level记忆游戏 - 为每个级别定义不同数量的卡片
【发布时间】:2015-05-13 06:51:30
【问题描述】:

我想做一个简单的记忆配对游戏。 这是我的以下情况:我有一个levelselectionviewcontroller 和一个gameviewcontroller。在我的levelselectionviewcontroller 我有10 个按钮(10 个级别)。我想要,当我点击 1 级时,我的游戏视图控制器用 2 对组成 1 行。例如,当我单击第 10 级时,我希望 gameviewcontroller 可以用 3 对制作 2 行。我的最大对数是 11,我的最大行数是 4。

我的游戏视图控制器中的代码是

.h

#define CARDS_PER_ROW 4
#define NUMBER_OF_PAIRS 2

.m

-(void) generateCardViews {
int positionsLeftInRow = CARDS_PER_ROW;
int j = 0; // j = ROWNUMBER (j = 0) = ROW1, (j = 1) = ROW2...

for (int i = 0; i < [self.gameModel.cards count]; i++) {
    NSInteger value = ((CardModel *)self.gameModel.cards[i]).value;

    CGFloat x = (i % CARDS_PER_ROW) * 121 + (i % CARDS_PER_ROW) * 40 + 285;
    if (j == 1) {
        x += 80; // set additional indent (horizontal displacement)
    }
    if (j == 2) {
        x -= 160;
    }

    CGFloat y = j * 122 + j * 40 + 158;
    CGRect frame = CGRectMake(x, y, 125, 125);




    CardView *cv = [[CardView alloc] initWithFrame:frame andPosition:i andValue:value];

    if (!((CardModel *)self.gameModel.cards[i]).outOfPlay) {
        [self.boardView addSubview:cv];

       if ([self.gameModel.turnedCards containsObject: self.gameModel.cards[i]]) {
            [self.turnedCardViews addObject: cv];
            [cv flip];
        }
    }

    if (--positionsLeftInRow == 0) {
        j++;
        positionsLeftInRow = CARDS_PER_ROW;
        if (j == 1) {
            positionsLeftInRow = CARDS_PER_ROW-1;

        if (j == 2) {
            positionsLeftInRow = CARDS_PER_ROW-2;
        }}
    }
}
}

我的代码现在可以工作了,但我只能在我的 gameviewcontroller 的#define 宏中更改行和对。但是我如何在我的levelselectionviewcontroller 中#define 它? 我想要一个可重复使用的 n 个级别的游戏视图控制器。

我希望我的问题可以理解。

【问题讨论】:

    标签: ios objective-c cgrectmake


    【解决方案1】:

    如果我明白你在写什么,你可以在 gameviewcontroller.h 中创建这些参数(卡片和对)作为参数(而不是常量):

    @property (nonatomic, assign) int CARDS_PER_ROW;
    @property (nonatomic, assign) int NUMBER_OF_PAIRS;
    

    添加到gameviewcontroller.m:

    -(id)initWithCards:(int)numCards andPairs:(int)numPairs
    {
        self = [Gameviewcontroller init];
        if (self)
        {
            CARDS_PER_ROW = numCards;
            NUMBER_OF_PAIRS = numPairs;
        }
        return self;
    }
    

    在levelselectionviewcontroller.m中:

    switch (chosenLevel):
        case 1:
        {
            Gameviewcontroller *level = [[Gameviewcontroller alloc] initWithCards: CARDS_PER_ROW andPairs: NUMBER_OF_PAIRS];
            break;
        }
        .
        .
        .
        case 10:
        {
            Gameviewcontroller *level = [[Gameviewcontroller alloc] initWithCards: CARDS_PER_ROW andPairs: NUMBER_OF_PAIRS];
            break;
        }
    }
    

    【讨论】:

    • 谢谢我现在改了,但我的 gamemodel.m 说使用了未声明的标识符 _NUMBER_OF_ROWS。不知道为什么?我已经在我的 gamemodel.m 中导入了 gameviewcontroller.h
    • 这意味着您正在调用一个名为“NUMBER_OF_ROWS”的对象/参数/常量,它根本没有定义或没有在相关类中定义。在您的项目(命令 shift f )中搜索“NUMBER_OF_ROWS”,您就会明白为什么那里不知道该值。也许你从 gameviewcontroller.h 中删除了 NUMBER_OF_ROWS ??无论如何,如果我的回答是正确的 - 如果您将其标记为正确的,我将不胜感激。
    • 在我的 gamemodel.m 我有 #import "GameViewController.h" 并且错误发生在 -(NSMutableArray *) createNewDeck { NSMutableArray *cardDeck = [NSMutableArray array]; for (int i = 0; i
    • 在我的 gameviewcontroller.h 中有你的属性(@property (nonatomic, assign) int CARDS_PER_ROW; @property (nonatomic, assign) int NUMBER_OF_PAIRS;)
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多