2016-05-03 11 views
0

私は一時停止IBAction &ボタンの再開IBActionを持っていますが、ゲームプレイ中に収集するゲームポイントがあります。ポーズIBAction &に、異なるゲームポイントの整数値を保存するには、再開IBActionメソッドにそれらを渡します。整数をIBActionに格納して別のIBActionに渡す

注:IBActionを一時停止するとすべてのNSTIMERSが無効になり、再開IBActionによって再作成されます。

+2

ストアそれクラスのメンバ変数としてIBActionsが関連付けられているに。そして、オブジェクト指向プログラミングに関する初心者の本を読んでください。失礼なことではありませんが、これは非常に非常に非常に基本的なものです。この種のものを尋ねなければならない場合は、あまり遠くまでは行かないので、自分で好きなことを学び、入門チュートリアルを読んでください。 – Gruntcakes

+0

例を挙げることはできますか? – 1QuickQuestion

答えて

1

を実装するオブジェクトのインスタンス変数のようなあなたの状態を保存する必要があり、私は迅速な解決策を見つけた....が、私はクリーンな方法があります確信している:

でvc.h:vc.mで

NSMutableArray storeVariables; 

int a, b, c, d; 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
a=0; 
b=0; 
c=0; 
d=0; 
storeVariables = [[NSMutableArray alloc] init]; 
} 

-(IBAction)pauseButton:(id)sender{ 
a=3; 
b=4; 
c=5; 
d=6; 
[storeVariables addObject:[NSNumber numberWithInt:a]]; 
[storeVariables addObject:[NSNumber numberWithInt:b]]; 
[storeVariables addObject:[NSNumber numberWithInt:c]]; 
[storeVariables addObject:[NSNumber numberWithInt:d]]; 
NSLog(@"%d, %d, %d, %d", storeVariables[0], storeVariables[1], storeVariables[2], storeVariables[3]); 
} 

-(IBAction)resumeButton:(id)sender{ 
a = [storeVariables[0]]; 
b = [storeVariables[1]] 
c = [storeVariables[2]] 
d = [storeVariables[3]] 
NSLog(@"%d, %d, %d, %d", a, b, c, d); 
storeVariables = [[NSMutableArray alloc] init]; 
} 
+0

素晴らしい!ありがとう、これは本当に助けになった!私が作った唯一の大きな違いは、Arrayを2回インスタンス化しないことでした。私は古い値をクリアします!これは多くの助けになりました!私は将来的に必要とするかもしれない誰のためにも自分のコードを追加します – 1QuickQuestion

1

IBActionはコードです。何も保存することはできません。あなたがどこかにだけ支援するためにIBAction

0

は、これは働いていたものです: の.hファイル -

@interface ViewController2 : UIViewController{ 

    NSMutableArray *savedScoreData; 
    int PlayTimer; 
    int timeMinute; 
    int bonusPts; 
    int enemykill; 

} 

の.mファイル -

- (void)viewDidLoad { 

    PlayTimer = 0; 
    timeMinute = 0; 
    bonusPts = 0; 
    timeMinute = 0; 

    savedScoreData = [[NSMutableArray alloc] init]; 
} 

- (IBAction)pauseButton:(id)sender { 
    NSLog(@"pause-before: %d, %d, %d,",PlayTimer,enemykill,bonusPts); 
    [GameTimer invalidate]; //this constantly changes the PlayTimer & timeMinute variables 
    [characterTimer invalidate]; 
    [enemyTimer invalidate]; //this constantly changes the enemyKill & bonusPts variables 
    [_bonusImgHolder removeFromSuperview]; 
    if([_soundEnable isEqualToString:@"YES"]){ 
     [_player2 pause]; 
    } 
    //NSLog(@"%tu",_player2.playing); 
    _resumeButton.hidden = NO; 
    _pauseButton.hidden = YES; 
    NSLog(@"pause-after: %d, %d, %d,",PlayTimer,enemykill,bonusPts); 
    //[sender num :PlayTimer]; 
    [savedScoreData addObject:[NSNumber numberWithInteger: PlayTimer]]; 
    [savedScoreData addObject:[NSNumber numberWithInteger: timeMinute]]; 
    [savedScoreData addObject:[NSNumber numberWithInteger: enemykill]]; 
    [savedScoreData addObject:[NSNumber numberWithInteger: bonusPts]]; 
    NSLog(@"%i", [savedScoreData[0] intValue]); 
    NSLog(@"%d", [savedScoreData[1] intValue]); 
    NSLog(@"%d", [savedScoreData[2] intValue]); 
    NSLog(@"%d", [savedScoreData[3] intValue]); 

} 
- (IBAction)resumeButton:(int)pauseButton{ 
    NSLog(@"resume-before: %d, %d, %d,",PlayTimer,enemykill,bonusPts); 
    PlayTimer = [savedScoreData[0] intValue]; 
    timeMinute = [savedScoreData[1] intValue]; 
    enemykill = [savedScoreData[2] intValue]; 
    bonusPts = [savedScoreData[3] intValue]; 
    NSLog(@"%d", (int)enemykill); 
    _resumeButton.hidden = YES; 
    _pauseButton.hidden = NO; 
    if([_soundEnable isEqualToString:@"YES"]){ 
     [_player2 play]; 
    } 
    GameTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(CollectPoints) userInfo:nil repeats:YES]; 
    characterTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(Jumping) userInfo:nil repeats:YES]; 
    enemyTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(enemyTravel) userInfo:nil repeats:YES]; 
    NSLog(@"resume-after: %d, %d, %d,",PlayTimer,enemykill,bonusPts); 
    [savedScoreData removeAllObjects]; 
} 
関連する問題