SpriteKit(基础用法)

文件1:GameViewController

#import "GameViewController.h"

#import "FirstScene.h"

@implementation SKScene (Unarchive)

@end

@implementation GameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    /* Sprite Kit applies additional optimizations to improve rendering performance */
    skView.ignoresSiblingOrder = YES;
    
    // Create and configure the scene.
    FirstScene *scene = [FirstScene sceneWithSize:self.view.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    
    // Present the scene.
    [skView presentScene:scene];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

@end


文件2:FirstScene.m

#import "FirstScene.h"
#import "ButtonSpriteNode.h"
#import "SecondScene.h"
@implementation FirstScene

//场景显示时机
- (void)didMoveToView:(SKView *)view
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
    self.backgroundColor = [SKColor redColor];
    
    ButtonSpriteNode *buttonSpriteNode = [[ButtonSpriteNode alloc] initWithImageNamed:@"share_logo"];
    [self addChild:buttonSpriteNode];
    buttonSpriteNode.position = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
    buttonSpriteNode.didClicked = ^(ButtonSpriteNode *buttonSpritrNode){
        SKTransition *transition = [SKTransition doorsOpenVerticalWithDuration:3];
        SecondScene *secondScene = [SecondScene sceneWithSize:view.bounds.size];
        [self.view presentScene:secondScene transition:transition];
    };
}
@end


文件3:SecondScene.m

#import "SecondScene.h"
@interface SecondScene()<SKPhysicsContactDelegate>
@end
@implementation SecondScene
{
    SKSpriteNode *spriteNode;
    NSTimeInterval oldTime;
}

- (void)didMoveToView:(SKView *)view
{
    self.backgroundColor = [SKColor purpleColor];
    
    spriteNode = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
    spriteNode.position = CGPointMake(self.frame.size.width/2, self.frame.size.height-25);
    [self addChild:spriteNode];
    
    SKPhysicsBody *body = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.size];
    body.mass = 10;
    body.categoryBitMask = 0x1<<1;//标示物理体的碰撞类别,0xFFFF
    body.collisionBitMask = 0x1<<2;//标示能够与那些类型的物体碰撞
    body.contactTestBitMask = 0x1<<1;//与某一类物体碰撞后是否发出通知,0x0
    spriteNode.physicsBody = body;
    
    SKSpriteNode *spriteNode1 = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(200, 20)];
    spriteNode1.position = CGPointMake(80, 200);
    [self addChild:spriteNode1];
    
    SKPhysicsBody *body1 = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode1.size];
    body1.dynamic = NO;
    body1.categoryBitMask = 0x1<<2;
    body1.collisionBitMask = 0x1<<1;
    body1.contactTestBitMask = 0x1<<1;
    spriteNode1.physicsBody = body1;
    
    SKSpriteNode *spriteNode2 = [SKSpriteNode spriteNodeWithColor:[SKColor cyanColor] size:CGSizeMake(200, 20)];
    spriteNode2.position = CGPointMake(300, 100);
    [self addChild:spriteNode2];
    
    SKPhysicsBody *body2 = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode2.size];
    body2.dynamic = NO;
    body2.categoryBitMask = 0x1<<2;
    body2.collisionBitMask = 0x1<<1;
    body2.contactTestBitMask = 0x1<<1;
    spriteNode2.physicsBody = body2;
    
    //设置SKScene物理世界
    self.physicsWorld.gravity = CGVectorMake(0, -10);
    self.physicsWorld.contactDelegate = self;
    
    //世界边界
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    
//    SKAction *action1 = [SKAction moveTo:CGPointMake(self.frame.size.width/2, 25) duration:3];
    [spriteNode runAction:action1];
//    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, nil, CGRectMake(self.frame.size.width/2, self.frame.size.height/2, 200, 200));
    SKAction *action2 = [SKAction followPath:path duration:10];
    [spriteNode runAction:action2];
//    
//    SKAction *action3 = [SKAction rotateByAngle:M_PI duration:3];
    [spriteNode runAction:action3];
//    
//    SKAction *repAction = [SKAction repeatActionForever:action3];//永远执行
    SKAction *reqAction = [SKAction sequence:@[action1,repAction]];//按顺序执行动画
    [spriteNode runAction:reqAction];
//    SKAction *groupAction = [SKAction group:@[action1,repAction]];
//    [spriteNode runAction:groupAction];
    
//    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(didRefresh:)];
//    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
//
//- (void)didRefresh:(id)sender
//{
//    NSLog(@"%s",__PRETTY_FUNCTION__);
//}
//
//- (void)update:(NSTimeInterval)currentTime
//{
//    static int i = 0;
//    NSLog(@"%i",i++);
//    NSLog(@"%f",currentTime - oldTime);
//    oldTime = currentTime;
//}

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    NSLog(@"Begin: %@-%@", contact.bodyA.node.name, contact.bodyB.node.name);
}

- (void)didEndContact:(SKPhysicsContact *)contact
{
    NSLog(@"End: %@-%@", contact.bodyA.node.name, contact.bodyB.node.name);
}

//- (void)didBeginContact:
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值