Cocos2d iphone Cocos2D-sprite的userData属性
我正在使用Cocos2d开发一个iphone游戏应用程序 我发现这是一个很好的解决方案,可以解决我在精灵属性中存储一个整数的问题: sprite.userData=123Cocos2d iphone Cocos2D-sprite的userData属性,cocos2d-iphone,Cocos2d Iphone,我正在使用Cocos2d开发一个iphone游戏应用程序 我发现这是一个很好的解决方案,可以解决我在精灵属性中存储一个整数的问题: sprite.userData=123 但是,sprite.userData只能存储一段数据。如果我需要存储三段数据,最好的方法是什么?对于一个或九个自定义变量,最好的选择是将CCSprite子类化,并将自定义变量作为新类的类变量,使用属性公开读/写它们 就像一个精通Objective-C的程序员一样 明确回答您的问题:您可以将NSDictionary或NSArra
但是,sprite.userData只能存储一段数据。如果我需要存储三段数据,最好的方法是什么?对于一个或九个自定义变量,最好的选择是将CCSprite子类化,并将自定义变量作为新类的类变量,使用属性公开读/写它们 就像一个精通Objective-C的程序员一样
明确回答您的问题:您可以将NSDictionary或NSArray设置为userData,userData本身可以包含多个项。但是,如果您要这么做:请阅读上面的内容。'userData'实际上是无效指针,而不是由'Node'类保留的:
void *userData_;
因此,它可以指向任何数据结构或类(甚至是C函数) 将ccsprite类扩展到userdata类,并将该类用于任何要创建的变量。。。但是,您需要将该类用于所有检查和条件 这是密码 Userdata.h
//
// UserData.h
//
//
#import "CCSprite.h"
#import "Constants.h"
@class GameLayer;
@interface UserData : CCSprite {
int userDataType;
int tag;
int parentTag;
GameLayer *gameLayer;
BOOL readyForDeletion;
}
@property(nonatomic) int userDataType;
@property(nonatomic) int tag;
@property(nonatomic) int parentTag;
@property(nonatomic, assign) GameLayer *gameLayer;
@property(nonatomic) BOOL readyForDeletion;
-(id) initWithSpriteName:(NSString *)spriteName;
@end
以及userdata.mm
//
// UserData.mm
//
//
#import "UserData.h"
@implementation UserData
@synthesize userDataType;
@synthesize tag;
@synthesize parentTag;
@synthesize gameLayer;
@synthesize readyForDeletion;
-(id) initWithSpriteName:(NSString *)spriteName {
if (self = [super initWithFileName:spriteName]) {
}
return self;
}
-(void) onExit {
[super onExit];
}
-(void) dealloc {
[super dealloc];
}
@结束