开发者

Image replacement in iphone

I have added some sprite by using the following code and then by checking the rect intersection I want to replace these sprites with another one.Can you help me out by providing code to replace these random images.Problem is that the image to be replaced is also taken randomly.........

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


I have did this using sprite sheet. if you can create a sprite sheet and then assign that sprite sheet to CCSpriteBatchNode's object like

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

and then load plist

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

and then use sprite frames to assign images to your sprite like

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

i used TexrturePacker to make sprite sheets

hope this helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