所以我在我的应用程序中需要替换精灵的图像,我想我知道该怎么做,但是我在实现它时遇到了麻烦。基本上,我正在制作多米诺骨牌游戏,我需要能够将多米诺骨牌翻转过来,这样你才能看到数字。
从我的 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];
但这只会导致程序崩溃。你能为我指出正确的方向吗?