首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >克隆阵列CCSprite

克隆阵列CCSprite
EN

Stack Overflow用户
提问于 2013-11-29 15:38:55
回答 1查看 103关注 0票数 0

请咨询: Cocos2d。我有一个数组CCSprite:

代码语言:javascript
复制
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:

代码语言:javascript
复制
- (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,而不是从第一个数组和第二个出现的数组中旋转)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-29 15:48:07

若要创建使用与已存在的sprite相同的纹理(图片)的sprite,请执行以下操作:

代码语言:javascript
复制
CCSprite* newSprite = [CCSprite spriteWithTexture:existingSprite.texture
                                             rect:existingSprite.textureRect];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20289177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档