0
votes

I am building an airport simulation game using sprite kit. My layout for the game states intact before adding SKPhysics body and once SKPhysicsbody is set for nodes, my sprite nodes goes wary.

This is what I am adding to scene without SKPhysicsBody. Imagine a airport with many gates and flights standing next to the gates. That is what I am trying to achieve with below code.

@implementation 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;
}

Now code for methods [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];

}

Plane object method

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

    return self;
}

Till now everything looks fine.Please see attached image called scene1 to see what I see with above code.enter image description here

Now my problem begins here when I am trying to set physics body to my plane sprites. In my "newPlane" method, I am adding below code

 -(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;
}

After setting Physicsbodies, my scene looks like this

enter image description here

only one plane sprite is seen in my scene now and I am not able to figure out why?

1

1 Answers

1
votes

Try initializing and assigning the physics body before adding the sprite as child:

-(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;
}

I also converted the code to use dot notation, I find this easier to type and read.