项目介绍:
模仿微信打飞机游戏,子弹打到敌机有碰撞效果,使用UIKit框架,以下为主要代码:
[Objective-C] 查看源文件 复制代码
//1.背景移动
-(void)beijing
{
bg1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ATGetDeviceWidth, ATGetDeviceHeight)];
bg1.image = [UIImage imageNamed:@"bg"];
[self.view addSubview:bg1];
bg2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, -480, ATGetDeviceWidth, ATGetDeviceHeight)];
bg2.image = [UIImage imageNamed:@"bg"];
[self.view addSubview:bg2];
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveBackgroundView) userInfo:nil repeats:YES];
}
//2.战机
-(void)zhanji
{
myPlane = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 60, 70)];
myPlane.image = [UIImage imageNamed:@"plane1"];
[self.view addSubview:myPlane];
NSArray* array = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"plane1"],[UIImage imageNamed:@"plane2"], nil];
//帧动画
myPlane.animationDuration = 0.2;
myPlane.animationImages = array;
[myPlane startAnimating];
}
//3.敌机
-(void)diji
{
//敌机数组
dijiArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i<20; i++) {
//(1)初始位置:放置屏幕外
UIImageView* diji = [[UIImageView alloc] initWithFrame:CGRectMake(arc4random()%(321-30), -30, 30, 30)];
diji.image = [UIImage imageNamed:@"diji"];
//(2)初始状态
diji.tag = 0;//未使用
[self.view addSubview:diji];
[dijiArray addObject:diji];
}
//找敌机0.2、 移动敌机0.02
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(findAndMoveDiji) userInfo:nil repeats:YES];
}
//4.子弹
-(void)zidan{
zidanArray = [[NSMutableArray alloc] init];
for (int i = 0; i<10; i++) {
//(1)初始位置:放在屏幕外
UIImageView* zidan = [[UIImageView alloc] initWithFrame:CGRectMake(0,-10, 3, 10)];
zidan.image = [UIImage imageNamed:@"zidan"];
//(2)初始状态
zidan.tag = 0;//未使用
[self.view addSubview:zidan];
[zidanArray addObject:zidan];
}
//找子弹。移动子弹
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(findAndMoveZidan) userInfo:nil repeats:YES];
}
