Having issues subclassing CCSprite. What did I miss?
I've recently picked up app writing as a hobby and I've run into a problem with my first app. My first app attempt is fairly simple...a domino game. I started out using an NSObject class to describe each tile, but I think I've decided that CCSprite is easier for me at the moment.
The first step was creating an NSArray of all the tiles and then shuffling them. This is where I'm getting stuck....
Domino.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface Domino : CCSprite {
int int_leading;
int int_trailing;
int int_suitrank;
int int_trickvalue;
NSString *str_tilename;
NSString *str_mirrortilename;
}
@property int int_leading,int_trailing, int_suitrank, int_trickvalue;
@property(nonatomic, retain) NSString *str_tilename;
@property(nonatomic, retain) NSString *str_mirrortilename;
-(void) print;
-(void) setTileName: (NSString *) theTileName;
-(void) setMirrorName: (NSString *) theMirrorName;
-(NSString *) str_tilename;
-(NSString *) str_mirrortilename;
@end
Domino.m
@implementation Domino
@synthesize int_leading,int_trailing, int_suitrank, int_trickvalue, str_tilename, str_mirrortilename;
//@synthesize char_tilename, char_mirrortilename;
-(void) print {
NSLog (@"%i/%i", int_leading, int_trailing);}
-(void) setTileName: (NSString *) theTileName;
{
str_tilename=[[NSString alloc] initWithString: theTileName];
}
-(void) setMirrorName: (NSString *) theMirrorName;
{
str_mirrortilename=[[NSString alloc] initWithString: theMirrorName];
}
-(NSString *) str_tilename
{
return str_tilename;
}
-(NSString *) str_mirrortilename
{
return str_mirrortilename;
}
@end
helloworld.m
// Import the interfaces
#import "HelloWorldLayer.h"
#import "Domino.h"
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
if( (self=[super init] )) {
//NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Domino *d00 =[[Domino alloc] init];
NSString *TileName= @"0-0.png";
NSString *MirrorName= @"0-0.png";
[d00 setTileName: TileName];
[d00 setMirrorName: MirrorName];
d00.int_leading=0;
d00.int_trailing=0;
.
.
.
.
.
.
.
.
movableSprites = [[NSMutableArray alloc] init];
//build initial array of tiles
NSArray *uniquetiles = [NSArray arrayWithObjects:
d00,d01,d02,d03,d04,d05,d06,
d11,d12,d13,d14,d15,d16,
d22,d23,d24,d25,d26,
开发者_如何学Python d33,d34,d35,d36,
d44,d45,d46,
d55,d56,
d66,
nil];
//shuffle integer array representing tile index number
int tile[28]; // array of tile row ids;
for (int i=0; i<28; i++) {
tile[i] = i; // fill the array in order
}
for (int i=0; i<(27-1); i++) {
int r = i + (arc4random() % (27-i)); // Random remaining position.
int temp = tile[i]; tile[i] = tile[r]; tile[r] = temp;
}
int i=30;
Domino *sprite =[[Domino alloc] init];
for(int h = 1; h < 8; ++h) {
for(int w=0;w<4; ++w){
i=tile[(h-1)*4+(w)];
NSString *name=[[uniquetiles objectAtIndex:i]valueForKey:@"str_tilename"];
CGPoint spriteOffset = ccp(100+60*w, 45+25*h);
NSLog(@" client Id : %@",[CCSprite spriteWithSpriteFrameName:name]);
sprite=[CCSprite spriteWithSpriteFrameName:name];
sprite.scale=.50;
sprite.int_leading=1; //program fails here
When I try to set the value of the tile, I end up getting a "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite setInt_leading:]" What am I doing different here than what I just did before when I defined d00?
Well for one thing that is bothering me, you never actually set up a image for the sprite to use. I see you setting up the strings to load an image but I don't see you loading the image.
More importantly I am guessing is that you did not set any rules for your properties. I expect making:
@property int int_leading,int_trailing, int_suitrank, int_trickvalue;
Into:
@property (nonatomic, retain) int int_leading,int_trailing, int_suitrank, int_trickvalue;
would fix your problem. (or assign)
精彩评论