摘要:本文将带你了解IOS开发入门ios开发中苹果2D引擎SpriteKit介绍,希望本文对大家学IOS有所帮助。
本文将带你了解IOS开发入门ios开发中苹果2D引擎SpriteKit介绍,希望本文对大家学IOS有所帮助。
ios开发中苹果2D引擎SpriteKit介绍,最近研究了苹果自家开发的2D引擎SpriteKit和3D引擎SceneKit,开篇之前,需要客观的讲,如果你要从事的是团队或者公司的项目,还是直接unity搞起,这涉及到开发与维护成本的问题,毕竟SpriteKit目前无法对跨平台给予支持。但是如果你是一个独立开发者,对苹果原生框架感兴趣,或者只关注与苹果的App Store,我想SpriteKit和SceneKit也是个不错的选择。
Sprite译作精灵,可以这样理解,在SpriteKit的世界里,游戏里的怪兽是一个精灵,主角与主角发射的炮弹也是一个精灵,或者说游戏里的一个不会动的背景图,也可以是一个精灵。下面以精灵为切入点,讲解一下一个充满野心的苹果弄出来的2D引擎。
一、精灵与场景
1.新建一个Xcode工程,可以看到,不管是iOS,还是macOS,甚至于tvOS,都有一个叫Game的项目新建方式。我们选择iOS的Game,新建出一个游戏项目。Game与普通工程项目有什么不同,其实就一点项目默认的GameViewController的view是以skView的形式load出来的,下面我们接着新建一个游戏场景SKScene,用以装载即将new出来的精灵。
2.场景的新建。
注:Scene场景的起始坐标是以左下角为(0,0)原点,而非传统view的左上角。
- (MenuScene *)menuScene {
if (!_menuScene) {
_menuScene = [[MenuScene alloc] initWithSize:self.view.bounds.size];
//@interface MenuScene : SKScene
//@end
}
return _menuScene;
}
- (void)viewDidLoad {
[super viewDidLoad];
[(SKView *)self.view presentScene:self.menuScene];
// Do any additional setup after loading the view, typically from a nib.
}
@implementation MenuScene
-(instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
#if TARGET_OS_IPHONE
#define SKColor UIColor
#else
#define SKColor NSColor
#endif
// SKColor主要是为了兼容mac的NSColor与iOS的UIColor
self.backgroundColor = [SKColor whiteColor];
[self addChild:self.titleNode];
[self addChild:self.pathLabelNode];
[self addChild:self.collLabelNode];
[self addChild:self.physLabelNode];
[self addChild:self.physCollNode];
}
return self;
}
3.游戏场景的切换。与初始化的页面一致,游戏的转场为使用presentScene跳转到新建的Scene当中。
- (SKLabelNode *)titleNode {
if (!_titleNode) {
_titleNode = ({
SKLabelNode *labelNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
labelNode.text = @"SpriteKit Test";
labelNode.fontSize = 30;
labelNode.fontColor = [SKColor blueColor];
labelNode.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height * 0.75);
labelNode.name = labelNode.text;
labelNode;
});
}
return _titleNode;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
[self changeToGameSceneWithNodeName:node.name];
}
}
-(void)changeToGameSceneWithNodeName:(NSString *)nodeName {
NSLog(@"nodeName=%@",nodeName);
if ([nodeName isEqualToString:self.pathLabelNode.name]) {
PathScene *pathScene = [PathScene sceneWithSize:self.size];
// 定制转场类型
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionUp duration:0.5];
[self.scene.view presentScene:pathScene transition:reveal];
}
else if ...
4.精灵的新建与添加。精灵的新建有两种形式,一个是直接以图片的形式新建,其size默认为图片的size,另一种则以纹理的形式新建。
// 以图片新建
- (SKSpriteNode *)player {
if (!_player) {
_player = [SKSpriteNode spriteNodeWithImageNamed:@"player"];
_player.name = @"player";
_player.position = CGPointMake(self.size.width /2, self.size.height /2);
}
return _player;
}
// 以纹理新建
- (SKSpriteNode *)walkMan {
if (!_walkMan) {
_walkMan = [SKSpriteNode spriteNodeWithImageNamed:@"walkR01"];
_walkMan.name = @"walkMan";
_walkMan.position = CGPointMake(self.player.position.x, CGRectGetMaxY(self.player.frame) + 30);
SKTexture * texture1 = [SKTexture textureWithImageNamed:@"walkR01"];
SKTexture * texture2 = [SKTexture textureWithImageNamed:@"walkR02"];
SKTexture * texture3 = [SKTexture textureWithImageNamed:@"walkR03"];
SKTexture * texture4 = [SKTexture textureWithImageNamed:@"walkR04"];
SKTexture * texture5 = [SKTexture textureWithImageNamed:@"walkR05"];
SKAction *animation = [SKAction animateWithTextures:@[texture1, texture2, texture3, texture4, texture5] timePerFrame:1];
SKAction *action = [SKAction repeatActionForever:animation];
[_walkMan runAction:action];
}
return _walkMan;
}
5、精灵的添加运动事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
// 添加武器
SKSpriteNode * arms = [SKSpriteNode spriteNodeWithImageNamed:@"projectile"];
arms.position = self.player.position;
[self addChild:arms];
CGPoint location = [touch locationInNode:self];
// 直线轨迹
// SKAction * moveToAction = [SKAction moveTo:location duration:0.5];;
// 持续增加
// SKAction * moveByAction = [SKAction moveByX:100 y:100 duration:0.3];
// 改变大小
// SKAction * sizeAction = [SKAction resizeByWidth:arms.size.width * 1.5 height:arms.size.height * 1.5 duration:0];
// 旋转
// SKAction * radiansAction = [SKAction rotateByAngle:M_PI * 4 duration:moveToAction.duration];
// 音效
// SKAction * armsSound = [SKAction playSoundFileNamed:@"pew-pew-lei.caf" waitForCompletion:NO];
SKAction * armsAction = [SKAction group:@[pathAction, sizeAction,armsSound]];
[arms runAction:armsAction completion:^{
// 移除
[arms removeFromParent];
}];
}
}
二、精灵的接触检测
SKScene有一个场景方法,改方法每帧都会触发一次,可供简单的事件分析与监测,比如精灵越界销毁,精灵的接触监测,故事板的得分情况的更新等等。
<a href="#"
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号