Sprite Kit编程指南

更新时间:2023-06-11 12:21:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

ios原生游戏开发

oe:本 P译自⑸pple官方的 pr/te K/t Programm/ng Gu/de》,b用C.ome

浏 g的自动P译功能作G译,h后在一t语句不n或i易造成s解的地方作局

Cu正:方便英 不好的开发者查_:如有错d之处,欢迎K家指 u正:

p时欢迎K家S注r的技mA客 .ttp: /t_mag/c/an:K家的

持lr最K的动c:

S

API

, , API

Sprite Kit rendering textured

Sprite Kit

Sprite Kit

Sprite Kit

Sprite Kit

Xcode Sprite Kit ”

atlases ) Sprite Kit

Core Animation

ios原生游戏开发

Sprite Kit iOS OS X “ ” 2D

Sprite Kit

CGPath

Sprite Kit

animate

# OpenGL Sprite Kit

ios原生游戏开发

SKView

) scenes SKScene

Sprite Kit SKScene

SKView

“ Sprite Kit” “ ” “ Sprite Kit ”

n

SKScene SKNode

SKNode Sprite Kit

SKEffectNode Core Image

layer ”

responder descending UIResponder

NSResponder

“ ” “ ” “ ”

ios原生游戏开发

bundle Sprite Kit

Sprite Kit #

Sprite Kit

Sprite Kit

# Sprite Kit

“ ”

p

actions SKAction

) ”

“ ” “ # ”

ios原生游戏开发

SKPhysicsBody

forces

hook

SKPhysicsWorld

SKPhysicsJoint

“ ”

e

“ Sprite Kit” Sprite Kit Sprite

Kit ( Sprite Kit Sprite

Kit

“Sprite Kit ” Sprite Kit

Sprite Kit

iOS iOS OS X Mac

Xcode

ios原生游戏开发

Objective-C blocks

Sprite Kit SpriteKit

Xcode Sprite Kit

SKSpriteNode Sprite Tour

Sprite Kit SpriteKit

Sprite Kit Explained Adventure

ios原生游戏开发

Sprite Kit Sprite Kit

Sprite Kit )

Sprite Kit

iOS iOS Sprite Kit OS X

( Xcode 5.0 iOS Xcode

SpriteWalkthrough ClassPrefix Sprite “ iPad

Sprite Kit

ios原生游戏开发

Sprite Kit Sprite Kit SKView

SKView SKScene

Sprite Kit )

Sprite Kit

!

1. storyboard SpriteViewController

view SKView

2. import <SpriteKit/SpriteKit.h>

3. viewDidLoad - void viewDidLoad

{

[super viewDidLoad];

SKView * spriteView = SKView * self.view;

spriteView.showsDrawCount = YES;

spriteView.showsNodeCount = YES;

spriteView.showsFPS = YES;

}

ios原生游戏开发

4.

spriteView.showsFPS

Hello !

1. HelloScene SKScene

2. #import “HelloScene.h”

3. - void viewWillAppear: BOOL animated

{

HelloScene *hello = [[HelloScene alloc] initWithSize:CGSizeMake(768,1024)];

SKView *spriteView =(SKView *)self.view;

[spriteView presentScene:hello];

}

f

Sprite Kit (chuck)

“Hello World”

ios原生游戏开发

didMoveToView:

Hello !

1. @interface HelloScene()

@property BOOL contentCreated; @end

2. didMoveToView: - self didMoveToView:(SKView *)view

{

if self.contentCreated

{

[self createSceneContents];

self.contentCreated = YES;

}

}

didMoveToView

contentCreated

ios原生游戏开发

3. createSceneContents - (void)createSceneContents

{ self.backgroundColor = [SKColor blueColor];

self.scaleMode = SKSceneScaleModeAspectFit;

[self AddChild [self newHelloNode];

}

SKColor color

SKColor iOS UIColor OS X

NSColor

: scale : :

(letterboxing)

4. newHelloNode - SKLabelNode * newHelloNode

{

SKLabelNode * helloNode = [SKLabelNode labelNodeWithFontNamed:@“Chalkduster”];

@helloNode.text =“Hello, World ”;

helloNode.fontSize = 42;

helloNode.position = CGPointMake(CGRectGetMidX(self.frame) CGRectGetMidY(self.frame)) return helloNode;

}

ios原生游戏开发

OpenGL ES Quartz 2D

Sprite Kit

Sprite Kit

Sprite Kit

“Hello, World ”

Sprite Kit

p p

action

Sprite Kit action

p !

1. newHelloNode = @“helloNode”;

monster

2. touchesBegan withEvent 、

helloNode

ios原生游戏开发

iOS UIResponder OS X NSResponder

Sprite Kit - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

SKNode *helloNode = [self childNodeWithName:@“helloNode”];

If(helloNode != nil)

{

= 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 fadeWithDuration:0.25];

SKAction *remove = [SKAction removeFromParent];

SKAction * moveSequence = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remo

[helloNode runAction moveSequence];

}

}

) ;

