iOS Dev (49) 苹果官方 SpriteKit Game 模版
- 作者:大锐哥
- 博客:http://prevention.iteye.com
基本架构
- AppDelegate - ViewController:基础的 VC。 - MyScene:动画场景,处理动作等等。
在 AppDelegate 中实例化一个 ViewController,在 ViewController 中实例化一个 MyScene。
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
ViewController
- (void)loadView
{
self.view = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
}
- (void)viewDidLoad
{
[super viewDidLoad];
SKView * skView = (SKView *)self.view;
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
}
上面这个很好看懂。loadView 里面初始化 view,这个一定要记住,不能在 init 中做,也不能在 viewDidLoad 中做。
viewDidLoad 中,先实例化一个 MyScene,设置这个 MyScene 带 scaleMode 为 SKSceneScaleModeAspectFill。最后再在 view 上 present 这个 scene。
以上步骤,都是常规做法。
MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
myLabel.text = @"Hello, World!";
myLabel.fontSize = 30;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:myLabel];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite.position = location;
SKAction *action = [SKAction rotateByAngle:M_PI duration:1];
[sprite runAction:[SKAction repeatActionForever:action]];
[self addChild:sprite];
NSLog(@"for loop");
}
NSLog(@"touchesBegan");
}
实现 touchesBegan 方法,这个方法是 MyScene 从 UIResponder 继承来的,其定义为:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
这个继承关系是这样带:
MyScene -> SKScene -> SKEffectNode -> SKNode -> UIResponder
回头来说这个 touchesBegan 吧。
- 先获取到 touch 的点 location。
- 创建一个 sprite,用的是 spriteNodeWithImageNamed 这个 API。
- 设置这个 sprite 带位置。
- 创立一个 SKAction,让 sprite 来 repeat 这个 action。
- 最后呢,把这个 sprite 加到 scene 上吧。
转载请注明来自大锐哥的博客:http://prevention.iteye.com
相关推荐
简单的SpriteKit打飞机 利用SpriteKit写的一个简单的打飞机场景,可以发射子弹、移动飞机以及碰撞检测。 利用SpriteKit写的一个简单的打飞机场景,可以发射子弹、移动飞机以及碰撞检测。
spine-spritekit, 针对 iOS 7 SpriteKit的非官方书脊 2D 运行时 脊椎 spritekitUnofficial ( http://esotericsoftware.com )的非官方 iOS 7运行时运行时官方运行时在这里:http://esotericsoftware.com/spine-runti
iOS游戏开发之使用 Spritekit 框架和 Swift 的 iOS 2D 太空射击游戏源码
iOS 7 Game Development(ios7 英文版 SpriteKit 介绍)1标签: ios7 Game Spritekit .本书英文版,就是有关spritekit的, 当前有关这个的书非常少,只能看看英文版的了。
ios7 最新游戏框架spritekit框架制作的demo
nnIn Chapter 1, you’ll start with a template that gives you an intro screen and a main game screen that will serve as the foundation for everything else that you do with Spite Kit. nnIn Chapter 2, ...
Swift的SpritekitDemo。打飞机,素材来源于网上,没写什么逻辑,注释不多,不过效果蛮不错的,给大家娱乐一下。没有使用任何第三方,以及插件。请在Xcode7,iOS9.0以上版本中编译执行。
IOS SpriteKit学习之微信打飞机
IOS 2D游戏开发框架
SKTetrisGame 使用Apple的SpriteKit为iOS创建的俄罗斯方块游戏
使用SpriteKit实现iOS版本的2048游戏
_ Objective-C_代码_下载 开始使用 SpriteKit 的演示。将著名的 Cocos2DSimpleGame 移植到 ...See my blog for more: SpriteKit快速入门和新时代iOS游戏开发指南 更多详情、使用方法,请下载后阅读README.md文件
Power up your inner game developer and start building incredible games with Sprite Kit. This book will teach you everything you need to know about Apple's 2D game engine. If you have some programming ...
SpriteKit游戏开发教程,2D方向,示例易懂,适合所有选手
一个关于SpriteKit的物理系统的简单Demo,实现一些基本的物理特性
Orbit7, 在SpriteKit中,开源iOS游戏创建Swift Orbit7使用 Swift 在SpriteKit中创建的开源iOS游戏https://www.mav3r1ck.iohttps://itunes.apple.com/us/app/orbit-7/id959313456
iOS游戏框架Sprite Kit基础教程 Swift版上册
SpriteKit游戏开发教程,2D方向,示例易懂,适合所有选手
一个简单的iOS 7 Sprite Kit学习demo
手把手指导初学者如何使用sprite Kit开发一个"割绳子"iOS游戏