Search code examples
cocos2d-iphonensmutablearrayccsprite

Changing an objects CCSprite Image in an Mutable array


i create a sprite object which sets a CCSprite as its visual

//shapes.h
@interface Shapes : CCSprite 
{
    CCSprite *mySprite;
    GamePlayLayerShapes *theGame;
    NSInteger shapeID;
}

@property (nonatomic, retain) CCSprite *mySprite;
@property (nonatomic, retain) GamePlayLayerShapes *theGame;
@property (nonatomic, readwrite) NSInteger shapeID;

@end

then in shapes.m i initialize the object

//shapes.m
@implementation Shapes

@synthesize mySprite, theGame, active, shapeID;

-(id) initWithGame:(GamePlayLayerShapes *)game andFile:(NSString *)file andNumber:  (NSInteger)number andZ:(NSInteger)z
{
self = [super init];
if(self != nil) 
{
    self.theGame = game;

    if (number > 25)
    {
        mySprite = [CCSprite spriteWithFile:file];
    }
    else
    {
        mySprite = [CCSprite spriteWithSpriteFrameName:file];
    }
    [theGame addChild:mySprite z:z];
    [mySprite setPosition:ccp(500, 200)];
    self.shapeID = number;
}
return self;
}

then in gameplaylayershapes.h i set up two NSMutableArray (i didn't add all irrelevant variables.

@interface GamePlayLayerShapes : CCLayer 
{
CCSpriteBatchNode *shapesSpriteBatchNode;
NSMutableArray *shapeArray;
NSMutableArray *targetShapeArray;
    Shapes *newShape;
}

then i set it all up in gameplaylayershapes.m

-(id) init
{
self = [super init];
if(self != nil)
{
    srandom(time(NULL));
    shapeArray = [[NSMutableArray alloc] init];
    targetShapeArray = [[NSMutableArray alloc] init];



    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"GameplayShapes.plist"];
    shapesSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"GameplayShapes.png"];

    [self addChild:shapesSpriteBatchNode];

    [self createTargetShape];

    [self runGame];

    }
return self;
}

-(void)createTargetShape
{
   //fills 4 variables with random numbers between 1 and 25
    targetShape1 = (arc4random() % 25) +1;
    targetShape2 = (arc4random() % 25) +1;
    targetShape3 = (arc4random() % 25) +1;
    targetShape4 = (arc4random() % 25) +1;

for (int x=1; x<5; x++)
{
    switch (x)
    {
        case 1:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape1];
            shapeNumber = targetShape1;
            break;
        case 2:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape2];
            shapeNumber = targetShape2;
            break;
        case 3:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape3];
            shapeNumber = targetShape3;
            break;
        case 4:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape4];
            shapeNumber = targetShape4;
            break;
        default:
            break;
    }
    newShape = [[Shapes alloc] initWithGame:self andFile:shapeFileName andNumber:shapeNumber andZ:10];
    [targetShapeArray addObject:newShape];
    [newShape release];
}
}

now what I'm trying to do is change the shapeID and mySprite for each shape object in targetshapearray. i can get the shapeID to change, but i cant for the life of me get the mySprite image to change which would change the appearance of it on my game. i keep getting a EXC_BAD_ACCESS error. Any Help would be appreciated

-(void)createNewTargetShapes
{
//fills 4 variables with random numbers between 1 and 25
targetShape1 = (arc4random() % 25) +1;
targetShape2 = (arc4random() % 25) +1;
targetShape3 = (arc4random() % 25) +1;
targetShape4 = (arc4random() % 25) +1;

for (int x=0; x<4; x++)
{
    Shapes * n = (Shapes *) [targetShapeArray objectAtIndex:x];
    switch (x)
        {
            case 0:
                n.shapeID = targetShape1;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape1];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            case 1:
                n.shapeID = targetShape2;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape2];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            case 2:
                n.shapeID = targetShape3;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape3];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            case 3:
                n.shapeID = targetShape4;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape4];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            default:
                break;
        }
}
}

Solution

  • To change a CCSprites image you must change its texture. Here is an example block of code for how to do that:

    CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"spriteimagefile.png"];
    [sprite setTexture: tex];
    

    This will change the sprites image to "spriteimagefile.png".

    Use the above code instead of

    n.mySprite = [CCSprite spriteWithFile:shapeFileName];
    

    I hope that's what you're looking for!