2014-01-24 47 views
1

我想創建一個場景,將有在上面平鋪背景,其中將存在可用的級別,用戶可以選擇他/她就可以出鍋背景,以查看所有級別。瓷磚背景:並不是所有的精靈出現

所以,在iOS的遊戲建議通過教程我創建了一個SKNode *_backgroundLayer,將容納所有的瓷磚,以及其他任何將頂上去。這將允許我只移動SKNode,其所有孩子都會隨之移動。大。現在,我的瓷磚是450x450 pixels,所以我計算我需要在我的_backgroundLayer中使用9個瓷磚,並使用下面的代碼添加它們。

我不明白的是:它的負載和位置有出現在我的屏幕前兩個磚,它似乎並沒有加載休息!在模擬器的「x節點」中,它表示它有2個節點,而不是9個。是否會創建節點,然後將它們移除,因爲它們最初不在我的iphone屏幕內部?我不明白。有關如何解決這個問題並解決它的任何想法?

更新:在我的iPad視網膜我得到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 

謝謝!

回答

0

代碼很好。調試標籤中的節點數量指的是繪製的節點數量,而不是場景圖形中的總數量。檢查_backgroundLayer.children.count節點的實際數量。

+0

但爲什麼後來當我平移背景層我沒有看到精靈的休息嗎?背景圖層(sknode)不應該像1500x1500像素,因爲它包含所有這9個精靈?我看到一個黑色的背景,而不是另一個背景精靈。我是否在做平搖的事情上做錯了事?但是我平移了背景圖層。我也會將代碼上傳到該圖層。有任何想法嗎?順便說一句,謝謝你的回覆! –

+0

_backgroundLayer.children.count返回什麼?如果它返回9,那麼可能出於某種原因,節點不會被繪製(在正確的位置)。檢查屬性:位置,zPosition,隱藏,anchorPoint,xScale,yScale,框架和大小,用於任何不尋常的事情。 – LearnCocos2D

+0

UPDATE2:NSLog(@「%d」,_backgroundLayer.children.count);確實會返回9。我很確定我正在用UIPanGestureRecognizer進行一些錯誤的操作。我移動了錯誤的圖層嗎? –