iOS7 Sprite Kit 学习
iOS7 Sprite Kit 学习
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.
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]; //@3
SKAction *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 //@2
SKSpriteNode *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]; //@3
hull.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
iOS7 Sprite Kit 学习的更多相关文章
- Sprite Kit编程指南(1)-深入Sprite Kit
深入Sprite Kit 学习Sprite Kit最好的方法是在实践中观察它.此示例创建一对场景和各自的动画内容.通过这个例子,你将学习使用Sprite Kit内容的一些基础技术,包括: · ...
- ios学习之路四(新建Sprite Kit 项目的时候出现apple LLVM 5.0 error)
在新建sprite kit 项目的时候出现"apple LLVM 5.0 error" 解决方法 在网上搜索,stackoverflow 上是这么说的点击打开链接.按照他的我也没解 ...
- Sprite Kit 入门教程
Sprite Kit 入门教程 Ray Wenderlich on September 30, 2013 Tweet 这篇文章还可以在这里找到 英语, 日语 If you're new here, ...
- ios游戏开发 Sprite Kit教程:初学者 1
注:本文译自Sprite Kit Tutorial for Beginners 目录 Sprite Kit的优点和缺点 Sprite Kit vs Cocos2D-iPhone vs Cocos2D- ...
- Sprite Kit教程:初学者
作者:Ray Wenderlich 原文出处:点击打开链接 http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners 转自 ...
- Sprite Kit编程指南中文版下载
下载地址:http://download.csdn.net/detail/xin814/6032573 关于Sprite Kit 重要提示: 这是API或开发技术的一个初版文档.虽然本文档的技术准确 ...
- Sprite Kit教程:制作一个通用程序 2
注1:本文译自Sprite Kit Tutorial: Making a Universal App: Part 2 目录 动画的定义:可行性 属性列表 添加游戏逻辑 添加音效 何去何从 上一篇文章中 ...
- swift语言注册非免费苹果账号iOS游戏框架Sprite Kit基础教程
swift语言注册非免费苹果账号iOS游戏框架Sprite Kit基础教程 1.2.3 注册非免费苹果账号swift语言注册非免费苹果账号iOS游戏框架Sprite Kit基础教程 免费的苹果账号在 ...
- Swift版iOS游戏框架Sprite Kit基础教程下册
Swift版iOS游戏框架Sprite Kit基础教程下册 试读下载地址:http://pan.baidu.com/s/1qWBdV0C 介绍:本教程是国内唯一的Swift版的Spritekit教程. ...
随机推荐
- 对[foreach]的浅究到发现[yield]
原文:对[foreach]的浅究到发现[yield] 闲来无事,翻了翻以前的代码,做点总结,菜鸟从这里起航,呵呵. 一.List的foreach遍历 先上代码段[1]: class Program { ...
- 利用WebBrowser实现Web打印的分析
原文:利用WebBrowser实现Web打印的分析 WebBrowser是IE内置的浏览器控件,无需用户下载.本文档所讨论的是有关IE6.0版本的WebBrowser控件技术内容.其他版本的IE应该也 ...
- Xcode下执行HelloWorld
准备工作 到Cocos2d-x官方站点下载最新版本号v3.0beta2 创建HelloWorld项目 将刚才下载的压缩包解压到你指定的目录里. 进入到文件夹**cocos2d-x-3.0beta2/t ...
- Cocos2d-x3.0之路--02(引擎文件夹分析和一些细节)
关于怎么搭建好开发环境的我就不写了,网上非常多. 那么 我们来看看 引擎文件的文件夹 所谓知己知彼 百战不殆嘛 先说一下setup.py 这个文件是有关配置的python文件,比方我们在进行andro ...
- nodejs 平台的 webscoket 的实现
新手入门,没办法,只能选择不断不断的google吧. 找了很多的例子都跑不了,不知道什么原因. 后,自己在git搜索吧,选择了一个下面的例子: nodejs-web-socket 经过我的改造,改成我 ...
- Cocos2d-x 3.0final 终结者系列教程12-Vector&map&value
北京时间昨天下午,温40度.中午12:16我来到了篮球场点.思维1分钟决定开站 转球: 我和另一个3队友半开始, 我手中的球的那一刻我突然火爆球不稳,突然问,淡淡的味道橡胶和烧烤的味道混合. 个腾空跳 ...
- php连接sql server 2008数据库
原文:php连接sql server 2008数据库 关于php连接sql server 2008的问题,2000的版本可以直接通过php中的配置文件修改,2005以上的版本就不行了,需要使用微软公司 ...
- 编译安装gimp插件之Mathmap(流水记录)
本文为在Fedora 20下编译安装Mathmap1.3.5的编译过程,如果你仅仅需要快速的安装Mathmap,那么请拉至文末的"快速安装" 其实,过程还是很有趣的,充满Error ...
- [探索]点点轻博客搬家到WordPress(一)
摘要:点点博客备份XML通过DiandianToWordpress-beta.sh(文末给出)搬家到Wordpress博客 本人曾使用过点点轻博客,也深知像点点博客,Lofter博客导出的XML文件不 ...
- [译]Java 设计模式之中介者
(文章翻译自Java Design Pattern: Mediator) 中介者设计模式被用于一组的同事进行协作.这些同事不彼此进行直接的交流联系,但是是通过中介者. 在下面的例子中,A同事想去说话, ...