#import <Foundation/Foundation.h>
@class RWGameData;
@protocol RWGameStateProtocol <NSObject>
-(void)StateUpdateForGameData:(RWGameData*)data;
@end
@interface RWGameData : NSObject <NSCoding>
@property (weak) id<RWGameStateProtocol> delegate;
@property (assign, nonatomic) long regularBubbleCount;
@property (assign, nonatomic) long premiumBubbleCount;
@property (assign, nonatomic) long megaBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleFactoryUpgradeTier;
@property (assign, nonatomic) long bubblersUpgradeTier;
@property (assign, nonatomic) long mysteryBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleBankUpgradeTier;
@property (assign, nonatomic) int megaBubblePopValue;
@property (assign, nonatomic) int bubbleFactoryTickValue;
@property (assign, nonatomic) long bubbleBankCapacity;
@property (assign, nonatomic) int collectionBallsQuantity;
@property (assign, nonatomic) int collectionGlowsticksQuantity;
@property (assign, nonatomic) int collectionFlowersQuantity;
@property (assign, nonatomic) int collectionStuffedAnimalsQuantity;
@property (assign, nonatomic) int collectionEasterEggsQuantity;
@property (assign, nonatomic) int currentXP;
@property (assign, nonatomic) int targetXP;
@property (assign, nonatomic) int level;
@property (assign, nonatomic) BOOL dataIsInitialized;
+(instancetype)sharedGameData;
-(void)reset;
-(void)save;
-(void)timerSetup;
-(void)timerCalled;
@end
RWGameData.m
#import "RWGameData.h"
@implementation RWGameData
static NSString* const SSGameDataRegularBubbleCountKey = @"regularBubbleCount";
static NSString* const SSGameDataPremiumBubbleCountKey = @"premiumBubbleCount";
static NSString* const SSGameDataMegaBubbleUpgradeTierKey = @"megaBubbleUpgradeTier";
static NSString* const SSGameDataBubbleFactoryUpgradeTierKey = @"bubbleFactoryUpgradeTier";
static NSString* const SSGameDataBubblersUpgradeTierKey = @"bubblersUpgradeTier";
static NSString* const SSGameDataMysteryBubbleUpgradeTierKey = @"mysteryBubbleUpgradeTier";
static NSString* const SSGameDataBubbleBankUpgradeTierKey = @"bubbleBankUpgradeTier";
static NSString* const SSGameDataMegaBubblePopValueKey = @"megaBubblePopValueKey";
static NSString* const SSGameDataBubbleFactoryTickValueKey = @"bubbleFactoryTickValueKey";
static NSString* const SSGameDataBubbleBankCapacityKey = @"bubbleBankCapacityKey";
static NSString* const SSGameDataCollectionBallsQuantityKey = @"collectionBallsQuantityKey";
static NSString* const SSGameDataCollectionGlowsticksQuantityKey = @"collectionGlowsticksQuantityKey";
static NSString* const SSGameDataCollectionFlowersQuantityKey = @"collectionFlowersQuantityKey";
static NSString* const SSGameDataCollectionStuffedAnimalsQuantityKey = @"collectionStuffedAnimalsQuantityKey";
static NSString* const SSGameDataCollectionEasterEggsQuantityKey = @"collectionEasterEggsQuantityKey";
/*static NSString* const SSGameDataCollectionBallsModifierKey = @"collectionBallsModifierKey";
static NSString* const SSGameDataCollectionGlowsticksModifierKey = @"collectionGlowsticksModifierKey";
static NSString* const SSGameCollectionFlowersModifierKey = @"collectionFlowersModifierKey";
static NSString* const SSGameCollectionStuffedAnimalsModifierKey = @"collectionStuffedAnimalsModifierKey";
static NSString* const SSGameCollectionEasterEggsModifierKey = @"collectionEasterEggsModifierKey";*/
static NSString* const SSGameDataCurrentXPKey = @"currentXPKey";
static NSString* const SSGameDataTargetXPKey = @"targetXPKey";
static NSString* const SSGameDataLevelKey = @"levelKey";
static NSString* const SSGameDataIsInitializedKey = @"dataIsInitializedKey";
+ (instancetype)sharedGameData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self loadInstance];
});
return sharedInstance;
}
-(void)reset {
self.regularBubbleCount = 0;
self.premiumBubbleCount = 0;
self.megaBubbleUpgradeTier = 0;
self.bubbleFactoryUpgradeTier = 0;
self.bubblersUpgradeTier = 0;
self.mysteryBubbleUpgradeTier = 0;
self.bubbleBankUpgradeTier = 0;
self.megaBubblePopValue = 1;
self.bubbleFactoryTickValue = 1;
self.bubbleBankCapacity = 500;
self.collectionBallsQuantity = 0;
self.collectionGlowsticksQuantity = 0;
self.collectionFlowersQuantity = 0;
self.collectionStuffedAnimalsQuantity = 0;
self.collectionEasterEggsQuantity = 0;
self.currentXP = 0;
self.targetXP = 83;
self.level = 1;
self.dataIsInitialized = true;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeDouble:self.regularBubbleCount forKey: SSGameDataRegularBubbleCountKey];
[encoder encodeDouble:self.premiumBubbleCount forKey: SSGameDataPremiumBubbleCountKey];
[encoder encodeDouble:self.megaBubbleUpgradeTier forKey: SSGameDataMegaBubbleUpgradeTierKey];
[encoder encodeDouble:self.bubbleFactoryUpgradeTier forKey: SSGameDataBubbleFactoryUpgradeTierKey];
[encoder encodeDouble:self.bubblersUpgradeTier forKey: SSGameDataBubblersUpgradeTierKey];
[encoder encodeDouble:self.mysteryBubbleUpgradeTier forKey: SSGameDataMysteryBubbleUpgradeTierKey];
[encoder encodeDouble:self.bubbleBankUpgradeTier forKey: SSGameDataBubbleBankUpgradeTierKey];
[encoder encodeDouble:self.megaBubblePopValue forKey: SSGameDataMegaBubblePopValueKey];
[encoder encodeDouble:self.bubbleFactoryTickValue forKey: SSGameDataBubbleFactoryTickValueKey];
[encoder encodeDouble:self.bubbleBankCapacity forKey: SSGameDataBubbleBankCapacityKey];
[encoder encodeDouble:self.collectionBallsQuantity forKey: SSGameDataCollectionBallsQuantityKey];
[encoder encodeDouble:self.collectionGlowsticksQuantity forKey: SSGameDataCollectionGlowsticksQuantityKey];
[encoder encodeDouble:self.collectionFlowersQuantity forKey: SSGameDataCollectionFlowersQuantityKey];
[encoder encodeDouble:self.collectionStuffedAnimalsQuantity forKey: SSGameDataCollectionStuffedAnimalsQuantityKey];
[encoder encodeDouble:self.collectionEasterEggsQuantity forKey: SSGameDataCollectionEasterEggsQuantityKey];
[encoder encodeDouble:self.currentXP forKey: SSGameDataCurrentXPKey];
[encoder encodeDouble:self.targetXP forKey: SSGameDataTargetXPKey];
[encoder encodeDouble:self.level forKey:SSGameDataLevelKey];
[encoder encodeBool:self.dataIsInitialized forKey: SSGameDataIsInitializedKey];
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [self init];
if (self) {
_regularBubbleCount = [decoder decodeDoubleForKey: SSGameDataRegularBubbleCountKey];
_premiumBubbleCount = [decoder decodeDoubleForKey: SSGameDataPremiumBubbleCountKey];
_megaBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMegaBubbleUpgradeTierKey];
_bubbleFactoryUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryUpgradeTierKey];
_bubblersUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubblersUpgradeTierKey];
_mysteryBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMysteryBubbleUpgradeTierKey];
_bubbleBankUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleBankUpgradeTierKey];
_megaBubblePopValue = [decoder decodeDoubleForKey: SSGameDataMegaBubblePopValueKey];
_bubbleFactoryTickValue = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryTickValueKey];
_bubbleBankCapacity = [decoder decodeDoubleForKey: SSGameDataBubbleBankCapacityKey];
_collectionBallsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionBallsQuantityKey];
_collectionGlowsticksQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionGlowsticksQuantityKey];
_collectionFlowersQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionFlowersQuantityKey];
_collectionStuffedAnimalsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionStuffedAnimalsQuantityKey];
_collectionEasterEggsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionEasterEggsQuantityKey];
_currentXP = [decoder decodeDoubleForKey: SSGameDataCurrentXPKey];
_targetXP = [decoder decodeDoubleForKey: SSGameDataTargetXPKey];
_level = [decoder decodeDoubleForKey: SSGameDataLevelKey];
_dataIsInitialized = [decoder decodeBoolForKey: SSGameDataIsInitializedKey];
}
return self;
}
+(NSString*)filePath
{
static NSString* filePath = nil;
if (!filePath) {
filePath =
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
stringByAppendingPathComponent:@"gamedata"];
}
return filePath;
}
+(instancetype)loadInstance
{
NSData* decodedData = [NSData dataWithContentsOfFile: [RWGameData filePath]];
if (decodedData) {
RWGameData* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
return gameData;
}
return [[RWGameData alloc] init];
}
-(void)save
{
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
[encodedData writeToFile:[RWGameData filePath] atomically:YES];
}
- (void)timerSetup { // to be called from delegate didFinishLaunching….
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled) userInfo:nil repeats:YES];
}
-(void)timerCalled
{
//NSLog(@"Timer Called");]
if ([RWGameData sharedGameData].bubbleFactoryUpgradeTier > 0) {
if ([RWGameData sharedGameData].regularBubbleCount < [RWGameData sharedGameData].bubbleBankCapacity) {
[RWGameData sharedGameData].regularBubbleCount += [RWGameData sharedGameData].bubbleFactoryTickValue;
[[RWGameData sharedGameData] save];
} else {
NSLog(@"Capacity Reached! Capacity: %li", [RWGameData sharedGameData].bubbleBankCapacity);
}
[self.delegate StateUpdateForGameData:self];
} NSLog(@"Regular Bubble Count: %li", [RWGameData sharedGameData].regularBubbleCount);
}
@end
PrimaryViewController.h
#import <UIKit/UIKit.h>
#import "RWGameData.h"
@interface PrimaryViewController : UIViewController <RWGameStateProtocol>
@property (strong, nonatomic) IBOutlet UILabel *regularBubLabel;
@end
我想能够regularBubLabel的值从timerCalled方法内变化。感谢您的时间。在我的TabViewController中创建对选定选项卡的引用?
包含我的RWGameData类的完整请求。谢谢。
AppDelegate.h
#import <UIKit/UIKit.h>
#import "RWGameData.h"
#import "PrimaryViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) RWGameData *gameData;
@end
后来看到的#RWGameData.m
请澄清。 Xcode是IDE(专门为特定环境编写其他程序的计算机程序),而不是语言或操作系统。你想引用当前打开的Xcode标签,还是UITabViewController的标签? – CaptJak 2015-02-06 20:16:38
@CaptJak更新。我指的是我的选项卡视图控制器中的选定选项卡。 – 2015-02-06 20:21:54
为什么你需要一个参考。你想从哪里改变标签的价值? – rdelmar 2015-02-06 20:23:50