0

スプライトキットを使って空港シミュレーションゲームを作っています。SKPhysics ボディを追加する前のゲーム状態のレイアウトはそのままで、SKPhysicsbody がノードに設定されると、スプライト ノードが警戒します。

これは、SKPhysicsBody なしでシーンに追加しているものです。多くのゲートがあり、ゲートの隣にフライトが立っている空港を想像してみてください。それが、以下のコードで達成しようとしていることです。

@実装 MyScene

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        allPlanes = [[NSMutableArray alloc]init];
        // setting gravity to 0 
        [[self physicsWorld] setGravity:CGVectorMake(0, 0)];
        [[self physicsWorld] setContactDelegate:self];

        [self setBackgroundColor:[UIColor whiteColor]];
       // Below method will provide runway and other static objects that can be seen in an airport
        [self setupAirport];

      /* This is where I am setting up by plane sprites.This method will provide a node which is added to the scene. As you can notice, the sprites are added at specific coordinates until they fill the screen. Imagine three or four gates with flights standing by those gates. That is what I am trying to achieve with below while loop */    

        int xval = 20;
        while (xval < kScreenWidth)
        {
            [self setupFlightAtPoint:xval];
            xval = xval + 60;
        }

    }

    return self;
}

メソッドのコード [self setupFlightAtPoint:xPos]

-(void) setupFlightAtPoint:(CGFloat ) xPos
{
    // Below code will provide a static gate like object.gateNode is of type Gate class which is a subclass of SKNode
    gateNode = [[Gate node] newGate];
    [gateNode setPosition:CGPointMake(xPos, kScreenHeight * 0.37)];
    [self addChild:gateNode];

   // Below code provide plane node and positions it near the gate object.Plane is subclass of SKNode
    Plane *plane = [[Plane alloc]init];
    imageNode = [plane newPlane];
    imageNode.planeIdentifier = xPos;
    [imageNode setPosition:CGPointMake(gateNode.frame.origin.x + 12, gateNode.frame.origin.y+15)];
    [allPlanes addObject:imageNode];
    [self addChild:imageNode];

}

平面オブジェクト法

-(instancetype) newPlane
{
    [self setScale:0.10];
    SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"plane.png"];
    [self addChild:spriteNode];

    return self;
}

これまでのところ、すべて問題ないように見えます。添付の​​ Scene1 という画像を参照して、上記のコードで何が表示されるかを確認してください。ここに画像の説明を入力

物理ボディをプレーンスプライトに設定しようとすると、ここから問題が始まります。私の「newPlane」メソッドでは、以下のコードを追加しています

 -(instancetype) newPlane
 {
    [self setScale:0.10];
    SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"plane.png"];
    [self addChild:spriteNode];

    SKPhysicsBody *planePhysics = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.frame.size];
    [spriteNode setPhysicsBody:planePhysics];
    [[spriteNode physicsBody] setAffectedByGravity:NO];

    return self;
}

Physicsbodies を設定した後、私のシーンは次のようになります

ここに画像の説明を入力

現在、シーンに平面スプライトが 1 つしか表示されていませんが、その理由がわかりません。

4

1 に答える 1

1

スプライトを子として追加する前に、物理ボディを初期化して割り当ててみてください。

-(instancetype) newPlane
{
    [self setScale:0.10];
    SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"plane.png"];
    spriteNode.position = CGPointMake(100, 200);

    SKPhysicsBody *planePhysics = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.frame.size];
    spriteNode.physicsBody = planePhysics;
    spriteNode.physicsBody.affectedByGravity = NO;

    [self addChild:spriteNode];

    return self;
}

また、ドット表記を使用するようにコードを変換しました。この方が入力しやすく読みやすいと思います。

于 2014-02-24T12:50:09.697 に答える