|
// 小怪物
SKSpriteNode *character = [SKSpriteNode spriteNodeWithImageNamed:@"player1"];
character.position = CGPointMake(60 + character.size.width/2, self.size.height - character.size.height/2);
[self addChild:character];
// 叫声
SKAction *soundAction = [SKAction playSoundFileNamed:@"MC.mp3" waitForCompletion:NO];
[character runAction:soundAction];
同时出现一个漩涡,漩涡旋转两圈并消失。
// 出现漩涡
SKSpriteNode *wheel = [SKSpriteNode spriteNodeWithImageNamed:@"wheel"];
wheel.position = CGPointMake(character.position.x, character.position.y - 10);
[self addChild:wheel];
// 旋转两圈后消失
SKAction *rotateAction = [SKAction rotateByAngle:4 * M_PI duration:0.4];
[wheel runAction:rotateAction completion:^{
[wheel removeFromParent];
}];
小怪物蹦蹦跳跳的动画,与仙人掌呼吸动画一样。
NSArray *array = @[[SKTexture textureWithImageNamed:@"player1"],
[SKTexture textureWithImageNamed:@"player2"],
[SKTexture textureWithImageNamed:@"player3"],
[SKTexture textureWithImageNamed:@"player1"]];
SKAction *animation = [SKAction animateWithTextures:array timePerFrame:0.15];
[character runAction:[SKAction repeatActionForever:animation]];
按照固定路线行走。(正常需要根据路线、屏幕尺寸等计算出,这里给的定值)
CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathMoveToPoint(pathRef, nil, character.position.x, character.position.y);
CGPathAddLineToPoint(pathRef, nil, 83, 165);
CGPathAddLineToPoint(pathRef, nil, 245, 165);
CGPathAddLineToPoint(pathRef, nil, 245, 225);
CGPathAddLineToPoint(pathRef, nil, 415, 225);
CGPathAddLineToPoint(pathRef, nil, 415, 160);
CGPathAddLineToPoint(pathRef, nil, 580, 160);
CGPathAddLineToPoint(pathRef, nil, 580, self.size.height - character.size.height/2);
SKAction *moveToEndAction = [SKAction followPath:pathRef asOffset:NO orientToPath:NO duration:10];
[character runAction:moveToEndAction completion:^{
// 走完全程后,移除小怪物
[character removeFromParent];
[self.monsters removeObject:character];
}];
// 将小怪物暂存起来,便于后续跟炮台的飞镖做碰撞时使用。
[self.monsters addObject:character];
CGPathRelease(pathRef);
这里通过followPath:asOffset:orientToPath:duration:函数来让精灵按CGPathRef路线移动10秒。self.monsters这个成员变量是用来存储每一个生成出来的小怪物,如果小怪物走完全程也要相应的移除掉。
我们来连续的创建这样的小怪物7个,时间间隔1秒。
typeof(self) weakSelf = self;
SKAction *actionWaitNextMonster = [SKAction waitForDuration:1];
SKAction *actionAddMonster = [SKAction runBlock:^{
// 添加小怪物的所有动作行为,出场、叫声、行动路线等
[weakSelf addMonster];
}];
SKAction *sequenceAction = [SKAction sequence:@[actionAddMonster, actionWaitNextMonster]];
[self runAction:[SKAction repeatAction:sequenceAction count:7] completion:^{
// isFinish用来标记怪物全部出场
weakSelf.isFinsh = YES;
以上内容就是[2d游戏引擎]iOS 2D游戏引擎框架SpriteKit入门的相关内容介绍,喜欢侠外游戏论坛的朋友可以关注我们。
上一页123456下一页 |
|