0

我创建了一个将 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.mi中初始化对象

//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.hi中设置两个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;
        }
}
}
4

1 回答 1

0

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

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

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

使用上面的代码代替

n.mySprite = [CCSprite spriteWithFile:shapeFileName];

我希望这就是你要找的!

于 2012-09-28T02:12:56.947 回答