子彈和敵機(jī)以及動(dòng)畫
1.創(chuàng)建子彈票灰,位置與飛機(jī)位置關(guān)聯(lián)
2.創(chuàng)建敵機(jī),從屏幕上方出現(xiàn)
3.我覺得最好還是使用計(jì)時(shí)器吧惹盼,這個(gè)確實(shí)是方便了很多手报,而且很容易就能控制頻率掩蛤,缺點(diǎn)就是不能通過skview的pause屬性來暫停計(jì)時(shí)器揍鸟,不過這個(gè)暫時(shí)用不上
-(void)didMoveToView:(SKView *)view {
self.size = self.view.frame.size;
[self backgroundNode];
[self planeNode];
//創(chuàng)建一個(gè)手勢與panAction關(guān)聯(lián)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//添加手勢到view
[self.view addGestureRecognizer:pan];
//每0.5秒執(zhí)行一次bullet方法 target添self userinfo為nil 重復(fù)為YES
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(bullet) userInfo:nil repeats:YES];
//與上同理
[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(enemy) userInfo:nil repeats:YES];
}
一谈撒、子彈以及動(dòng)作
-(void)bullet {
SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithImageNamed:@"bullet2"];
bullet.name = @"bullet";
//同理子彈也是在bg的上層啃匿,zPosition = 1
bullet.zPosition = 1;
//子彈的位置始終和飛機(jī)的位置相關(guān)聯(lián)的,所以要獲取到飛機(jī)的位置
SKSpriteNode *plane = (SKSpriteNode *)[self childNodeWithName:@"plane"];
//為了方便計(jì)算位置設(shè)置anchorPoint為底部中間
bullet.anchorPoint = CGPointMake(0.5, 0);
bullet.position = CGPointMake(plane.position.x, plane.position.y + plane.size.height/2);
[self addChild:bullet];
//子彈的飛行動(dòng)作,這里要是用moveToY的話 當(dāng)飛機(jī)移動(dòng)到頂端時(shí)終點(diǎn)不變立宜,子彈飛行路程變短橙数,飛行時(shí)間不變灯帮,速度就會(huì)明顯變慢,所以使用moveBy迎献,x軸方向不做位移
SKAction *act = [SKAction moveByX:0 y:self.size.height duration:2];
[bullet runAction:act];
}
二扒秸、敵機(jī)以及動(dòng)作
-(void)enemy {
SKSpriteNode *enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy0"];
enemy.name = @"enemy";
enemy.zPosition = 1;
//enemy出現(xiàn)的位置的x軸是不固定的,最好不要把初始位置設(shè)置在屏幕內(nèi) x軸最好不要超出屏幕
//最小值enemy.size.width/2 隨機(jī)數(shù)0到self.size.width - enemy.size.width 整個(gè)x的范圍是enemy.size.width/2到self.size.width - enemy.size.width/2
CGFloat x = enemy.size.width/2 + arc4random()%(int)(self.size.width - enemy.size.width);
enemy.position = CGPointMake(x, self.size.height + enemy.size.height);
[self addChild:enemy];
[enemy runAction:[SKAction moveToY:-enemy.size.height duration:4]];
}