【发布时间】: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