有问题继承CCSprite。我错过了什么?

时间:2011-08-20 04:50:34

标签: iphone objective-c ios cocos2d-iphone

我最近把应用写作作为一种爱好,我遇到了第一个应用程序的问题。我的第一个应用尝试相当简单......一个多米诺骨牌游戏。我开始使用NSObject类来描述每个tile,但我想我现在已经决定CCSprite对我来说更容易了。

第一步是创建所有瓷砖的NSArray,然后将它们洗牌。这就是我被卡住的地方......

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

当我尝试设置磁贴的值时,我最终得到一个“因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:' - [CCSprite setInt_leading:]”我在这里做的不同于我刚刚做的事情之前我定义了d00吗?

1 个答案:

答案 0 :(得分:0)

有一件事困扰着我,你实际上从未为精灵设置图像。我看到你设置字符串来加载图像,但我没有看到你加载图像。

更重要的是,我猜你没有为你的房产设置任何规则。我期待制作:

@property  int int_leading,int_trailing, int_suitrank, int_trickvalue; 

分为:

@property  (nonatomic, retain) int int_leading,int_trailing, int_suitrank, int_trickvalue; 

会解决您的问题。 (或指定)