如何从Mutable Object Array获取NSString值?

时间:2012-05-04 05:53:51

标签: objective-c cocos2d-iphone

所以我正在我的应用程序中我需要替换精灵图像的点,我想我知道该怎么做,但我在实现它时遇到了麻烦。基本上,我正在制作一个多米诺骨牌游戏,我需要能够将多米诺骨牌翻过来,这样你才能看到数字。

从我的domino.h文件开始....

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Domino : CCSprite {
    int int_leading;
    int int_trailing;
    int int_suitrank;
    int int_playerid;

    NSString *str_tilename;
    NSString *str_mirrortilename;    
}


@property  int int_leading,int_trailing, int_playerid, 

@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

然后是我的.m文件......

#import "Domino.h"

@implementation Domino

@synthesize int_leading,int_trailing, str_tilename, str_mirrortilename, int_playerid;

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

最后,在我的游戏层......

Domino *d06 =[[Domino alloc] initWithSpriteFrameName:@"blank.png"];
TileName= @"0-6.png";
MirrorName= @"6-0.png";
[d06 setTileName: TileName];
[d06 setMirrorName: MirrorName];
d06.int_leading=0;
d06.int_trailing=6;

此时,我将所有多米诺骨牌一起添加到一个大的可变数组中,该数组跟踪哪些仍可供玩家选择。我遇到的问题是我要么没有找到如何从可变数组中取出“TileName”,要么我找到了它,但是没有理解它。

如果我在For循环中,我认为代码应该是

 NSString *temp1=[[[movableSprites objectAtIndex:i]valueForKey:@"str_tilename"]string];

但这只会导致程序崩溃。你能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

 NSString *temp1=[(Domino *)[movableSprites objectAtIndex:i] str_tilename];

我认为使用此代码将解决您的问题...检查语法..希望这有助于..:)