1、生成数据图
/** 生成地图数据 */
- (void)createMapData{
for (int i = 0; i < self.mapRow; i++) {
NSMutableArray *subList = [NSMutableArray array];
for (int j = 0; j < self.mapCol; j++) {
//外围辅0
if (i == 0 || j == 0 || i == self.mapRow - 1 || j == self.mapCol - 1) {
[subList addObject:@0];
}
//内围随机
else{
NSInteger value = random() % (ElementTypeNum + 1);
[subList addObject:[NSNumber numberWithInteger:value]];
}
}
[self.mapList addObject:subList];
}
///数据图
/////0000000000000/////
/////0273672362530/////
/////0565455354350/////
/////0354565346350/////
/////0544343452520/////
/////0000000000000/////
}
ElementTypeNum:代表多少种元素类型
数据图中大于0的数据都将按类型替换成图片
2、把数据换成图片
/** 创建连连看元素*/
- (void)createElement{
for (int i = 0; i < self.mapList.count; i++) {//遍历数据图
NSMutableArray *subList = self.mapList[I];
for (int j = 0; j < subList.count; j++) {
NSInteger type = [subList[j] integerValue];
if (i == 0 || j == 0 || i == self.mapRow - 1 || j == self.mapCol - 1 || type == 0) {
continue;//过滤掉0,0代表无图片
}
//生成元素
float elementX = (j % self.mapCol) * self.elementWH + Padding;
float elementY = (i % self.mapRow) * self.elementWH + self.topPadding;
__weak typeof(self) weakSelf = self;
LLKElement *element = [[LLKElement alloc] initWithFrame:CGRectMake(elementX, elementY, self.elementWH, self.elementWH) clickedBlock:^(UIButton *button) {
LLKElement *clickElement = (LLKElement *)button;
if (weakSelf.curElement) {
if (weakSelf.curElement.row == clickElement.row && weakSelf.curElement.col == clickElement.col && weakSelf.curElement.type == clickElement.type) {
//如果行列类型全相等 则表示用户点了两次该按钮
return;
}
weakSelf.oldElement = weakSelf.curElement;
}
weakSelf.curElement = clickElement;
[weakSelf checkElementConnection];
}];
//赋值坐标给元素
element.row = I;
element.col = j;
element.type = type;
[self.elementDic setValue:[NSNumber numberWithInteger:type] forKey:[NSString stringWithFormat:@"%d%d", i,j]];
[self addSubview:element];
}
}
}
(元素的点击事件可以先不看,以免影响理解)创建元素方法做了以下几件事
0:无元素占据,所以要过滤掉
按数据图给的数据生成元素,并把坐标和类型赋值给元素
以坐标为key,元素为value 存到元素字典
效果图:
顶上是时间条控制游戏进度的.png
网友评论