2012-07-15 3 views
0

CCSprite 객체의 레이어링과 관련하여 Kobold2d-Cocos2d에 문제가 있습니다.Cocos2d Kobold2d reorderChildren이 CCSprites를 올바르게 지정하지 않았습니다.

배열에서 보유 된 특정 타일 (CCSprite)이 항상 맨 위에 있어야한다는 것만 제외하면 내 모든 코드가 올바르게 작동합니다. 이 코드에는 CCSprites (Tile)에 대한 참조 배열이 있으며 서로 바꿔 놓은 모든 타일을 따라갑니다. 따라서 요소 0과 1이 (배열과 화면에서 moveTo를 사용하여) 전환 된 다음 새 요소 1과 2가 전환 된 다음 새 요소 2와 3이 전환됩니다.

아이디어는 선의 앞 부분에 체인 도약의 시작!

나는 첫 번째 타일이 항상 맨 위에 머물길 원하지만, 그렇지 않습니다.

//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; 

    } 
` 

에 의해 다음과 같은 코드를 호출합니다 (그것은 화면에 함께 장소를 전환 상대방의 타일 위에있는 경우 나중에 추가 된 의미) 그것은 단지

가 여기에 문제의 CCScene 코드입니다 때때로 않습니다

(여기서 쉽게 이해보다는 그냥에만 그려 내 모든 타일을 유지하기 위해 2 차원 배열을 사용 할 수 있습니다 : 실제로 두 개의 타일을 교체하는 방법은 (내가 여러 인수에 필요한 기능 래퍼 중개인 제외) 줄 당 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]; 

} 

좋아.

내가하는 일 중 일부는 정확하게 효율적이지 않으며 관련이 없다면 그들에게 집중하기를 정말로 원하지 않는다는 것을 알고 있습니다. 이것은 저를위한 다만 학습 운동이다 - 나는 정당하게 왜 기발한 도와가 정상에 체재하지 않을지 이해하지 않는다.

나는 아마도 답을 찾을 수있는 모든 자원 또는 예를 시도했습니다 (샘플 코드, 온라인 자습서, 내가 구입 한 끔찍한 책, 임의 다소 관련 유래 기사를 포함)와 나는 아무것도 점점없는거야

:에서

\를 그것은이 중요한 경우 내가 현장에 내 스프라이트를 추가 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]; 
} 

참고 : I 체크하고하면 MoveTo 애니메이션이 시작되기 전에 모두 타일에 zOrder를이 줄에 올바른 것입니다.

+0

스프라이트 아틀라스를 사용했기 때문에 내 스프라이트를 내 레이어가 아니라 이미지 노드에 추가했습니다. 그래서 거기에 없었던 스프라이트에 액세스하려고했습니다. - 나는 새로운 사용자이기 때문에 내가 닫을 때까지 8 시간을 기다려야한다. 평화. 왜 오류가 발생하지 않았는지 나는 결코 알 수 없습니다. –

+1

스프라이트의 zOrder 속성을 단순히 업데이트하지 않는 이유는 무엇입니까? 높은 zOrder는 다른 노드보다 낮은 zOrder로 노드를 그립니다. 특정 타일이 항상 맨 위임을 확인하여 매우 높은 zOrder를 제공합니다. – LearnCocos2D

+0

위의 의견에서 나는 당신에게 생각한다고 대답했습니다. –

답변

0

스프라이트 아틀라스를 사용했기 때문에 내 스프라이트를 내 레이어가 아닌 이미지 노드에 추가했습니다. 그래서 거기에 없었던 스프라이트에 액세스하려고했습니다. - 나는 새로운 사용자이기 때문에 내가 닫을 때까지 8 시간을 기다려야한다. 평화. 왜 내가 결코 알지 못할 오류를 던지지 않았다.