SpriteKit初探的解释--自学X…

SpriteKit里面,有几个关键的class:

SKView,理解为页面,也就是容纳场景(SKScene)的容器,相当于普通应用的View;

SKScene:场景,一些元素比如人物、武器都是通过场景展示给用户的;

SKView和SKScene的关系,通常用ViewController.m的这几句构造联系:

- (void)viewDidLoad     //这个方法是关键

{

    [super viewDidLoad];

    

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

    skView.showsFPS = YES;

    skView.showsNodeCount = YES;

        //skView是视图

    //------------------以下几句是联系:

    SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];

    scene.scaleMode = SKSceneScaleModeAspectFill;

    //skScene是场景

    

    // Present the scene.

    [skView presentScene:scene];    //添加场景

}


skLabelNode:理解为一个标签类,这样设置属性:

-(SKLabelNode*)newHelloNode     //返回一个SKLabelNode

{

    SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];   //设置字体

    helloNode.name = @"helloNode"       //Key-Value系统

    helloNode.text = @"游戏世界";

    helloNode.fontSize = 44;

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

    return helloNode;

}


skScene和skNode通常这样写:

-(void)createSceneContents

{

    NSLog(@"createSceneContents");

    self.backgroundColor = [SKColor blueColor];

    self.scaleMode = SKSceneScaleModeAspectFit;

    [self addChild:[self newHelloNode]];

}

-(SKLabelNode*)newHelloNode     //返回一个SKLabelNode

{

    SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];   //设置字体

    helloNode.name = @"helloNode"       //Key-Value系统

    helloNode.text = @"游戏世界";

    helloNode.fontSize = 44;

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

    return helloNode;

}


skAction:动画类,

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]];  //动画数组


skNode:对象类,skLabelNode的superClass,

SKNode *helloNode = [self childNodeWithName:@"helloNode"];

和SkAction之间的交互这样写:

[helloNode runAction: moveSequence completion:^{//......}];


skSpriteNode:精灵类,写的时候,继承自SKScene:

SKSpriteNode *spaceship = [self newSpaceship];

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

    [self addChild:spaceship];


- (SKSpriteNode *)newSpaceship  //返回一个SKSpriteNode对象 理解为里面的东东

{

    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]]];

            //动作为hover

    [hull runAction: [SKAction repeatActionForever:hover]];

    

    SKSpriteNode *light1 = [self newLight];

    light1.position = CGPointMake(-28.0, 6.0);  //参考系是ship

    [hull addChild:light1];

    

    SKSpriteNode *light2 = [self newLight];

    light2.position = CGPointMake(28.0, 6.0);

    [hull addChild:light2]; //添加两个方块

    

    hull.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hull.size];       //重力影响 掉落

    hull.physicsBody.dynamic = NO //重力影响 取消

    

    return hull;

}


- (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;  

}


函数指针指向的方法:

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];

}


代码示例源于:http://blog.csdn.net/kobbbb/article/details/9093601

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值