转自kobbbb的博客 [微博]
- SKView *spriteView = (SKView *)self.view;
- spriteView.showsFPS = YES;
- spriteView.showsDrawCount = YES;
- spriteView.showsNodeCount = YES;
- HelloScene *hello = [[HelloScene alloc]initWithSize:CGSizeMake(360, 480)];
- SKView *spriteView = (SKView *)self.view;
- [spriteView presentScene:hello];
- - (self)didMoveToView: (SKView*) view
- {
- if (!self.contentCreated)
- {
- [self createSceneContents];
- self.contentCreated = YES;
- }
- }
- - (void)createSceneContents
- {
- self.backgroundColor = [SKColor blueColor];
- self.scaleMode = SKSceneScaleModeAspectFit;
- [self addChild: [self newHelloNode]];
- }
- - (SKLabelNode*)newHelloNode
- {
- SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
- helloNode.text = @"Hello, World!";
- helloNode.fontSize = 24;
- helloNode.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
- return helloNode;
- }
- - (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 duration:0.5];
- SKAction *zoom = [SKAction scaleTo:2 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];
- }
- }
- @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;
- }
- [helloNode runAction: moveSequence completion:^{
- SKScene* spaceshipScene = [[SpaceshipScene alloc] initWithSize:self.size];
- SKTransition *doors = [SKTransition doorsOpenVerticalWithDuration:0.5];
- [self.view presentScene:spaceshipScene transition:doors];
- }];
- SKSpriteNode *spaceship = [self newSpaceship];
- spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
- [self addChild:spaceship];
- - (SKSpriteNode *)newSpaceship
- {
- SKSpriteNode *hull = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(33,24)];
- 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]];
- return hull; }
- 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];
- - (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;
- }
- SKAction *makeRocks = [SKAction sequence: @[
- [SKAction performSelector:@selector(addRock) onTarget:self],
- [SKAction waitForDuration:0.10 withRange:0.15]
- ]];
- [self runAction: [SKAction repeatActionForever:makeRocks]];
- 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];
- }];
- }