我正在嘗試在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的最後一個對象。我如何獲得?
任何想法如何獲得子節點的最後一個對象? – icam0
我認爲floorArray聽起來像是更好的選擇,只是增加和減少,但是每個SKNode都有一個名爲children的屬性,它返回一個包含所有子節點的數組。 –