我有一個暫停IBAction &一個簡歷IBAction,都是用於按鈕的,但我有我在遊戲期間收集的遊戲點數。如何在暫停IBAction &中將不同的遊戲點整數值存儲到繼續IBAction方法中。在IBAction中存儲整數並將它們傳遞給另一個IBAction
注意:暫停IBAction使所有NSTIMERS無效,並且恢復IBAction重新創建它們。
我有一個暫停IBAction &一個簡歷IBAction,都是用於按鈕的,但我有我在遊戲期間收集的遊戲點數。如何在暫停IBAction &中將不同的遊戲點整數值存儲到繼續IBAction方法中。在IBAction中存儲整數並將它們傳遞給另一個IBAction
注意:暫停IBAction使所有NSTIMERS無效,並且恢復IBAction重新創建它們。
對象的實例變量只是爲了幫助,我找到了一個快速的解決方案....但我敢肯定有一個更清潔方法:
在在vc.h:
NSMutableArray storeVariables;
在vc.m
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];
}
太棒了!謝謝,這真的有幫助!我唯一的主要區別是沒有兩次實例化Array,我只是清除舊的值!這非常有幫助!我將在未來爲任何可能需要它的人添加我的代碼 – 1QuickQuestion
IBAction是代碼。它不能存儲任何東西。您需要保存在某個地方你的狀態一樣,實現IBAction爲
這是什麼工作: 的.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];
}
存儲它作爲類的成員變量到所述IBActions與相關聯。並閱讀一本關於面向對象編程的初學者書。不要粗魯,但這是非常非常非常基本的東西,如果你不得不問這樣的東西,你不會很遠,所以請你幫個忙,閱讀一些入門教程。 – Gruntcakes
你能提供一個例子嗎? – 1QuickQuestion