Sprite Kit很早就有了,但是大家可能更多的是知道Cocos 2d或者Cocos 2dx,小编我是先学习Cocos 后面看了一个星期的Sprite Kit的文档。
为什么小编一个星期就能学完Sprite Kit ,因为早期Cocos 2d是用OC语言来写的,后面Cocos 2dx就开始要使用C++了,然后苹果公司出了Sprite Kit ,然后Api跟Cocos 2d就一模一样了。
小编今天就跟大家一起学习Sprite Kit !
*1.1创建工程
A1.png*1.2填写工程信息
A2.png*2.1第一次运行工程
A3.png*2.2GameViewController介绍
这里就是游戏的入口处,前不久我看了下Cocos 2dx也把opengl那段加载代码省去了
/*创建skview,,cocos2d里面叫Director(导演),
这里再把隐藏代码讲一下,就是大多数简单的游戏其实用uiview的动画跟frame可以做出来,但是游戏开发的动画相对比较多,定时器也比较多,用uivew就会容易造成卡屏的现象,
所以用到的是opengl,最小的帧能够到1/60,当然精灵过多还是会卡屏,那时候就要用opengles重写opengl
*/
SKView * skView = (SKView *)self.view;
/*显示FPS*/
skView.showsFPS = YES;
/*显示精灵个数*/
skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
/*创建场景,然后跳转到场景*/
GamePlayReady *scene = [[GamePlayReady alloc]initWithSize:self.view.frame.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// 跳转到场景当中
[skView presentScene:scene];
*2.3创建场景
A4.png/*将下面的代码替换成*/
// GameScene *scene = [GameScene nodeWithFileNamed:@"GameScene"];
// scene.scaleMode = SKSceneScaleModeAspectFill;
//
// // Present the scene.
// [skView presentScene:scene];
/*创建场景,然后跳转到场景*/
GamePlayReady *scene = [[GamePlayReady alloc]initWithSize:self.view.frame.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
A5.png这时候你运行会是这样的情况
*3.1创建精灵
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
#import "GamePlayReady.h"
#import "GamePlayFlight.h"
@implementation GamePlayReady
#pragma mark 注意这里就类似viewdidlod,所有的场景都是最先进入这个方法
-(void)didMoveToView:(SKView *)view {
/*应用开发的uivew这里叫SKSpriteNode(中文:精灵。。。实际就是图片、文字、颜色都可以作为精灵)*/
SKSpriteNode *backNode=[self CreateBackNode];
//将精灵添加
[self addChild:backNode];
}
-(SKSpriteNode *)CreateBackNode
{
//创建精灵
SKSpriteNode *backNode=[[SKSpriteNode alloc]initWithImageNamed:@"background0"];
/*注意游戏开发并没有frame只有CGPoint,大小一般都是游戏的大小,如果你要它的size要用action去改变他的size*/
backNode.position=CGPointMake(Width/2, Height/2);
return backNode;
}
A6.png现在运行应该是这样的,有背景。
*3.2创建Action
在-(void)didMoveToView:(SKView *)view里面添加如下代码
-(void)didMoveToView:(SKView *)view {
/*应用开发的uivew这里叫SKSpriteNode(中文:精灵。。。实际就是图片、文字、颜色都可以作为精灵)*/
SKSpriteNode *backNode=[self CreateBackNode];//创建背景精灵
[self addChild:backNode];
SKSpriteNode *startNode=[self CreateStartNode];//创建start精灵
[self addChild:startNode];
SKSpriteNode *pesonNode=[self CreatePesonNode];//创建peson精灵
[self addChild:pesonNode];
SKSpriteNode *logoNode=[self CreateLogoNode];//创建logo精灵
[self addChild:logoNode];
SKSpriteNode *grilNode=[self CreateGrilNode];//创建gril精灵
[self addChild:grilNode];
}
//创建背景
-(SKSpriteNode *)CreateBackNode
{
SKSpriteNode *backNode=[[SKSpriteNode alloc]initWithImageNamed:@"background0"];
/*注意游戏开发并没有frame只有CGPoint,大小一般都是游戏的大小,如果你要它的size要用action去改变他的size*/
backNode.position=CGPointMake(Width/2, Height/2);
return backNode;
}
//创建纹理动画
-(SKSpriteNode *)CreateGrilNode
{
SKSpriteNode *girlNode=[[SKSpriteNode alloc]initWithImageNamed:@"background0"];
/*注意游戏开发并没有frame只有CGPoint,大小一般都是游戏的大小,如果你要它的size要用action去改变他的size*/
girlNode.position=CGPointMake(25, 45);
//纹理添加资源
SKTexture *w01 = [SKTexture textureWithImageNamed:@"walkR01"];
SKTexture *w02 = [SKTexture textureWithImageNamed:@"walkR02"];
SKTexture *w03 = [SKTexture textureWithImageNamed:@"walkR03"];
SKTexture *w04 = [SKTexture textureWithImageNamed:@"walkR04"];
SKTexture *w05 = [SKTexture textureWithImageNamed:@"walkR05"];
SKAction *resizeToWH = [SKAction resizeToWidth:50 height:90 duration:0];//Action改变大小
[girlNode runAction:resizeToWH];
SKAction *move=[SKAction moveToX:Width-25 duration:30];//Action移动到某个位置 duration移动需要的时间
[girlNode runAction:move completion:^{
//move Action执行完,之后执行move2
SKAction *move2=[SKAction moveToX:25 duration:30];
[girlNode runAction:move2];
}];
SKAction *animation = [SKAction animateWithTextures:@[w01, w02, w03, w04, w05] timePerFrame:0.1];
//重复Action
SKAction *action = [SKAction repeatActionForever:animation];
[girlNode runAction:action];
return girlNode;
}
//创建Start精灵
-(SKSpriteNode *)CreateStartNode
{
SKSpriteNode *backNode=[[SKSpriteNode alloc]initWithImageNamed:@"btn_battle_1"];
backNode.position=CGPointMake(Width/2, Height/2);
return backNode;
}
//创建Peson精灵
-(SKSpriteNode *)CreatePesonNode
{
SKSpriteNode *backNode=[[SKSpriteNode alloc]initWithImageNamed:@"img_head-_shenmiren"];
backNode.position=CGPointMake(200, 200);
return backNode;
}
//创建Logo精灵
-(SKSpriteNode *)CreateLogoNode
{
SKSpriteNode *backNode=[[SKSpriteNode alloc]initWithImageNamed:@"i174"];
backNode.position=CGPointMake(200, Height-100);
/*这里就是改变精灵size的方法*/
SKAction *resizeToWH = [SKAction resizeToWidth:300 height:180 duration:1];
//串行执行Action
[backNode runAction:[SKAction sequence:@[resizeToWH]]];
//淡出
SKAction *fadeOut = [SKAction fadeOutWithDuration:0.2];
//淡入
SKAction *fadeIn = [SKAction fadeInWithDuration:0.2];
//串行淡入淡出
SKAction *ack=[SKAction sequence:@[fadeOut,fadeIn]];
//重复执行
[backNode runAction:[SKAction repeatActionForever:ack]];
return backNode;
}
看到这里是不是就已经头晕晕的,我还是把demo附上吧,然后你去下载
有什么问题在下方评论问就可以!
网友评论