iPhoneマルチスクリーン

3DSみたいに下のスクリーンは主にタッチ、上がメインという感じのものってどうやったらいいか。と思ったので、下の四角をタップすると上の四角の同じ座標にボールがでてくるというシンプルなiPhoneアプリのサンプルコードを描いてみます。2枚のSKViewを使ってます。


動かすとこんな感じです。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

#define MyTapNotification @“tap screen”

@interface MainScene : SKScene

@end

@implementation MainScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor grayColor];

    self.physicsWorld.speed = 0.1;

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivePoint:) name:MyTapNotification object:nil];

    

    [self createWall];

}

– (void)createWall

{

    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.frame];

    SKShapeNode *wall = [SKShapeNode node];

    wall.path = path.CGPath;

    wall.strokeColor = [SKColor whiteColor];

    wall.lineWidth = 5;

    [self addChild:wall];

    

    wall.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:path.CGPath];

    wall.physicsBody.dynamic = NO;

}

– (void)receivePoint:(NSNotification *)notification {

    

    NSValue *val = [notification object];

    CGPoint p = [val CGPointValue];

    

    [self updateLabel:p];

    [self createBall:p];

}

– (void)updateLabel:(CGPoint)p

{

    SKLabelNode *label = (SKLabelNode*)[self childNodeWithName:@”label”];

    if (!label) {

        label = [SKLabelNode node];

        label.name = @”label”;

        label.fontSize = 15;

        label.position = CGPointMake(140, 180);

        [self addChild:label];

    }

    label.text = [NSString stringWithFormat:@”point = (%f, %f)”, p.x, p.y];

}

– (void)createBall:(CGPoint)p

{

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-10, –10, 20, 20)];

    SKShapeNode *ball = [SKShapeNode node];

    ball.position = p;

    ball.path = path.CGPath;

    ball.lineWidth = 0;

    float saturation = (arc4random() % 7) * 0.1 + 0.2;

    ball.fillColor = [SKColor colorWithHue:0.3 saturation:saturation brightness:1.0 alpha:1.0];

    [self addChild:ball];

    

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

}

– (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end

@interface TapScene : SKScene

@end

@implementation TapScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor grayColor];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    NSValue *val = [NSValue valueWithCGPoint:p];

    [[NSNotificationCenter defaultCenter] postNotificationName:MyTapNotification object:val];

    

    SKLabelNode *label = [SKLabelNode node];

    label.text = @”+”;

    label.position = p;

    [self addChild:label];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    self.view.backgroundColor = [UIColor redColor];

    

    SKView *top = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 280, 200)];

    top.center = CGPointMake(160, 140);

    [self.view addSubview:top];

    

    SKScene *topScene = [[MainScene alloc] initWithSize:top.frame.size];

    [top presentScene:topScene];

    

    

    SKView *bottom = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 260, 180)];

    bottom.center = CGPointMake(160, 360);

    [self.view addSubview:bottom];

    

    SKScene *bottomScene = [[TapScene alloc] initWithSize:bottom.frame.size];

    [bottom presentScene:bottomScene];

}

@end