克隆数组CCSprite

时间:2013-11-29 15:38:55

标签: objective-c cocos2d-iphone

建议: Cocos2D中。 我有一个数组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 个答案:

答案 0 :(得分:1)

创建一个使用与现有精灵相同的纹理(图片)的精灵:

CCSprite* newSprite = [CCSprite spriteWithTexture:existingSprite.texture
                                             rect:existingSprite.textureRect];