我在Kobold2d-Cocos2d中遇到了关于CCSprite对象分层的问题。
我的所有代码都可以正常工作,除了数组中保存的某个tile(CCSprite)应始终位于顶部。代码有一个对CCSprites(Tile)的引用数组,并且将所有的tile交换在一起。所以元素0和1切换(在数组和屏幕上使用moveTo),然后新元素1和2切换,然后新元素2和3切换等...
这个想法是链条开始时的瓷砖跳跃到线的前端!
我总是希望第一块瓷砖保持在最顶层,但事实并非如此。它只是有时候(当它高于另一个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的方式调用以下代码(我排除了多个参数所需的函数包装中间人):
(如果它更容易理解,而不是使用二维数组来保存我的所有图块,我每行只绘制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];
}
好的。
我知道我正在做的一些事情并不完全有效,如果不相关,我真的不想专注于它们。这对我来说只是一个学习练习 - 老实说,我不明白为什么原始瓷砖不会保持领先。
我已经尝试了所有可能找到答案的资源或示例(示例包含代码,在线教程,我买的可怕书,随机有点相关的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];
}
注意:在MoveTo动画开始之前,我检查了两个Tiles上的zOrder是否正确。
答案 0 :(得分:0)
我没有将精灵添加到我的图层,而是添加到图像节点,因为我使用的是Sprite Atlas。所以我试图访问那些甚至不存在的精灵。 - 我是新用户,所以要等8个小时才能关闭。和平。为什么它没有抛出我永远不会知道的错误。