CCSpriteオブジェクトのレイヤー化に関してKobold2d-Cocos2dで問題が発生しています。
配列に保持されている特定のタイル(CCSprite)が常に一番上にあると想定されていることを除いて、私のコードはすべて正常に機能します。このコードには、CCSprites(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;
}
`
これは、実際に2つのタイルを交換する方法で次のコードを呼び出します(複数の引数に必要な関数ラッパー仲介者を除外しました)。
(2D配列を使用してすべてのタイルを保持するのではなく、理解しやすい場合は、1行あたり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];
}
大丈夫。
私が行っているいくつかのことは正確に効率的ではないことを認識しており、それが関連性がない場合は、それらに重点を置きたくありません。これは私にとって単なる学習演習です。元のタイルがトップに留まらない理由を正直に理解していません。
答えが見つかる可能性のあるすべてのリソースまたは例(サンプルに含まれるコード、オンラインチュートリアル、購入したひどい本、ランダムに関連するスタックオーバーフローの記事)を試しましたが、何も得られません:\
重要な場合は、これが元々スプライトをシーンに追加した方法です。
//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アニメーションが開始する前に、チェックしたところ、両方のタイルのzOrderが正しい行になっています。