我在 Kobold2d-Cocos2d 中遇到了关于 CCSprite 对象分层的问题。
我所有的代码都可以正常工作,除了一个保存在数组中的某个图块(CCSprite)应该始终位于顶部。该代码有一个对 CCSprites (Tile) 的引用数组,并沿着所有瓷砖相互交换它们。所以元素 0 和 1 切换(在数组和屏幕上使用 moveTo),然后新元素 1 和 2 切换,然后新元素 2 和 3 切换等等......
这个想法是链开始处的 Tile 跨越到线的前面!
我总是希望第一块瓷砖保持在顶部,但事实并非如此。它只是有时会这样做(当它在另一个 Tile 上方时,它会在屏幕上切换位置,这意味着它是稍后添加的)
这是有问题的CCScene代码:
//reference first CCSprite in array -works
Tile *firstTile = [tileArray objectAtIndex:(int)[[[selTracker oldSelectionArray] objectAtIndex:0]element]];
//How much time should be spend leapfrogging each two tiles broken up over 1 second.
float intervalTime = .75/(float)[[selTracker oldSelectionArray] count];
//Will use this to count each tile set we leapfrog
float currentPass = 0;
//TODO: destroy this wrapper class that holds number, use NSNumber. Fine for now.
SimpCoord *lastCord;
//Make moving sprite higher than all others. DOESN'T WORK?
[self reorderChild:firstTile z:100];
//Loop through all the tiles that were selected
for (SimpCoord *coord in [selTracker oldSelectionArray])
{
if (lastCord)
{
//Queue each tile to leapfrog with our moving tile
if ([self respondsToSelector:@selector(switchTilesFuncWrapper:)])
{
NSArray *argArray = [NSArray arrayWithObjects:
[NSNumber numberWithInt:[lastCord element]],
[NSNumber numberWithInt:[coord element]],
[NSNumber numberWithFloat:(intervalTime)], nil];
[self performSelector:@selector(switchTilesFuncWrapper:) withObject:argArray afterDelay:((currentPass) * intervalTime)];
}
currentPass++;
}
lastCord = coord;
}
`
这通过实际交换两个 Tiles 的方式调用以下代码(我排除了多个参数所需的函数包装中间人):
(如果它更容易理解,而不是使用 2d 数组来保存我所有的图块,我每行只绘制 10 个,因此 tileCoordsByElement 方法 - 但这不应该是重要的)
//
// Switch two tiles on the screen and in the holder array
// onlySwitchArray pass in true to only swap tiles in organization array
// but provide no animation.
- (void) switchTiles:(NSNumber*)elePosVal secPos:(NSNumber*)elePos2Val timeToSwitch:(NSNumber*)time
{
float switchTime = [time floatValue];
int elePos = [elePosVal intValue];
int elePos2 = [elePos2Val intValue];
Tile *tmpTile = [tileArray objectAtIndex:elePos];
Tile *tmpTile2 = [tileArray objectAtIndex:elePos2];
//Move actual tiles on screen
//Move Tile is elePos (1) to elePos2
int curCol = 0;
int curRow = 0;
[self tileCoordsByElement:elePos2 x:&curRow y:&curCol];
CCSequence *tmpTile1_Seq = [CCSequence actions:
[CCMoveTo actionWithDuration:switchTime position:ccp((curRow-1)*tmpTile.width + tmpTile.width/2,
(curCol-1)*tmpTile.height + tmpTile.height/2)], nil];
[tmpTile runAction:[CCRepeat actionWithAction:tmpTile1_Seq times:1]];
//Move Tile that is elePos2 to elePos(1)
[self tileCoordsByElement:elePos x:&curRow y:&curCol];
CCSequence *tmpTile1_Seq2 = [CCSequence actions:
[CCMoveTo actionWithDuration:switchTime position:ccp((curRow-1)*tmpTile.width + tmpTile.width/2,
(curCol-1)*tmpTile.height + tmpTile.height/2)], nil];
[tmpTile2 runAction:[CCRepeat actionWithAction:tmpTile1_Seq2 times:1]];
//Swap tiles in array second
[tileArray replaceObjectAtIndex:elePos withObject:tmpTile2];
[tileArray replaceObjectAtIndex:elePos2 withObject:tmpTile];
}
好吧。
我知道我正在做的一些事情并不完全有效,如果它不相关,我真的不想过多地关注它们。这对我来说只是一个学习练习——老实说,我真的不明白为什么原来的 Tile 不会保持领先。
我已经尝试了所有可能找到答案的资源或示例(示例包含代码、在线教程、我买的糟糕的书、随机的一些相关的 stackoverflow 文章),但我什么也没得到:\
万一这很重要,这就是我最初将精灵添加到场景中的方式:
//Set up A CCSpriteBatchNode to load in pList/pngs of images
[[CCSpriteFrameCache sharedSpriteFrameCache]
addSpriteFramesWithFile:@"zAtl_BasicImages_64.plist"];
nodeBasicImages = [CCSpriteBatchNode batchNodeWithFile:@"zAtl_BasicImages_64.png" capacity:100];
//Initialize array used to track tiles in scene
tileArray = [[NSMutableArray alloc] init];
for (int i = 0; i<100; i++)
{
Tile *tmpTile = [[Tile alloc] init];
[tileArray addObject:tmpTile];
[self reorderChild:tmpTile z:-1];
}
注意:我检查了两个 Tiles 上的 zOrder 在 MoveTo 动画开始之前的行上是正确的。