UIbutton实现打地鼠的小游戏
来源:互联网 发布:淘宝放心淘是什么意思 编辑:程序博客网 时间:2024/05/16 09:22
//
图片需要自己添加,主要使用了UIButton来实现打地鼠的操作.
1)button添加图片的时候最好使用setBackgroundImage:这个方法,这个图片可以根据按钮的大小不同的变化
2)button的命名,不是使用alloc 而是有自己的方法.
3)此游戏的逻辑不好想,不过一般人还是能看懂的.
// AppDelegate.m
// Move
//
// Created by Apple on 15/6/18.
// Copyright (c) 2015年 zhanghao. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
{
NSInteger _index;//记录上一次的tag值
NSInteger _count;//记录打击地鼠次数
BOOL _isLock; //是否锁上
}
@end
@implementation AppDelegate
-(void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
for (int i = 0; i<2; i++) {
for (int j=0; j<3; j++) {
//循环添加的控件都要加tag值
UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
button.frame = CGRectMake(10+(100+20)*j,20+(100+20)*i, 100, 100);
[button setBackgroundImage:[UIImage imageNamed:@"0.png" ] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//tag值
button.tag = 100 +(i*3+j);
[_window addSubview:button];
}
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 400, 80, 40)];
label.backgroundColor = [UIColor yellowColor];
label.tag = 200;
//text赋值
label.text = [NSString stringWithFormat:@"%ld",_count];
label.textAlignment =NSTextAlignmentCenter;
[_window addSubview:label];
//计时器
[ NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
//赋初值
_index = 100;
return YES;
}
-(void)buttonAction:(UIButton *)sender
{
if((sender.tag == _index)&&(_isLock == NO)){
//换图片
[sender setBackgroundImage:[UIImage imageNamed:@"4.png"] forState:(UIControlStateNormal)];
//取label
UILabel *tempLabel = (UILabel *)[_window viewWithTag:200];
//计数+1并显示
tempLabel.text = [NSString stringWithFormat:@"%ld",++_count];
//上锁
_isLock = YES;
}
}
-(void)timerAction:(NSTimer *)sender
{
UIButton *lastbutton = (UIButton *)[_window viewWithTag:_index];
[lastbutton setBackgroundImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal];
//随机tag值
_index = arc4random()%6+100;
//通过tag获取button
UIButton *tempButton = (UIButton *)[_window viewWithTag:_index];
//设置图片
[tempButton setBackgroundImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];
//解锁
_isLock = NO;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
- UIbutton实现打地鼠的小游戏
- JavaScript实现打地鼠小游戏
- Java打地鼠小游戏
- 打地鼠小游戏展示
- 打地鼠小游戏
- 小游戏 打地鼠
- 打地鼠小游戏
- Android打地鼠的实现
- MFC制作打地鼠小游戏
- 小游戏-打地鼠(js版)
- 打地鼠小游戏 版本一
- Java实现的打地鼠游戏
- Handler实现的打地鼠游戏
- 打地鼠游戏实现
- Qt5实现打地鼠
- js实现打地鼠
- 4.打地鼠实现
- android简单小游戏——打地鼠
- Linux学习笔记(一)--区分版本、挂载、目录结构
- 网络传输中send与recv需要注意的地方----------来自博客讨论
- 在编写unity各项功能的时候出现一些编码无误却不能执行的问题
- 丢失的勤奋,我需要静静
- Android PinnedSectionListView 收缩
- UIbutton实现打地鼠的小游戏
- Maven的pom.xml介绍
- Spark sort (排序)
- Linux学习笔记(二)--Linux启动顺序、常用命令
- C++的深拷贝与浅拷贝
- sql左外连接、右外连接、全外连接
- Linux学习笔记(三)--文件权限、重定向、设置系统启动级别、管道流
- Makefile文件解析与编写
- 关于android.view.WindowLeaked异常的解决方案