需求:現(xiàn)在有一個需求,創(chuàng)建一個飛船,飛船上有一個燈,燈隨著飛船的移動而移動
飛船移動.gif
使用節(jié)點構(gòu)建復(fù)雜的內(nèi)容
新的場景還沒有任何內(nèi)容,所以你要準備添加一個飛船到場景,要構(gòu)建飛船,你需要用到多個SKSpriteNode
對象來創(chuàng)造飛船和他表面的燈光.每個精靈節(jié)點都執(zhí)行動作
閃爍的燈光是飛船的一部分!如果飛船移動,燈光應(yīng)該和他一起移動.解決的辦法是使飛船節(jié)點成為燈光節(jié)點的父節(jié)點,同樣的場景將是飛船的父節(jié)點.光的坐標將要相對于父節(jié)點的位置來指定,而父節(jié)點是在子精靈圖像的中心.
1.添加飛船
SKSpriteNode *spaceship = [self newSpaceship];
spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:spaceship];
2.實現(xiàn)添加飛船的方法newSpaceship
// 創(chuàng)建飛船節(jié)點
- (SKSpriteNode *)newSpaceship {
SKSpriteNode *hull = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(200, 150)];
SKAction *hover = [SKAction sequence:@[
[SKAction waitForDuration:1.0],
[SKAction moveByX:100 y:50 duration:1.0],
[SKAction waitForDuration:1.0],
[SKAction moveByX:-100 y:-50 duration:1.0]
]];
[hull runAction:[SKAction repeatActionForever:hover]];
hull.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hull.size];
hull.physicsBody.dynamic = NO;
SKSpriteNode *light1 = [self creatLight];
light1.position = CGPointMake(-30.0, 6.0);
[hull addChild:light1];
SKSpriteNode *light2 = [self creatLight];
light2.position = CGPointMake(30.0, 6.0);
[hull addChild:light2];
return hull;
}
此方法創(chuàng)建了一個飛船的船體,并添加了一個簡短的動畫,運行后你會看到一個矩形
3.創(chuàng)建燈光節(jié)點
// 創(chuàng)建燈光節(jié)點
- (SKSpriteNode *)creatLight {
SKSpriteNode *light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:CGSizeMake(30, 30)];
SKAction *blink = [SKAction sequence:@[
[SKAction fadeOutWithDuration:0.25],
[SKAction fadeInWithDuration:0.25]
]];
SKAction *blinkForever = [SKAction repeatActionForever:blink];
[light runAction:blinkForever];
return light;
}
運行后你會看到一對燈光在飛船上,飛船移動時,燈光隨著移動
以上就是核心代碼,關(guān)于添加的巖石,以及物理系統(tǒng)碰撞在下面的源碼之中,這里就不做講解
@implementation ZNBSceneA
- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor darkGrayColor];
self.physicsWorld.gravity = CGVectorMake(0.0, -9.0);
}
return self;
}
- (void)didMoveToView:(SKView *)view {
[super didMoveToView:view];
SKSpriteNode *spaceship = [self newSpaceship];
spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:spaceship];
[self makeRock];
}
// 創(chuàng)建巖石方法
- (void)makeRock {
SKAction *makeRocks = [SKAction sequence:@[
[SKAction performSelector:@selector(addRock) onTarget:self],
[SKAction waitForDuration:0.1 withRange:0.15]
]];
[self runAction:[SKAction repeatActionForever:makeRocks]];
}
// 創(chuàng)建燈光節(jié)點
- (SKSpriteNode *)creatLight {
SKSpriteNode *light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:CGSizeMake(30, 30)];
SKAction *blink = [SKAction sequence:@[
[SKAction fadeOutWithDuration:0.25],
[SKAction fadeInWithDuration:0.25]
]];
SKAction *blinkForever = [SKAction repeatActionForever:blink];
[light runAction:blinkForever];
return light;
}
// 創(chuàng)建飛船節(jié)點
- (SKSpriteNode *)newSpaceship {
SKSpriteNode *hull = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(200, 150)];
SKAction *hover = [SKAction sequence:@[
[SKAction waitForDuration:1.0],
[SKAction moveByX:100 y:50 duration:1.0],
[SKAction waitForDuration:1.0],
[SKAction moveByX:-100 y:-50 duration:1.0]
]];
[hull runAction:[SKAction repeatActionForever:hover]];
hull.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hull.size];
hull.physicsBody.dynamic = NO;
SKSpriteNode *light1 = [self creatLight];
light1.position = CGPointMake(-30.0, 6.0);
[hull addChild:light1];
SKSpriteNode *light2 = [self creatLight];
light2.position = CGPointMake(30.0, 6.0);
[hull addChild:light2];
return hull;
}
static inline CGFloat skRandf() {
return rand()/(CGFloat)RAND_MAX;
}
static inline CGFloat skRand(CGFloat low, CGFloat high) {
return skRandf()*(high - low) + low;
}
- (void)addRock {
SKSpriteNode *rock = [[SKSpriteNode alloc] initWithColor:[SKColor brownColor] size:CGSizeMake(10, 10)];
rock.position = CGPointMake(skRand(0, self.size.width), self.size.height-50);
rock.name = @"rock";
rock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rock.size];
rock.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:rock];
}
-(void)didSimulatePhysics {
[self enumerateChildNodesWithName:@"rock" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
// 刪除不在屏幕的節(jié)點,否則節(jié)點會越來越多
if (node.position.y < 0) {
[node removeFromParent];
}
}];
}