timer countdown
  [ Ignoră ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Am de dezvolat un mic joc care functioneaza in felul urmator, ca sa pornesc 1 joc am nevoie de 1 token (se primesc initial 20 token-uri).
La fiecare 30 minute se primeste un token iar pe screen-ul de “game over” trebuie sa afisez un timer countdown gen: “token available in 22:30” care sa fie actualizat la fiecare secunda.
M-am gandit sa folosesc un NSTimer in app delegate, sa am un int cu o valoare initiala de 1800 (30 minute * 60 secunde) iar timer-ul la fiecare secunda sa apeleze o metoda care va scadea valoarea int-ului cu 1. cand ajunge la 0 se primeste token-ul si se reseteaza valoarea la 1800.
Problema vine acum, am nevoie ca valoarea acestui int (care se modifica la fiecare secunda, scade cu 1) sa o folosesc in controller-ul de game over unde sa afisez un label cu timer countdown gen: 22:30 si acela sa fie la randul sau actualizat la secunda.
O prima idee ce mi-a venit in cap a fost sa trimit din app delegate un NSNotification la fiecare secunda dar sigur e o idee proasta dpdv al performanei smile
Orice idee e bine venita.
Multumesc antificat,

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 1 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

anticipat smile

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 2 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Am rezolvat intre timp: Key-Value Observing smile

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 3 ]
Avatar
RankRankRankRank
Moderator
Din: Cluj-Napoca
Macuser din: 26.01.06

Cum mai exact?

 Semnătură 

Mcintoshing…

Profil
 
  [ Ignoră ]   [ # 4 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Am folosit 2 metode de aici si le-am implementat in app delegate:

#pragma mark TimerKeyValueObservingProtocol Methods

- (voidaddTheObserver: (ContainerViewController *) sender
{
    NSLog
(@"%@",NSStringFromSelector(_cmd));
    
[self addObserversender.gameOverViewController forKeyPath: @"rootTimerValue" optionsNSKeyValueObservingOptionNew contextnil];
}


- (voidremoveTheObserver: (ContainerViewController *) sender
{
    NSLog
(@"%@",NSStringFromSelector(_cmd));
    
[self removeObserversender.gameOverViewController forKeyPath: @"rootTimerValue"];

App delegate are ca root un container controller care face tranzitia intre controllerele jocului, aici apelez metodele de mai sus (din app delegate)

- (voidgotoGameOverScreenCallbackWithSender: (GameStartedViewController *) sender
{
    NSLog
(@"%@",NSStringFromSelector(_cmd));    
    
self.gameOverViewController [[[GameOverViewController alloc] initWithNibName: @"GameOverViewController" bundle:nil] autorelease];
    
[self.gameStartedViewController willMoveToParentViewControllernil];
    
[self addChildViewControllerself.gameOverViewController];
    
    
// add the gameOverViewController as an observer using addObserver:forKeyPath:options:context: method
    
[self.delegate addTheObserverself];
    
    
[self transitionFromViewControllerself.gameStartedViewController toViewControllerself.gameOverViewController duration1.0 options:UIViewAnimationOptionTransitionFlipFromRight animationsnil completion: ^(BOOL finished){
        [self
.gameOverViewController didMoveToParentViewControllerself];
        
self.gameOverViewController.delegate self;
        
[self.gameStartedViewController removeFromParentViewController];
    
}];
    
}


#pragma mark GameOverProtocol method

- (voidplayNewGameWithSender: (GameOverViewController *) sender
{
    NSLog
(@"%@",NSStringFromSelector(_cmd));
    
    
self.gameStartedViewController [[[GameStartedViewController alloc] initWithNibName: @"GameStartedViewController" bundle:nil] autorelease];
    
[self.gameOverViewController willMoveToParentViewControllernil];
    
[self addChildViewControllerself.gameStartedViewController];
    
    
// remove gameOverViewController as an observer using removeObserver:forKeyPath:
    
[self.delegate removeTheObserverself];

    
    
[self transitionFromViewControllerself.gameOverViewController toViewControllerself.gameStartedViewController duration1.0 optionsUIViewAnimationOptionTransitionFlipFromRight animationsnil completion: ^(BOOL finished){
        [self
.gameStartedViewController didMoveToParentViewControllerself];
        
self.gameStartedViewController.delegate self;
        
[self.gameOverViewController removeFromParentViewController];
    
}];
 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 5 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Am uitat sa pun aici inca 3-4 metode:
In app delegate implementarea timer-ului si a metodei care este executata la fiecare secunda:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog
(@"%@",NSStringFromSelector(_cmd));
    
self.myTimer [NSTimer scheduledTimerWithTimeInterval1.0 targetself selector: @selector(timerMethod:) userInfonil repeatsYES];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog
(@"%@",NSStringFromSelector(_cmd));
    if(
[self.myTimer isValid])
        
[self.myTimer invalidate];
    
[[NSUserDefaults standardUserDefaults] setIntegerself.rootTimerValue forKey: @"self.rootTimerValue"];
    
[[NSUserDefaults standardUserDefaults] synchronize];
 
}

- (voidtimerMethod: (NSTimer *) theTimer
{
    
if(self.rootTimerValue 0)
        
self.rootTimerValue--;
    else
    
{
        
// MAKE 1 TOKEN AVAILABLE
        
int tokens [[NSUserDefaults standardUserDefaults] integerForKey: @"tokens"];
        
NSLog(@"tokens %d",tokens);
        
tokens++;
        
[[NSUserDefaults standardUserDefaults] setIntegertokens forKey: @"tokens"];
        
[[NSUserDefaults standardUserDefaults] synchronize];
        
NSLog(@"tokens %d",tokens);
        
self.rootTimerValue 1800;
    
}

In controller-ul de game over unde afisez timer-ul se apeleaza urmatoarea metoda de fiecare data cand valoarea variabilei “rootTimerValue” din app delegate este modificata

#pragma mark NSKeyValueObservingProtocol method

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
if([keyPath isEqualToString: @"rootTimerValue"])
    
{
        NSNumber 
*theValue [change objectForKeyNSKeyValueChangeNewKey];
        
int value [theValue intValue];
        
int minute value 60;
        
int secunde value 60;
        if(
self.view.superview != nil)
            
self.getTokensTimerLabel.text [NSString stringWithFormat: @"%.2d:%.2d",minute,secunde];
    
}


Sper sa va fie de folos daca aveti nevoie de asa ceva smile

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil