0
我有一個遊戲,火球UIImage從屏幕上掉下來,用戶用觸摸屏移動玩家對象以避免它們。 8分後,playAgainButton彈出。但是,這個playAgainButton按鈕不起作用。它不執行ResetGame方法中的任何代碼,並且它實際上重置了我的角色對象「Dustin」的位置,我不想發生這種情況。我有一個連接到按鈕的參考插座。我如何讓ResetGame中的代碼正常工作?我的ViewController代碼如下。iOS:我怎樣才能使這個遊戲重置按鈕正常工作?
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.
}