在iphone中更换图像

时间:2011-07-07 07:14:21

标签: iphone image replace cocos2d-iphone

我已经使用以下代码添加了一些精灵,然后检查了rect 我希望用另一个精灵替换这些精灵。你可以通过提供代码来帮助我  替换这些随机图像。问题是还要拍摄要替换的图像 随机.........

             yp = 40;
    //CCSprite *spr;

    movableSprites = [[NSMutableArray alloc] init];

    for(int m=0; m<4; m++)
    {               
        do
        {
            i = 'a';
            //NSLog(@"valueeeeeeeee %d",i);
            next = arc4random()%maxalphabets;
            //NSLog(@"valueeeeeeeeenexttttt %d",next);
            i+=next;
            //NSLog(@"valueeeeeeeee %d",i);

            spr = (CCSprite*)[self getChildByTag:i];//checks if found alreadyyyyyy
            NSLog(@"Strrrrrrrr is:%@",spr);
        }while(spr);

        spNameStr = [NSString stringWithFormat:@"%c3.png",i];
        NSLog(@"tagggg %d",i);
        NSLog(@"spNameStr is:%@",spNameStr);
        spr = [CCSprite spriteWithFile:spNameStr ];
        spr.tag = i;
        //NSLog(@"tagiiiiii %d",i);
        spr.position = ccp(60,yp);  
        [self addChild:spr z:2];
        [movableSprites addObject:spr];
        yp+=spr.contentSize.height+35;
    }

    yp = 40;

    NSMutableArray *answerimagesCopy = [NSMutableArray arrayWithArray:movableSprites];
    NSLog(@"answer image copy elements areeeee %@",answerimagesCopy);
    for(k = 0; k < movableSprites.count; ++k) 
    {
        NSLog(@"inside forrrr");
        int j=arc4random()%([answerimagesCopy count]);
        NSLog(@"valueee of jjjjjjjj %d",j);
        CCSprite *ansimage = [answerimagesCopy objectAtIndex:j];
        //NSLog(@"................ %@",ansimage);
        p=ansimage.tag;
        NSLog(@"tagg..... %d",p);
        [answerimagesCopy removeObjectAtIndex:j];
        //NSLog(@"tagg..... %d",j);


        spNameStr = [NSString stringWithFormat:@"%c4.png",p];   
        NSLog(@"spNameStr is:%@",spNameStr);
        //NSLog(@"tagggg %d",j);
        spr = [CCSprite spriteWithFile:spNameStr ];
        spr.tag = p+100;
        spr.position = ccp(260,yp);
        [self addChild:spr z:1];
        yp+=spr.contentSize.height+35;
    }

1 个答案:

答案 0 :(得分:0)

我使用精灵表做了这个。如果你可以创建一个精灵表,然后将该精灵表分配给CCSpriteBatchNode的对象,如

$ CCSpriteBatchNod * m_meteoritesSprites = [CCSpriteBatchNode batchNodeWithFile:@“Meteo.pvr.ccz”];

然后加载plist

$ [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@“Meteo.plist”];

然后使用精灵帧将图像分配给精灵,如

$ m_meteoritesSprites = [CCSpriteBatchNode batchNodeWithFile:@“Meteo.pvr.ccz”];

我使用TexrturePacker制作精灵表

希望这会对你有所帮助。