LoginSignup
3
2

More than 5 years have passed since last update.

SpriteKitでiOS7/8で動作が異なっていたところ

Last updated at Posted at 2015-05-04

SpriteKitでゲームを作っていてiOS7と8で異なっていたところがあったので、書こうと思います。
そんなにSpriteKit使い倒してるわけでもないので、認識違ってるところだったり、もっとよい方法などぜひアドバイス頂きたいです!m(_ _)m

アプリをバックグラウンド/フォアグラウンド遷移時の挙動

iOS8

・バックグラウンド遷移時、画面が一時停止する(SKNode.paused = YESしたときのような挙動)。
・フォアグラウンド遷移時、画面が一時停止が解除される(SKNode.paused = NOしたときのような挙動)。
 →コードで明示的にSKNode.paused = YESで一時停止ていても、フォアグラウンド遷移時に一時停止が解除される。

iOS7

・バックグラウンド/フォアグラウンド遷移時に勝手に画面が一時停止/一時停止の解除がされない。

バイクのタイヤの作成処理

バイクを走らせるゲームを作っているため、バイクの加速に合わせてタイヤを回転させる必要があり、そのときにタイヤの中心点を車体に固定するコードを書く必要がありました。
その処理を行うためのコードがiOS8,7で異なっていました。

iOS8

・SKPhysicsBody.pinnedプロパティ(iOS8SDKで追加)にYESをセット。

iOS7

・SKPhysicsJointPinクラスを使って、self.physicsWorldにaddJoint。


//selfのクラスは、SKSceneのサブクラスを想定。

@property(nonatomic,strong) SKSpriteNode *myWorld;//親ノード
@property(nonatomic,strong) SKSpriteNode *bikeFrame;//車体
@property(nonatomic,strong) NSMutableArray *tires;//タイヤ(後輪、前輪)

static const uint32_t Collision_bitmask_tire = 0x1 << 0;


/**
 * バイクノード作成
 * かなり汚いコードで恐縮ですが、★付きのところがタイヤの中心点を車体に固定するコードです。。
 */
- (void)createBikeNode
{
    self.bikeFrame = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(40.f, 38.f)];
    self.bikeFrame.position = CGPointMake(50, Y_GROUND_START + (self.bikeFrame.size.height/2));
    self.bikeFrame.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.bikeFrame.size];
    self.bikeFrame.physicsBody.dynamic = YES;//重力影響(default:YES)
    self.bikeFrame.physicsBody.collisionBitMask = Collision_bitmask_bikeframe;//衝突bitMask。ぶつかったときに物理的に影響受けるか。はねかえったりするか。
    self.bikeFrame.physicsBody.contactTestBitMask = 0;//接触bitMask。ぶつかったときにdelegate実行されるか。
    self.bikeFrame.physicsBody.restitution = 0;//できるだけ弾まないようにする
    self.bikeFrame.name = NAME_BIKE;

    self.tires = [NSMutableArray new];
    CGFloat tireRadius = 7.;
    CGFloat tireMargin = 5.5;
    //タイヤ
    CGPoint center[] = {
        //後輪
        CGPointMake(0 - tireRadius - tireMargin + 2.0,
                    0 - tireRadius - 3),
        //前輪
        CGPointMake(0 + tireRadius + tireMargin + 0.5,
                    0 - tireRadius - 3)
    };

    for (int i=0; i<2; i++) {
        SKSpriteNode* tire;
        tire = [SKSpriteNode spriteNodeWithImageNamed:@"tire"];
        tire.position = CGPointMake(center[i].x, center[i].y);
        tire.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:tire.size.width/2.];
        tire.physicsBody.collisionBitMask = Collision_bitmask_tire;//地面と接触可能にする
        tire.physicsBody.usesPreciseCollisionDetection = YES;
        if(i == 0) {
            tire.physicsBody.categoryBitMask = Category_bike_back_wheel;
        } else {
            tire.physicsBody.categoryBitMask = Category_bike_front_wheel;
        }

        tire.physicsBody.contactTestBitMask = Category_ground;//コンタクト対象
        tire.physicsBody.restitution = 0;//できるだけ弾まないようにする
        tire.physicsBody.dynamic = NO;

        if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            tire.position = CGPointMake(center[i].x, center[i].y);
            [self.bikeFrame addChild:tire];
            //★★★★★★★★★★★★★★★
            tire.physicsBody.pinned = YES;//iOS8のときはここで親ノードにpin止め
            //★★★★★★★★★★★★★★★
        } else {
            tire.position = CGPointMake(self.bikeFrame.position.x + center[i].x,
                                        self.bikeFrame.position.y + center[i].y);
            [self.myWorld addChild:tire];
        }

        [self.tires addObject:tire];
    }

    //ここで車体をmyWorldに追加して、タイヤよりも前面表示にする。
    [self.myWorld addChild:self.bikeFrame];

    //★★★★★★★★★★★★★★★
    //iOS7の場合はここでタイヤをpin止め
    if([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
        for (SKSpriteNode* tire in self.tires) {
            SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:self.bikeFrame.physicsBody bodyB:tire.physicsBody anchor:tire.position ];
            [self.physicsWorld addJoint:pin];
        }
    }
    //★★★★★★★★★★★★★★★


    //車体画像を追加。
    //iOS8ではここでやることにより、車体画像をタイヤよりも前面にしている
    SKSpriteNode* frame = [SKSpriteNode spriteNodeWithImageNamed:@"bikeFrame"];
    frame.position = CGPointMake(0.f, 0.f);//中央
    [self.bikeFrame addChild:frame];
}

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2