#在蓝懿学习iOS的日子#Day10今天做了一个大的游戏,涵盖了这段时间学习的知识,页面的切换字符串,还有可变数组和遍历等知识点。
-(void)initBG{
//添加背景图片
self.bgIV1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
//第二张图片放在第一张的上面,y:-self.view.frame.size.height
self.bgIV2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, -self.view.frame.size.height,self.view.frame.size.width, self.view.frame.size.height)];
self.bgIV1.image = [UIImage imageNamed:@"img_bg_level_1.jpg"];
self.bgIV2.image = [UIImage imageNamed:@"img_bg_level_1.jpg"];
//插入子控件到某个位置,把背景图片放最下边
[self.view insertSubview:self.bgIV1 atIndex:0];
[self.view insertSubview:self.bgIV2 atIndex:0];
//给 背景的移动设置TImer
-(void)moveBG{
//设置图片往下移动y+1
self.bgIV1.center = CGPointMake(self.bgIV1.center.x, self.bgIV1.center.y+1);
self.bgIV2.center = CGPointMake(self.bgIV2.center.x, self.bgIV2.center.y+1);
//两张图片的衔接
if (self.bgIV1.center.y>=self.view.frame.size.height*1.5) {
self.bgIV1.center = CGPointMake(self.bgIV1.center.x, -self.view.frame.size.height/2);
}
if (self.bgIV2.center.y>=self.view.frame.size.height*1.5) {
Hero *h = [[Hero alloc]initWithFrame:frame];
//用字符串对象变量图片
NSString *name = [NSString stringWithFormat:@"hero%ld.png",heroID];
//添加hero
self.hero = [Hero heroWithID:self.heroID andFrame:CGRectMake(150, 550, 80, 100)];
[self.view addSubview:self.hero];
}
//手指触摸屏幕的时候会调用此方法 从此方法中得到触摸屏幕的坐标
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//得到触摸的点
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
self.hero.center = p;
//子弹可变数组初始化
self.bullets = [NSMutableArray array];
//给子弹的出现添加timer
[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(addbulletAction) userInfo:nil repeats:YES];
//开启timer 移动子弹
[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(moveBullet) userInfo:nil repeats:YES];
}
-(void)moveBullet{
//遍历数组self.bullets
for (UIImageView *b in self.bullets) {
b.center = CGPointMake(b.center.x, b.center.y-5);
//当子弹飞出界面
if (b.center.y<0) {
[b removeFromSuperview];
[self.bullets removeObject:b];
break;
}
}
}
-(void)addbulletAction{
//添加子弹图片
UIImageView *bulletIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 20)];
bulletIV.image = [UIImage imageNamed:@"myb_2.png"];
//移动子弹
bulletIV.center = CGPointMake(self.center.x, self.center.y-20);
//将子弹图片添加进视图
[self.superview addSubview:bulletIV];
[self.bullets addObject:bulletIV];
@property (nonatomic)float speed;
//图片
@property (nonatomic, copy)NSString *name;
//timer
{
self = [super initWithFrame:frame];
if (self) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(moveAction) userInfo:nil repeats:YES];
}
return self;
}
-(void)moveAction{
self.center = CGPointMake(self.center.x, self.center.y+self.speed);
//离开页面的时候
if (self.center.y>667) {
[self.timer invalidate];
[self removeFromSuperview];
//在B类中通过self.delegate去调用A类中的方法
[self.delegate removeEnemy:self];
}
}
-(void)dead{
self.image = [UIImage imageNamed:@"dead_1.png"];
//让控件的透明度为0 并且以动画的形式展现
[UIView animateWithDuration:.5 animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
int type = arc4random()%4;
Enemy *e = nil;
switch (type) {
case 0:
e = [[EnemyA alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
break;
case 1:
e = [[EnemyB alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
break;
case 2:
e = [[EnemyC alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
break;
case 3:
e = [[EnemyD alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
break;
}
//在A类.m中创建B类的时候给B类的delegate赋值为self
e.delegate = self;
[self.view addSubview:e];
[self.enemys addObject:e];
[self.enemys removeObject:enemy];
//boss对象
Boss *boss = [[Boss alloc]initWithFrame:CGRectMake(100, -200, 200, 200)];
[self.view addSubview:boss];