我在Kobold2d-Cocos2d有关于CCSprite对象分层的问题。Cocos2d Kobold2d reorderChildren没有正确地订购CCSprites
我的所有代码都能正常工作,除了某个数组中保存的某个tile(CCSprite)总是处于顶层。该代码有一个对CCSprites(Tile)的引用数组,并沿着所有的tile互相交换。因此,元素0和1开关(在阵列和屏幕上使用moveTo),然后是新元素1和2开关,然后是新元素2和3开关等...
这个想法是在链条跳跃的开始!
我总是希望第一块瓷砖留在上面,但事实并非如此。它只是有时确实(当它的其他瓷砖它在屏幕上切换地上面,这意味着它是后来添加的)
这里是有问题的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;
}
`
由调用下面的代码(我排除了函数包装中间人我需要多个参数):
(如果它更容易理解,而不是使用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不会保持在最前面。
我试过的每个资源或例子,我可能可以找到答案(样本包括代码,在线教程,可怕的书我买了,随机有点关系计算器的文章),我得到什么:\
在它的事项这种情况下我是怎么origionally加入我的精灵现场:
//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];
}
注:我查了一下,对双方瓷砖的ZORDER是正确的就行了通过MoveTo动画开始之前。
我将我的精灵添加到了我的图层中,但添加到了图像节点,因为我使用的是Sprite Atlas。所以我试图访问甚至没有的精灵。 - 我是一名新用户,因此需要等待8个小时才能关闭。和平。 为什么它没有抛出一个我永远不会知道的错误。 – 2012-07-15 02:09:26
你为什么不简单地更新精灵的zOrder属性?较高的zOrder将使用较低的zOrder将节点绘制到其他节点上方。为了确保特定的瓷砖始终处于最高位置,请提供非常高的ZOrder。 – LearnCocos2D 2012-07-15 08:46:05
我回答说在你上面的评论中,我想。 – 2012-07-17 03:59:52