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 で 2 つの 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;
}
}
}