Note that there are some explanatory texts on larger screens.

plurals
  1. POCocos2d Kobold2d reorderChildren doesn't order CCSprites properly
    primarykey
    data
    text
    <p>I am having an issue in Kobold2d-Cocos2d regarding the layering of CCSprite objects.</p> <p>All of my code works fine, except that a certain tile (a CCSprite), held in an array, is supposed to always be on top. The code has an array of references to CCSprites (Tile) and goes along all the tiles swapping them with each other. So element 0 and 1 switch (in the array and on screen using a moveTo), then the new element 1 and 2 switch, then the new element 2 and 3 switch etc...</p> <p>The idea is the Tile at the start of the chain leapfrogs to the front of the line!</p> <p>I always want that first tile to stay on top, but it isn't. It only does sometimes (when it's above the other Tile it's switching places with on the screen, implying it was added later)</p> <p>Here's the CCScene code in question:</p> <pre><code>//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; } ` </code></pre> <p>That calls the following code by the way that actually swaps the two Tiles (I excluded the function wrapper middleman I needed for multiple arguments):</p> <p>(In case it makes it easier to understand, rather than use a 2d array to hold all my tiles I just only draw 10 per line, hence the tileCoordsByElement method - but that shouldn't be important)</p> <pre><code>// // 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:&amp;curRow y:&amp;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:&amp;curRow y:&amp;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]; } </code></pre> <p>Alright.</p> <p>I'm aware some things I'm doing aren't exactly efficient and if it's not relevant I don't really want to focus to heavily on them. This is just a learning exercise for me - I just honestly don't understand why the origional Tile won't stay on top. </p> <p>I've tried every resource or example I possibly can find for answers (sample included code, tutorials online, terrible book I bought, random somewhat related stackoverflow articles) and I'm getting nothing :\</p> <p>In case it matters this is how I origionally added my sprites to the scene:</p> <pre><code>//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&lt;100; i++) { Tile *tmpTile = [[Tile alloc] init]; [tileArray addObject:tmpTile]; [self reorderChild:tmpTile z:-1]; } </code></pre> <p>Note: I checked and the zOrder on both Tiles is correct on the line before the MoveTo animation begins.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload