在 IBAction 中存储整数并将它们传递给另一个 IBAction
Storing integers in an IBAction and passing them to another IBAction
我有一个暂停 IBAction 和一个恢复 IBAction,两者都用于按钮,但我有在游戏过程中收集的游戏点数。我如何在暂停 IBAction 中存储不同的游戏点整数值并将它们传递给恢复 IBAction 方法。
注意:pause IBAction 使所有 NSTIMERS 失效,resume IBAction 重新创建它们。
IBAction 是代码。它不能存储任何东西。您需要将状态保存在某个地方,例如实现 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];
}
这是有效的:
.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];
}
我有一个暂停 IBAction 和一个恢复 IBAction,两者都用于按钮,但我有在游戏过程中收集的游戏点数。我如何在暂停 IBAction 中存储不同的游戏点整数值并将它们传递给恢复 IBAction 方法。
注意:pause IBAction 使所有 NSTIMERS 失效,resume IBAction 重新创建它们。
IBAction 是代码。它不能存储任何东西。您需要将状态保存在某个地方,例如实现 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];
}
这是有效的: .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];
}