简体   繁体   中英

How do I get NSString values from an Mutable Object Array?

So I'm at the point in my app where I need to replace the image of a sprite and I think I know how to do it, but I'm having trouble implementing it. Basically, I'm making a domino game and I need to be able to flip the domino over so you can see the numbers.

Starting with my domino.h file....

#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

and then my .m file...

#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

Finally, in my game layer...

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;

At this point, I add all the dominoes together into a large mutable array that tracks which ones are still available for players to pick. The problem I'm having is that I either haven't found how to pull out the "TileName" from the mutable array, or maybe I have found it, but not understood it.

If I'm in a For loop, I would think the code should be something like

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

But this just results in the program crashing. Can you point me in the right direction?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM