iOS 7有一个新功能 Sprite Kit 这个有点类似cocos2d 感觉用法都差不多。下面简单来介绍下Sprite Kit

About Sprite Kit

Sprite Kit provides a graphics rendering and animation infrastructure that you can use to animate arbitrary textured images, or sprites. Sprite Kit uses a traditional rendering loop that allows processing on the contents of each frame before it is rendered. Your game determines the contents of the scene and how those contents change in each frame. Sprite Kit does the work to render frames of animation efficiently using the graphics hardware. Sprite Kit is optimized to allow essentially arbitrary changes to each frame of animation.

Sprite Kit also provides other functionality that is useful for games, including basic sound playback support and physics simulation. In addition, Xcode provides built-in support for Sprite Kit, allowing complex special effects and texture atlases to be easily created and then used in your app. This combination of framework and tools makes Sprite Kit a good choice for games and other apps that require similar kinds of animation. For other kinds of user-interface animation, use Core Animation instead.

可以参考官方文档。:https://developer.apple.com/library/prerelease/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40013043-CH1-SW1

Jumping into Sprite Kit

The best way to learn Sprite Kit is to see it in action. This example creates a pair of scenes and animates content in each. By working through this example, you will learn some of the fundamental techniques for working with Sprite Kit content, including:

  • Using scenes in a Sprite Kit–based game.

  • Organizing node trees to draw content.

  • Using actions to animate scene content.

  • Adding interactivity to a scene.

  • Transitioning between scenes.

  • Simulating physics inside a scene.

Getting Started

Xcode5.0以上。。

首先新建一个项目 SpriteWalkthrough 。

项目建好后在 ViewController.m 里添加如下代码

打开storyboard 然后找到右边 Sustom Slass 把Class 改成SKView!!!这个一定要改,不然会报错 -[UIView setShowsFPS:]: unrecognized selector sent to instance 0x9742e30

#import <SpriteKit/SpriteKit.h>

- (void)viewDidLoad
{[super viewDidLoad];SKView *spriteView = (SKView*)self.view;spriteView.showsFPS = YES;spriteView.showsDrawCount = YES;spriteView.showsNodeCount = YES;// Do any additional setup after loading the view, typically from a nib.
}

新建一个class  命名为HelloScene.h文件不需要做修改

#import <SpriteKit/SpriteKit.h>@interface HelloScene : SKScene@end

然后再回到ViewController.m .修改如下

#import "HelloScene.h"

-(void)viewWillAppear:(BOOL)animated
{HelloScene* hello = [[HelloScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height)];SKView *spriteView = (SKView*)self.view;[spriteView presentScene:hello];}

OK 。。run it ..

2.然后在HelloScene.m里添加如下

@interface HelloScene()
@property BOOL contentCreated;
@end

-(void)didMoveToView:(SKView *)view
{if(!self.contentCreated){[self  createSceneContents];self.contentCreated = YES;}
}-(void)createSceneContents
{NSLog(@"createSceneContents");self.backgroundColor = [SKColor blackColor];self.scaleMode = SKSceneScaleModeAspectFit;[self addChild:[self newHelloNode]];
}

-(SKLabelNode*)newHelloNode
{SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];helloNode.name = @"helloNode";//@ 这个和下面的一样helloNode.text = @"hello game ";helloNode.fontSize = 24;helloNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));return helloNode;
}

这个时候可以运行一次。

接下来继续,在HelloScene.m 里添加

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{SKNode *helloNode = [self childNodeWithName:@"helloNode"]; //@与上面的相同if(helloNode !=nil){helloNode.name = nil;SKAction *moveUp = [SKAction moveByX:0 y:100.0 duration:0.5]; //向上移动SKAction *zoom = [SKAction scaleTo:2.0 duration:0.25];    //扩大两倍SKAction *pause = [SKAction waitForDuration:0.5];    //暂停SKAction *fadeAway = [SKAction fadeOutWithDuration:0.25];  //消失SKAction *remove = [SKAction removeFromParent];SKAction *moveSequence = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]];[helloNode runAction:moveSequence];}
}

再编译运行

新建一个class 命名为 SpaceshipScene 然后在SpaceshipScene.m里添加如下

@import "SpaceshipScene.h"@interface SpaceshipScene ()
@property BOOL contentCreated;
@end@implementation SpaceshipScene
- (void)didMoveToView:(SKView *)view
{if (!self.contentCreated){[self createSceneContents];self.contentCreated = YES;}
}- (void)createSceneContents
{self.backgroundColor = [SKColor blackColor];self.scaleMode = SKSceneScaleModeAspectFit;
}
@end

.h

#import <SpriteKit/SpriteKit.h>@interface SpaceshipScene : SKScene@end

然后回到

SpaceshipScene.m 修改如下代码。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{SKNode *helloNode = [self childNodeWithName:@"helloNode"]; //@与上面的相同if(helloNode !=nil){helloNode.name = nil;SKAction *moveUp = [SKAction moveByX:0 y:100.0 duration:0.5]; //向上移动SKAction *zoom = [SKAction scaleTo:2.0 duration:0.25];    //扩大两倍SKAction *pause = [SKAction waitForDuration:0.5];    //暂停SKAction *fadeAway = [SKAction fadeOutWithDuration:0.25];  //消失SKAction *remove = [SKAction removeFromParent];SKAction *moveSequence = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]];//[helloNode runAction:moveSequence];[helloNode runAction: moveSequence completion:^{SKScene *spaceshipScene  = [[SpaceshipScene alloc] initWithSize:self.size];SKTransition *doors = [SKTransition doorsOpenVerticalWithDuration:0.5];[self.view presentScene:spaceshipScene transition:doors];}];}
}

运行。。

最后把SpaceshipScene.m 文件修改如下

//
//  SpaceshipScene.m
//  SpriteWalkthrough
//
//  Created by qingyun on 8/13/13.
//  Copyright (c) 2013 qingyun. All rights reserved.
//

#import "SpaceshipScene.h"@interface SpaceshipScene ()
@property BOOL contentCreated;
@end@implementation SpaceshipScene- (void)didMoveToView:(SKView *)view
{if (!self.contentCreated){[self createSceneContents];self.contentCreated = YES;}
}- (void)createSceneContents
{self.backgroundColor = [SKColor blackColor];self.scaleMode = SKSceneScaleModeAspectFit;SKSpriteNode *spaceship = [self newSpaceship];spaceship.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame)-150);[self addChild:spaceship];//@3SKAction *makeRocks = [SKAction sequence: @[[SKAction performSelector:@selector(addRock) onTarget:self],[SKAction waitForDuration:0.10 withRange:0.15]]];[self runAction: [SKAction repeatActionForever:makeRocks]];
}- (SKSpriteNode *)newSpaceship
{SKSpriteNode *hull = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(64,32)];SKAction *hover = [SKAction sequence:@[[SKAction waitForDuration:1.0],[SKAction moveByX:100 y:50.0 duration:1.0],[SKAction waitForDuration:1.0],[SKAction moveByX:-100.0 y:-50 duration:1.0]]];[hull runAction: [SKAction repeatActionForever:hover]]; //重复移动hover 里的action //@2SKSpriteNode *light1 = [self newLight];light1.position = CGPointMake(-28.0, 6.0);[hull addChild:light1];SKSpriteNode *light2 = [self newLight];light2.position = CGPointMake(28.0, 6.0);[hull addChild:light2];//@3hull.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hull.size];hull.physicsBody.dynamic = NO;return hull;
}//@2
- (SKSpriteNode *)newLight
{SKSpriteNode *light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:CGSizeMake(8,8)];SKAction *blink = [SKAction sequence:@[[SKAction fadeOutWithDuration:0.25],[SKAction fadeInWithDuration:0.25]]];SKAction *blinkForever = [SKAction repeatActionForever:blink];  //重复闪闪发光。
    [light runAction: blinkForever];return light;
}//@3
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(8,8)];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 *node, BOOL *stop) {if (node.position.y < 0)[node removeFromParent];}];
}@end

 

好了。一个简单的sprite 小游戏做成了。

demo 下载地址: https://github.com/qingjoin/SpriteKit

转载于:https://www.cnblogs.com/qingjoin/p/3255352.html

iOS7 Sprite Kit 学习相关推荐

  1. 苹果官方《Sprite Kit Programming Guide》翻译

    http://www.cocoachina.com/newbie/basic/2013/0822/6845.html 本文翻译自Apple官方的<Sprite Kit Programming G ...

  2. Sprite Kit 入门教程

    Sprite Kit 入门教程  Ray Wenderlich on September 30, 2013 Tweet 这篇文章还可以在这里找到 英语, 日语 If you're new here, ...

  3. 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比(一家之言)

    初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比 发布于:2013-07-18 11:00阅读数:1984 ...

  4. 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比

    前言 iOS7 beta发布后,大部分开发者和用户的注意力都集中在了iOS 7的全新UI交互设计界面上.一直负责硬件工业设计的Jony Ive首次全面负责苹果的软件和硬件设计工作,自然要把他自己的设计 ...

  5. Sprite Kit教程

    在iOS 7中内置了一个新的Sprite Kit框架,该框架主要用来开发2D游戏.目前已经支持的内容包括:精灵.很酷的特效(例如视频.滤镜和遮罩),并且还集成了物理库等许多东西.iOS 7中附带了一个 ...

  6. 游戏中的三角学——Sprite Kit 和 Swift 教程(1)

    原文链接 : Trigonometry for Games – Sprite Kit and Swift Tutorial: Part 1/2 原文作者 : Nick Lockwood 译文出自 : ...

  7. Sprite Kit 动作系统

    添加动作到节点 绘制精灵很有用,但是一张静态图像只是一幅画,而不是一个游戏.为了添加游戏剧本(game play),你需要能够让精灵在屏幕周围移动并执行其他逻辑.Sprite Kit让场景动起来所使用 ...

  8. sprite Kit Actions(三)

    感觉一辈子都没打过这么多字了... Scale action 缩放动作 You now have an animated zombie and some crazy cat ladies, but t ...

  9. 如何用 Sprite Kit 和 Swift 制作一个逃逸游戏-第一部分

    原文:How To Make a Breakout Game with SpriteKit and Swift: Part 1 作者:Michael Briscoe 译者:kmyhy 更新说明:本教程 ...

最新文章

  1. python使用fpdf创建页眉、页脚并嵌入图片
  2. c语言的非法字符常量,判断C语言数值常量是否合法?为什么不合法?
  3. 《编译原理》第二章知识点
  4. 现代软件工程讲义 3 代码规范与代码复审
  5. 浅谈格斗游戏的精髓——方块的战争
  6. 语音识别开放平台调研以及主要技术
  7. matlab中牛顿迭代程序,牛顿迭代法的MATLAB程序
  8. 数据库系统概论第五版_第二章:关系数据库
  9. 目前总结最新最系统的Java程序员未来职业规划路线,请收藏
  10. 抵制微信公众号,从我做起
  11. 在Python中用WordCloud生成聊天记录热点词汇词云图
  12. 分数测试录取率软件,圆梦志愿app准吗?测试大学录取概率高吗?
  13. codevs1013 求先序排列 string黑科技[三星]
  14. Python进阶-正则表达式
  15. EFS与NTFS联合应用解析
  16. 新版完整标准 BS ISO-IEC 24745-2022 信息安全、网络安全和隐私保护-生物特征信息保护
  17. 重定向与请求转发的区别及什么时候使用
  18. 请编写一个程序,使用字典存储学生信息,学生信息包括学号和姓名,请根据学生学号从小到大输出学生的信息。
  19. 斜率优化dp 的简单入门
  20. AEB 五种安全距离模型

热门文章

  1. python2.7+PyQt5 制作桌面便签小程序
  2. 腾讯音乐娱乐集团与华纳音乐续签长期战略协议 并将联合成立全新音乐厂牌
  3. [Unity]读取本地图片ArgumentException: A null reference or invalid value was found错误
  4. Python基础知识之函数篇
  5. 安卓机器人做图软件_美图秀秀绘画机器人app下载-美图绘画机器人Andy最新版下载v7.0.0.0-西西软件下载...
  6. Js 之移动端图片上传插件mbUploadify
  7. Windows驱动开发系列小白入门经典 - vs2019双机调试wdk驱动程序
  8. day02-HTML的基本标签
  9. 多维度入手打造稳定高效的自动测试设备,迎接集成电路融合时代的机遇与挑战
  10. 单招计算机面试考什么,单招考些什么?面试需要注意哪些?