在Mutable数组中更改对象CCSprite Image

时间:2012-09-23 03:15:27

标签: cocos2d-iphone nsmutablearray ccsprite

我创建了一个精灵对象,它将CCSprite设置为视觉

//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

然后在shapes.m我初始化对象

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

然后在gameplaylayershapes.h中我设置了两个NSMutableArray(我没有添加所有无关的变量。

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

然后我在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];
}
}

现在我要做的是更改targetshapearray中每个形状对象的shapeID和mySprite。我可以让shapeID改变,但我不能为我的生活改变mySprite图像,这会改变我在游戏中的外观。我一直收到EXC_BAD_ACCESS错误。任何帮助将不胜感激

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

1 个答案:

答案 0 :(得分:0)

要更改CCSprites图像,您必须更改其纹理。以下是如何执行此操作的示例代码块:

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

这会将sprite图像更改为“spriteimagefile.png”。

使用上述代码代替

n.mySprite = [CCSprite spriteWithFile:shapeFileName];

我希望这就是你要找的东西!