我有一个游戏,其中火球UIImage从屏幕上掉落,并且用户使用触摸屏移动玩家对象以避免它们。 8点之后,playAgainButton弹出。但是,这个playAgainButton按钮不起作用。它不会执行ResetGame方法中的任何代码,它实际上会重置我的角色对象“Dustin”的位置,我不想发生这种情况。我有一个连接到按钮的引用插座。如何让ResetGame中的代码生效?我的ViewController代码如下所示。
int theScore;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initializeTimer];
}
- (IBAction)ResetGame:(id)sender { //only thing that actually happens right now is that Dustin resets for some reason.
theScore = 0;
[self updateScore];
_Fireball.center = CGPointMake(64, 64);
_endLabel.hidden = true;
_playAgainButton.hidden = true;
[self gameLogic:theTimer];
}
- (void)updateScore {
_score.text = [[NSString alloc] initWithFormat:@"%d",theScore];
}
- (void) initializeTimer {
if(theTimer == nil)
{
theTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLogic:)];
}
theTimer.frameInterval = 1;
[theTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void) gameLogic:(CADisplayLink *) theTimer {
if((!CGRectIntersectsRect(_Fireball.frame, _Dustin.frame)) && (theScore < 8)){
if(_Fireball.center.y >600){
int num = arc4random() % 3;
if(num == 0){
_Fireball.center = CGPointMake(64, 64);
}
else if(num == 1){
_Fireball.center = CGPointMake(192, 64);
}
else if(num == 2){
_Fireball.center = CGPointMake(320, 64);
}
theScore = theScore + 1;
[self updateScore];
}
_Fireball.center = CGPointMake(_Fireball.center.x, _Fireball.center.y+12); }
else if(theScore == 8){
_Fireball.center = CGPointMake(_Fireball.center.x, _Fireball.center.y);
_endLabel.text = @"You Win!";
_endLabel.hidden = false;
_playAgainButton.hidden = false;
}
}
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event {
UITouch *touch = [touches anyObject];
_firstTouch = [touch locationInView:self.view];
_lastTouch = [touch locationInView:self.view];
_Dustin.center = CGPointMake(_firstTouch.x, _Dustin.center.y);
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_firstTouch = [touch locationInView:self.view];
_lastTouch = [touch locationInView:self.view];
_Dustin.center = CGPointMake(_firstTouch.x, _Dustin.center.y);
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_firstTouch = [touch locationInView:self.view];
_lastTouch = [touch locationInView:self.view];
_Dustin.center = CGPointMake(_firstTouch.x, _Dustin.center.y);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
答案 0 :(得分:0)
你的touchesBegan等方法并没有调用超级方式,因此你的应用程序正在捕获所有触摸,无法通过你的按钮。
为每个方法添加对super的调用。