2015-02-06 57 views
-1

RWGameData.h
#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

+1

请澄清。 Xcode是IDE(专门为特定环境编写其他程序的计算机程序),而不是语言或操作系统。你想引用当前打开的Xcode标签,还是UITabViewController的标签? – CaptJak 2015-02-06 20:16:38

+0

@CaptJak更新。我指的是我的选项卡视图控制器中的选定选项卡。 – 2015-02-06 20:21:54

+0

为什么你需要一个参考。你想从哪里改变标签的价值? – rdelmar 2015-02-06 20:23:50

回答

0

+(instancetype)loadInstance如果你能提供你想达到什么样的更多细节。

如果我理解正确的话,你想知道哪个标签是有源 所有你需要做的就是让tab.selectedIndex属性为您诠释代表activTabView的指数,那么你可以用它这样做任何事情,这将返回作为更改所选索引的文本

希望得到这个帮助。

+0

对不起,我很难解释我想要什么。从我的自定义类中,我试图更改已从另一个类声明并初始化的标签的文本值。对于每秒通过,我的目标是能够从我的自定义类更新此标签。 – 2015-02-06 20:33:35

0

从你的代码,我可以看到你的PrimaryViewController被确认为RWGameData即

@interface PrimaryViewController : UIViewController <RWGameStateProtocol> 

所以协议,RWGameData的委托设置为PrimaryViewController对象,然后实现在PrimaryViewController.m像

此委托方法
-(void)StateUpdateForGameData:(RWGameData*)data 
{ 
    //now update label 
    self.regularBubLabel.text = @"asdasd"; 
} 

编辑:

在PrimaryViewController.m viewDidLoad方法加入这行

[RWGameData sharedGameData].delegate = self; 

timercalled方法应该是这样

-(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); 
     } 

    if(self.delegate && [self.delegate respondsToSelector:@selector(StateUpdateForGameData:)]) 
     [self.delegate StateUpdateForGameData:self]; 

    } NSLog(@"Regular Bubble Count: %li", [RWGameData sharedGameData].regularBubbleCount); 
} 
+0

我的课程似乎无法识别UIViewController。它说这是一个未申报的类型?我想我需要一个导入。 – 2015-02-06 20:42:24

+0

是导入您的customViewController类,如果可以,请发布一些代码。 – 2015-02-06 20:50:58

+0

已更新,以便于清晰。 – 2015-02-06 21:04:50