2013-11-29 3 views
0

Совет: Cocos2d. У меня есть CCSprite массива:клон массив CCSprite

movableSprites = [[NSMutableArray alloc] init]; 
     NSArray *images = [NSArray arrayWithObjects:@"bird.png", @"cat.png", @"dog.png", @"turtle.png", nil]; 
     for(int i = 0; i < images.count; ++i) { 
      NSString *image = [images objectAtIndex:i]; 
      CCSprite *sprite = [CCSprite spriteWithFile:image]; 
      float offsetFraction = ((float)(i+1))/(images.count+1); 
      sprite.position = ccp(winSize.width*offsetFraction, winSize.height/2); 
      [self addChild:sprite]; 
      [movableSprites addObject:sprite]; 
     } 

Создать клон массива: MovableSprites2. Второй массив добавлен к сцене. У меня есть метод: selectSpriteForTouch:

- (Void) selectSpriteForTouch: (CGPoint) touchLocation { 
    CCSprite * newSprite = nil; 
    for (CCSprite * sprite in movableSprites) { 
        if (CGRectContainsPoint (sprite.boundingBox, touchLocation)) { 
            newSprite = sprite; 
            break; 
        } 
    } 
    if (newSprite! = selSprite) { 
        [selSprite stopAllActions]; 
        [selSprite runAction: [CCRotateTo actionWithDuration: 0.1 angle: 0 ]] ; 
        CCRotateTo * rotLeft = [CCRotateBy actionWithDuration: 0.1 angle: -4.0]; 
        CCRotateTo * rotCenter = [CCRotateBy actionWithDuration: 0.1 angle: 0.0]; 
        CCRotateTo * rotRight = [CCRotateBy actionWithDuration: 0.1 angle: 4.0]; 
        CCSequence * rotSeq = [CCSequence actions: rotLeft, rotCenter, rotRight, rotCenter, nil]; 
        [newSprite runAction: [CCRepeatForever actionWithAction: rotSeq]]; 
        selSprite = newSprite; 

    } 
} 

при этом, когда вы нажимаете на любой CCSprite, то CCSprite начинает вращаться. Скажите, пожалуйста, как я могу сделать, нажав на один из первого массива CCSprite, появился CCSprite с точно такой же картиной второго массива, он начал вращать CCSprite не из первого массива, а второго, который появился))

ответ

1

Чтобы создать спрайт, который использует ту же текстуру (рисунок), как уже существующий спрайт:

CCSprite* newSprite = [CCSprite spriteWithTexture:existingSprite.texture 
              rect:existingSprite.textureRect]; 
Смежные вопросы