1

0

ios原生游戏开发

Sprite Kit

“Hello, World ”

Hello

!

1. SpaceshipScene SKScene

2. HelloScene @interface SpaceshipScene()

@property BOOL contentCreated;

@end

@implementation SpaceshipScene

- (void)didMoveToView:(SKView *)view

{

if(!self.contentCreated)

{

[self createSceneContents];

self.contentCreated = YES;

}

}

ios原生游戏开发

- (void)createSceneContents

{ self.backgroundColor = [SKColor blackColor]; self.scaleMode = SKSceneScaleModeAspectFit; }

3. HelloScene.m SpaceshipScene.h

#import “SpaceshipScene.h”

4. touchesBegan:withEvent runAction

runAction:completion: [helloNode runAction:moveSequence completion:^ { SKScene * spaceshipScene = [[SpaceshipScene alloc] initWithSize:self.size];

SKTransition *doors= [SKTransition doorsOpenVerticalWithDuration:0.5];

[self.view presentScene:spaceshipScene transition:doors];

}];

SKSpriteNode

ios原生游戏开发

Sprite Kit

placeholder

Sprite Kit

!

1. SpaceshipScene.m createSceneContents SKSpriteNode *spaceship = [self newSpaceship];

spaceship.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame)-150)

[self addChild:spaceship];

2. newSpaceship - (SKSpriteNode *)newSpaceship

{

SKSpriteNode *hull= [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size CGSi

SKAction *hover= [SKAction sequence:@[

[SKAction waitForDuration:1.0]

[SKAction moveByX:100 y:50.0 duration:1.0]

ios原生游戏开发

[SKAction waitForDuration:1.0]

[SKAction moveByX:-100.0 y:-50 duration:1.0]];

[hull runAction:[SKAction repeatActionForever:hover];

return hull;}

” ” ”

client

3. newSpaceship 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];

4. newLight

- (SKSpriteNode *)newLight

ios原生游戏开发

{

SKSpriteNode *light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:C

SKAction *blink= [SKAction sequence:@ [

[SKAction fadeOutWithDuration:0.25]

[SKAction fadeInWithDuration:0.25]];

SKAction * blinkForever = [SKAction repeatActionForever:blink];

[light runAction:blinkForever];

return light;

}

Sprite Kit

f !

1. newSpaceship

ios原生游戏开发

hull.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hull.size];

2. newSpaceship hull.physicsBody.dynamic = NO;

3. createSceneContents SKAction * makeRocks = [SKAction sequence:@ [

[SKAction performSelector:@selector(addRock) onTarget:self]

[SKAction waitForDuration:0.10 withRange:0.15]

]];

[self runAction:[SKAction repeatActionForever:makeRocks];

4. addRock static inline CGFloat skRandf() {

return rand()/(CGFloat)RAND_MAX;

}

ios原生游戏开发

static inline CGFloat skRand(CGFloat low, CGFloat high) {

return skRandf()*(high - low) + low;

}

- (void)addRock {

SKSpriteNode *rock = [[SKSpriteNode alloc] initWithColor [SKColor brownColor] size:CG rock.position = CGPointMake(skRand(0, self.size.width),self.size.height-50);

= @“rock”;

rock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rock.size];

esPreciseCollisionDetection = YES;

[self addChild:rock];

}

。 。

, Sprite Kit

5. didSimulatePhysics

- (void)didSimulatePhysics

ios原生游戏开发

{

[self enumerateChildNodesWithName:@“rock” usingBlock:^(SKNode *node, BOOL *stop){ if (node.position.y <0)

[node removeFromParent];

}]; }

Sprite Kit

OS X OS X

delegate 、

、 。

!!

本文来源:https://www.bwwdw.com/article/mbo1.html

Top