效果图:
头文件:
#import <SpriteKit/SpriteKit.h> @interface MainScene : SKScene // 添加背景 - (void)addBgImgWithName:(NSString *)imgName; // 添加人物 - (void)addBoyWithImgName:(NSString *)imgName; @end
M文件
#import "MainScene.h" static NSString * const kAnimalNodeName = @"constant_boy"; @interface MainScene() { BOOL _isContentCreated; } @property (nonatomic, strong) SKSpriteNode *currentNode; @end @implementation MainScene - (void)willMoveFromView:(SKView *)view { DLog(@"即将从View:%@ 移除",view); } // 每当视图呈现场景时,didMoveToView:方法都会被调用 - (void)didMoveToView: (SKView *) view { DLog(@"didMoveToView:%@",view); if (!_isContentCreated) { [self initSceneContents]; _isContentCreated = YES; } // 给self 场景 所在的view添加平移手势 [self addPanReco]; } - (void)addPanReco { UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGuestRecoed:)]; [[self view] addGestureRecognizer:gestureRecognizer]; } - (void)initSceneContents { self.backgroundColor = [SKColor blueColor]; self.scaleMode = SKSceneScaleModeAspectFit; [self addChild: [self newHelloNode]]; } - (SKLabelNode *)newHelloNode { SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; helloNode.text = @"Hello, Beyond!"; // 类似于tag helloNode.name = @"HelloBeyond"; helloNode.fontSize = 32; helloNode.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame)); return helloNode; } // 根据图片名,添加背景图片精灵 - (void)abstract_addBgSpriteNodeNamed:(NSString *)imgName { SKSpriteNode *bgSprite = [SKSpriteNode spriteNodeWithImageNamed:imgName]; bgSprite.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame)); bgSprite.size = self.size; [self addChild:bgSprite]; } // 根据图片名,添加人物精灵 - (void)abstract_addBoySpriteNamed:(NSString *)imgName { SKSpriteNode *boy = [SKSpriteNode spriteNodeWithImageNamed:imgName]; boy.name = kAnimalNodeName; boy.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame)); [self addChild:boy]; } // 另一种方法添加背景 - (void)addBgImgWithName:(NSString *)imgName { [self abstract_addBgSpriteNodeNamed:imgName]; } - (void)addBoyWithImgName:(NSString *)imgName { [self abstract_addBoySpriteNamed:imgName]; } #pragma mark - 添加平移手势 - (void)panGuestRecoed:(UIPanGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"手势开始"); CGPoint touchLocation = [recognizer locationInView:recognizer.view]; // 打印 DLog(@"sg__%@",NSStringFromCGPoint(touchLocation)); touchLocation = [self convertPointFromView:touchLocation]; // 打印 DLog(@"sg__%@",NSStringFromCGPoint(touchLocation)); // 找出点击的目标,并且_currentNode记住 [self selectNodeForTouch:touchLocation]; } else if (recognizer.state == UIGestureRecognizerStateChanged) { NSLog(@"手势进行..."); CGPoint translation = [recognizer translationInView:recognizer.view]; // 打印 DLog(@"手势进行...sg__%@",NSStringFromCGPoint(translation)); translation = CGPointMake(translation.x, -translation.y); [self panForTranslation:translation]; [recognizer setTranslation:CGPointZero inView:recognizer.view]; } else if (recognizer.state == UIGestureRecognizerStateEnded) { NSLog(@"手势结束"); } } - (void)panForTranslation:(CGPoint)translation { CGPoint position = [_currentNode position]; if([[_currentNode name] isEqualToString:kAnimalNodeName]) { // 更新boy的坐标 [_currentNode setPosition:CGPointMake(position.x + translation.x, position.y + translation.y)]; } } // 根据位置, 选择出当前的Node - (void)selectNodeForTouch:(CGPoint)touchLocation { //1 SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation]; //2 点了新的 Node if(![_currentNode isEqual:touchedNode]) { [_currentNode removeAllActions]; [_currentNode runAction:[SKAction rotateToAngle:0.0f duration:0.1]]; _currentNode = touchedNode; //3 if([[touchedNode name] isEqualToString:kAnimalNodeName]) { SKAction *sequence = [SKAction sequence:@[[SKAction rotateByAngle:degToRad(-4.0f) duration:0.1], [SKAction rotateByAngle:0.0 duration:0.1], [SKAction rotateByAngle:degToRad(4.0f) duration:0.1]]]; [_currentNode runAction:[SKAction repeatActionForever:sequence]]; } } } // 选中后,摇摆 float degToRad(float degree) { return degree / 180.0f * M_PI; } #pragma mark - 触屏事件 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 当前Node停止抖动 [_currentNode removeAllActions]; } #pragma mark - 触摸事件 - (void)touchesBegan2:(NSSet *) touches withEvent:(UIEvent *)event { SKNode *helloNode = [self childNodeWithName:@"HelloBeyond"]; if (helloNode != nil) { // 为了防止Node 重复响应 touch事件,代码会清除节点的名称。 // To prevent the node from responding to repeated presses, the code clears the node’s name. helloNode.name = 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 fadeOutWithDuration: 0.25]; SKAction *remove = [SKAction removeFromParent]; SKAction *moveSequence = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]]; // [helloNode runAction: moveSequence]; [helloNode runAction: moveSequence completion:^{ return; // ShipScene *shipScene = [[ShipScene alloc] initWithSize:self.size]; // SKTransition *doorTransition = [SKTransition doorsOpenVerticalWithDuration:0.5]; // [self.view presentScene:shipScene transition:doorTransition]; }]; } } @end