我试图在sprite套件中创建一个移动的地板。我使用for循环将"tile"填满屏幕,并在屏幕上移动它们。如果它们的位置小于0,它们就会被移除。现在我想在最后一个贴图后面添加一个贴图。但我不知道如何获得最后一个贴图的位置(这将是具有最高位置值的贴图,因为它从右向左移动)。下面是我的代码:
-(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的最后一个对象。我怎么得到它?
是不是你的floorArray中的最后一个对象总是具有最高的x值的对象?假设你通过在屏幕上从左到右迭代来创建地砖?当你向屏幕中添加更多时,你会将它们添加到数组中,然后那个贴片将是最右边的也是数组中最后一个。
SKSpriteNode *lastTile = [floorArray lastObject];
编辑注释:
NSArray *allChildNodes = myScene.children;
既然你知道被删除贴图的x位置,你应该知道每行有多少个贴图,你可以简单地通过
偏移被删除的位置来计算位置numberOfTilesPerRow*tileWidth.
更简单,假设你的贴图在x=0开始时填满了整个屏幕,并且在移动时你有一个额外的贴图来填补空白:
_______ _______
|OOOOO|O -> O|OOOOOO|
|OOOOO|O O|OOOOOO|
------ -------
新的位置将是
parentWidth+tileWidth+removedNode.position.x