iOS - SpriteKit探索

一、粒子效果:SKEmitterNode

  1. 雪花效果 <转换出来的gif有点模糊嗯>


    mm.gif
  2. 烟花效果<转换出来的gif有点模糊嗯>


    qqp.gif
  3. 火焰效果、冒烟效果、下雨效果等等

  4. 自定义效果


    图片.png

创建粒子效果步骤<以雪花举例>

  1. 创建.sks文件 : File - file - SpriteKit Particle file , 选择对应粒子效果


    图片.png
  2. 更改Main.storyboard 文件中view对应的父视图层级结构


    图片.png
  3. 创建Scene类 : File - file - SpriteKit Scene ,

     @property(nonatomic,strong)SKEmitterNode* emitter;
    
     - (void)didMoveToView:(SKView *)view {
    
         NSString*burstPath =[[NSBundle mainBundle] pathForResource:@"snow"ofType:@"sks"];
    
         self.emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
    
         _emitter.position=CGPointMake(self.size.width/2,self.size.height/2-100);
    
         [self addChild:_emitter];
    
     }
    

5.如果想滑动、点击等也实现对应效果的话,实现下列代码

 - (void)touchDownAtPoint:(CGPoint)pos {
    SKEmitterNode *n = [_emitter copy];
    n.position = pos;
//    n.strokeColor = [SKColor greenColor];
    [self addChild:n];
}

//- (void)touchMovedToPoint:(CGPoint)pos {
//    SKEmitterNode *n = [_emitter copy];
//    n.position = pos;
////    n.strokeColor = [SKColor blueColor];
//    [self addChild:n];
//}

- (void)touchUpAtPoint:(CGPoint)pos {
    SKEmitterNode *n = [_emitter copy];
    n.position = pos;
//    n.strokeColor = [SKColor blueColor];
    [self addChild:n];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // Run 'Pulse' action from 'Actions.sks'

    for (UITouch *t in touches) {[self touchDownAtPoint:[t     locationInNode:self]];}
}

//- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//    for (UITouch *t in touches) {[self touchMovedToPoint:[t locationInNode:self]];}
//}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
}
  1. 加载效果

     GameScene *scene = (GameScene *)[SKScene nodeWithFileNamed:@"GameScene"];
     //    scene.size = CGSizeMake(KScreenWidth, KScreenHeight);
         // Set the scale mode to scale to fit the window
     scene.scaleMode = SKSceneScaleModeAspectFill;
     SKView *skView = (SKView *)self.view;
     skView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
       // Present the scene
     [skView presentScene:scene];
     skView.showsFPS = YES;
     skView.showsNodeCount = YES;
    

二、拖曳效果:SKShapeNode

qwr.gif
  1. 更改Main.storyboard 文件中view对应的父视图层级结构

  2. 创建Scene类 : File - file - SpriteKit Scene

      @property(nonatomic,strong) SKShapeNode * spinnyNode;
    
     - (void)didMoveToView:(SKView *)view{
    
         CGFloat w = (self.size.width + self.size.height) * 0.05;
    
         // Create shape node to use during mouse interaction
         _spinnyNode = [SKShapeNode         shapeNodeWithRectOfSize:CGSizeMake(w, w) cornerRadius:w * 0.3];
         _spinnyNode.lineWidth = 2.5;
    
         [_spinnyNode runAction:[SKAction repeatActionForever:[SKAction rotateByAngle:M_PI duration:1]]];
         [_spinnyNode runAction:[SKAction sequence:@[
             [SKAction waitForDuration:0.5],
             [SKAction fadeOutWithDuration:0.5],
             [SKAction removeFromParent],
         ]]];
     }
    
  3. 拖动等代码实现

     - (void)touchDownAtPoint:(CGPoint)pos {
         SKShapeNode *n = [_spinnyNode copy];
         n.position = pos;
         n.strokeColor = [SKColor greenColor];
         [self addChild:n];
     }
    
     - (void)touchMovedToPoint:(CGPoint)pos {
         SKShapeNode *n = [_spinnyNode copy];
         n.position = pos;
         n.strokeColor = [SKColor blueColor];
         [self addChild:n];
     }
    
     - (void)touchUpAtPoint:(CGPoint)pos {
         SKShapeNode *n = [_spinnyNode copy];
         n.position = pos;
         n.strokeColor = [SKColor redColor];
         [self addChild:n];
     }
    
     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
         // Run 'Pulse' action from 'Actions.sks'
    
         for (UITouch *t in touches) {[self touchDownAtPoint:[t locationInNode:self]];}
     }
    
     - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
         for (UITouch *t in touches) {[self touchMovedToPoint:[t locationInNode:self]];}
     }
    
     - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
         for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
     }
    
     - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
         for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
     }
    

你可能感兴趣的:(iOS - SpriteKit探索)