平铺背景:并非所有精灵都出现

时间:2014-01-24 09:21:37

标签: objective-c sprite-kit tile skspritenode sknode

我正在尝试创建一个平铺背景的场景,其中将存在用户可以选择的可用级别,并且他/她将能够平移背景以查看所有级别。< / p>

所以,正如iOS教程中的Tutorials所建议的那样,我创建了一个SKNode *_backgroundLayer,它可以容纳所有的磁贴以及其他任何顶部的磁贴。这样我就可以只移动SKNode,所有孩子都会随之移动。大。现在,我的图块为450x450 pixels,因此我计算出我的_backgroundLayer中需要9个图块,并使用下面的代码添加它们。

我不明白的是:它加载并定位必须出现在我的屏幕中的前两个图块,它似乎不会加载其余图块!在模拟器中的“x节点”中,它表示它有2个节点,而不是9.它是否创建节点然后将它们删除,因为它们最初不在我的iphone屏幕内?我不明白。关于如何解决这个问题的任何想法并解决它?

更新:在我的iPad Retina中,我获得了6个节点而不是9个节点,那么为什么它只是加载并保持可以在屏幕上显示的节点而不是我在下面的代码中请求的所有节点?

UPDATE2:NSLog(@“%d”,_ backgroundLayer.children.count);确实应该返回9。我很确定我在使用UIPanGestureRecognizer时遇到了一些问题。我移动了错误的图层吗?

#import "LevelSelectScene.h"
#import "TurtleWorldSubScene.h"

@interface LevelSelectScene ()
@property (nonatomic, strong) SKSpriteNode *selectedNode;
@end

@implementation LevelSelectScene
{
    SKNode *_backgroundLayer;
}

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        _backgroundLayer = [SKNode node];
        _backgroundLayer.name = @"backgroundLayer";
        [self addChild:_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];

                NSLog(@"x:%f, y:%f", background.position.x, background.position.y);

                textureID++;

                [_backgroundLayer addChild:background];
            }
        }

        NSLog(@"%d", _backgroundLayer.children.count);

        //[TurtleWorldSubScene displayTurtleWorld:self];

    }
    return self;
}

- (void)didMoveToView:(SKView *)view {
    UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [[self view] addGestureRecognizer:gestureRecognizer];
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

    if (recognizer.state == UIGestureRecognizerStateBegan) {

    } else if (recognizer.state == UIGestureRecognizerStateChanged) {

    } else if (recognizer.state == UIGestureRecognizerStateEnded) {

    }

}
@end

谢谢!

1 个答案:

答案 0 :(得分:0)

代码很好。调试标签中的节点数量是指绘制的节点数,而不是场景图中的总数。检查_backgroundLayer.children.count是否有实际的节点数。