我正在開發使用Cocos2d的iphone遊戲應用程序。Cocos2D - 用於精靈的userData屬性
我覺得這是我的存儲問題的整數精靈的屬性優秀的解決方案:
sprite.userData = 123;
但是,sprite.userData只能存儲一塊數據。如果我需要存儲三部分數據,那麼最好的方法是什麼?
我正在開發使用Cocos2d的iphone遊戲應用程序。Cocos2D - 用於精靈的userData屬性
我覺得這是我的存儲問題的整數精靈的屬性優秀的解決方案:
sprite.userData = 123;
但是,sprite.userData只能存儲一塊數據。如果我需要存儲三部分數據,那麼最好的方法是什麼?
對於一個或九個自定義變量,最好的方法是對CCSprite進行子類化,並將您的自定義變量作爲新類的類變量,使用屬性來公開讀/寫它們。
像精通Objective-C程序員一樣。
要明確回答您的問題:您可以將NSDictionary或NSArray設置爲userData,其本身可以包含多個項目。但是,如果你走得很遠:閱讀上面。
「用戶數據」實際上是不是由「節點」類保留空指針:
void *userData_;
因此,它可以指向任何數據結構或類(或甚至是C函數)。
擴展ccsprite類到用戶數據類和使用該類任何不變量要創建的......然而,你將需要使用類所有檢查和條件....
這裏取代碼
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];
}
@end