Я создаю игру-симулятор аэропорта, используя набор спрайтов. Мой макет для игры остается нетронутым до добавления тела 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;
}
После установки Physicsbody моя сцена выглядит так
сейчас в моей сцене виден только один спрайт самолета, и я не могу понять, почему?