2014-07-02 74 views
1

我正在嘗試在sprite工具包中創建移動的地板。我用一個for循環來填充屏幕上的「瓷磚」,我將它們移動到屏幕上。如果他們的xposition小於0,他們將被刪除。現在我想在最後一個瓷磚後面添加一塊瓷磚。但我不知道如何獲取最後一個tile的xposition(這將是從右到左移動時具有最高位置值的tile)。這裏是我的代碼:Xcode sprite工具包如何選擇特定節點

-(void)createTile:(float)xpos { 
SKSpriteNode *tile = [SKSpriteNode spriteNodeWithImageNamed:@"tile"]; 
tile.name = @"tile"; 
float tilesize = (CGRectGetMaxY(self.frame)/20); 
tile.yScale = tilesize/24; 
tile.xScale = tilesize/24; 
tile.position = CGPointMake(xpos, CGRectGetMidY(self.frame)); 
[self addChild:tile]; 
} 

-(void)update:(CFTimeInterval)currentTime { 
//Going through all the tiles an moving them all 

[self enumerateChildNodesWithName:@"tile" usingBlock:^(SKNode *node, BOOL *stop) { 
    if (node.position.x < 0.){ 
     [node removeFromParent]; 
     //[self createtile:(where the argument must be the position of the last floortile + the size of floortile] 
    } 
    node.position = CGPointMake(node.position.x -3, node.position.y); 

}]; 
} 

編輯:忘了我有地板陣列在那裏,我不想地板陣列的最後一個對象,但childNodeTree的最後一個對象。我如何獲得?

回答

0

不是您floorArray中的最後一個對象總是會成爲x值最高的對象?假設你通過在屏幕上從左到右重複創建地磚?然後,當你在屏幕上添加更多內容時,你會將它們添加到數組中,然後該圖塊將在數組中最右邊和最後一個。

SKSpriteNode *lastTile = [floorArray lastObject]; 

編輯點評:

NSArray *allChildNodes = myScene.children; 
+0

任何想法如何獲得子節點的最後一個對象? – icam0

+0

我認爲floorArray聽起來像是更好的選擇,只是增加和減少,但是每個SKNode都有一個名爲children的屬性,它返回一個包含所有子節點的數組。 –

0

,因爲你知道移除的tile的x位置,你應該知道你每行有多少瓷磚有,你可以簡單地通過偏移去除位置通過

numberOfTilesPerRow*tileWidth. 

計算位置更容易,假設你瓷磚用x = 0開始的時候充滿整個屏幕,和你有一個額外的瓷磚來填補空白,而移動:

_______  _______ 
|OOOOO|O -> O|OOOOOO| 
|OOOOO|O  O|OOOOOO| 
------   ------- 

新的位置將是

parentWidth+tileWidth+removedNode.position.x 
+0

嘗試這種解決方案,但它並不適用於所有的不同的速度我嘗試了工作。儘管如此,感謝您的意見。 – icam0