0

我正在使用 sprite kit 构建一个机场模拟游戏。在添加 SKPhysics 主体之前,我的游戏布局完好无损,一旦为节点设置了 SKPhysicsbody,我的精灵节点就会变得谨慎。

这就是我在没有 SKPhysicsBody 的情况下添加到场景中的内容。想象一下一个有许多登机口和登机口旁边的航班的机场。这就是我试图用下面的代码实现的。

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

现在为方法 [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 后,我的场景如下所示

在此处输入图像描述

现在我的场景中只看到一个平面精灵,我不知道为什么?

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 回答