0

我的游戏中有以下设置:

  • 一个名为 _backgroundLayer 的 SKNode
  • 我已经添加到 _backgroundLayer 9 次以制作更大背景的重复纹理
  • 一个名为 levelButton 的 SKSprite,它被添加到 _backgroundLayer ([_backgroundLayer addChild:levelButton];)。

我使用 levelButton.anchorPoint = CGPointMake(0.5, 0.5); 使 levelButton 在中间有一个锚点。现在,当我制作 levelButton.position = CGPointMake(0, 0); 和 _backgroundLayer.position = CGPointMake(0, 0); levelButton 的中间位置正确位于 (0, 0) 中,中间位于屏幕的左下角。所以没关系。但是,如果我将 levelButton 移动到 levelButton.position = CGPointMake(100, 0); 和 _backgroundLayer.position = CGPointMake(-100, 0);,如下所示,levelButton 仍应位于 (0,0) 的中间,即屏幕的左下角。然而,事实并非如此,levelButton 更靠右,它在右边大约 50 个像素。但不应该,因为我将 _backgroundLayer 100 向左 (-100) 和 _levelButton 100 向右 (100) 移动。它应该留在原地。

这些是基本的东西,我不明白为什么它们不能正常工作。我可能做错了什么,但即使我通读了 iOS Games by Tutorials 和一堆教程和技巧,我也找不到它。

请帮忙。

现在,我的代码如下:

@implementation LevelSelectScene
{
   SKNode *_backgroundLayer;
}

-(id)initWithSize:(CGSize)size {

    /* Setup your scene here */
    _backgroundLayer = [SKNode node];
    _backgroundLayer.name = @"backgroundLayer";

    SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:@"levelSelect"];

    int textureID = 0;

    for (int i = 0; i<3; i++) {
        for (int j = 0; j<3; j++) {

            SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture];

            background.anchorPoint = CGPointZero;
            background.position = CGPointMake((background.size.width)*i,
                                              (background.size.height)*j);
            background.zPosition = 0;
            background.name = [NSString stringWithFormat:@"background%d", textureID];

            textureID++;

            [_backgroundLayer addChild:background];
        }
    }

    [self addChild:_backgroundLayer];

    SKSpriteNode * levelButton = [SKSpriteNode spriteNodeWithImageNamed:@"lock"];
    levelButton.anchorPoint = CGPointMake(0.5, 0.5);
    levelButton.position = CGPointMake(100, 0);                 //IMPORTANT
    levelButton.zPosition = 150;
    levelButton.name = @"test";

    [_backgroundLayer addChild:levelButton];

    _backgroundLayer.position = CGPointMake(-100, 0);     //IMPORTANT
}
return self;
}
4

1 回答 1

0

找到了!我正在更改 _backgroundLayer SKnode 的比例,因此坐标不同。

于 2014-02-10T15:35:09.490 回答