在制作if语句时,我是否可以将其作为一个已经运行的空白被覆盖?

时间:2015-08-19 14:37:07

标签: ios objective-c

我的游戏中有关于红圈,蓝圈和黄圈的错误。你只能触摸红色圆圈。我有一个随机按钮选择器,我称之为buttonChanger。每当运行此方法时,它会选择一个随机按钮,并在一定时间后按钮消失并再次运行该方法。我想知道我是否发表了声明

If Bluecircle.hidden = YES, Redcircle.hidden = YES, yellowcircle.hidden = YES{
[self buttonChanger];
}
这是我被困的地方。如果我只运行这个if语句,按钮将不断变化,它将继续重叠虚空,直到它崩溃。我想知道如果在这个if语句中我可以创建另一个if语句来覆盖前一个语句,那么一切都会正常运行并且空格不会重叠。 像这样:

    If Bluecircle.hidden = YES, Redcircle.hidden = YES, yellowcircle.hidden = YES{
  if buttonChanger is running{
    }
    else{
        [self buttonChanger];
    }
        }

如果您希望在此处查看我的代码,请执行以下操作:

#import "ViewController.h"
#import "ViewController.m"


@interface ViewController ()

@end

int RandomNumber;
double ButtonTime;
int RandomNumberButton;
int score;
int highscoreint;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        if ([[UIScreen mainScreen] scale] == 2.0) {

            if([UIScreen mainScreen].bounds.size.height == 667){
                // iPhone retina-4.7 inch(iPhone 6)
            }
            else if([UIScreen mainScreen].bounds.size.height == 568){
                // iPhone retina-4 inch(iPhone 5 or 5s)
            }
            else{
                // iPhone retina-3.5 inch(iPhone 4s)
            }
        }
        else if ([[UIScreen mainScreen] scale] == 3.0)
        {
            //if you want to detect the iPhone 6+ only
            if([UIScreen mainScreen].bounds.size.height == 736.0){
                //iPhone retina-5.5 inch screen(iPhone 6 plus)
            }
            //iPhone retina-5.5 inch screen(iPhone 6 plus)
        }


    }
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
@implementation Game{


}


-(void)viewDidAppear:(BOOL)animated{

    Play.hidden = NO;


}
- (void)viewDidLoad {

    [super viewDidLoad];
    scorelabeldisplay.hidden = YES;
    // Do any additional setup after loading the view, typically from a nib.
    GameOver.hidden = YES;
    Menu.hidden = YES;
    RedCircle2.hidden = YES;
    YellowCircle.hidden = YES;
    BlueCircle.hidden = YES;
    score = 0;
    scoreLabel.font = [UIFont systemFontOfSize:25];
    scoreString = [NSString stringWithFormat:@"Score:%i", score];
    scoreLabel.text = scoreString;
    GameOver.font = [UIFont systemFontOfSize:50];
    scorelabeldisplay.font = [UIFont systemFontOfSize:25];
    highscore.hidden = YES;
    highscore.font = [UIFont systemFontOfSize:25];
    NSURL *SoundURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Ding.wav", [[NSBundle mainBundle] resourcePath]]];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);


}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark iAd Delegate Methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [banner setAlpha:2];
    [UIView commitAnimations];

}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{


    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [banner setAlpha:0];
    [UIView commitAnimations];

}


-(double)generateButtonTime {
    NSInteger randomNumber = arc4random_uniform(100);
    double buttomTime = 0;
    if (randomNumber>50) {
        if (randomNumber<75) {


        buttomTime = 0.5;
        }}
    if (randomNumber>75){



        buttomTime = 1.0;
        }
    if (randomNumber<50) {
        buttomTime = 0.4 ;
    }

    if (randomNumber>10){
        if (randomNumber<50) {


        buttomTime = 0.2 ;
        }}
    return buttomTime;
}


-(void)enableBlueCircle:(NSTimer*)timer {
    BlueCircle.hidden = YES;
    [self buttonChanger];


}

-(void)enableYellowCircle:(NSTimer*)timer {
    YellowCircle.hidden = YES;
    [self buttonChanger];


}


-(void)fixbug{
    if (RedCircle2.hidden = YES, BlueCircle.hidden = YES, YellowCircle.hidden = YES) {

        [self buttonChanger];
    }
}
-(void)setRedCircle:(NSTimer*)timer {
    RedCircle2.hidden = YES;
    [self buttonChanger];

}
-(void)buttonChanger{
    ButtonTime = 0;

    RandomNumberButton = arc4random_uniform(100);

    if (RandomNumberButton>75) {
        RedCircle2.hidden = YES;
        YellowCircle.hidden = YES;
        BlueCircle.hidden = NO;
        [NSTimer scheduledTimerWithTimeInterval:[self generateButtonTime] target:self selector:@selector(enableBlueCircle:) userInfo:nil repeats:NO];



    }
    if (RandomNumberButton<30){


        RedCircle2.hidden = YES;
        BlueCircle.hidden = YES;

        YellowCircle.hidden = NO;

        [NSTimer scheduledTimerWithTimeInterval:[self generateButtonTime] target:self selector:@selector(enableYellowCircle:) userInfo:nil repeats:NO];

        }

    if (RandomNumberButton>30) {
        if (RandomNumberButton<75) {

        RedCircle2.hidden = NO;
        BlueCircle.hidden = YES;
        YellowCircle.hidden = YES;
        [NSTimer scheduledTimerWithTimeInterval:[self generateButtonTime] target:self selector:@selector(setRedCircle:) userInfo:nil repeats:NO];
        }
    }

    }
- (IBAction)RedCircleButton:(id)sender {
    RandomNumber = 0;
    score = score + 1;
    scoreString = [NSString stringWithFormat:@"Score:%i", score];
    scoreLabel.text = scoreString;
    AudioServicesPlaySystemSound(PlaySoundID);


}



- (IBAction)BlueCircleButton:(id)sender {
    [self Gameover];
}
-(void)Gameover{
    GameOver.hidden = NO;
    BlueCircle.enabled = NO;
    BlueCircle.hidden = YES;
    YellowCircle.enabled = NO;
    YellowCircle.hidden = YES;
    RedCircle2.hidden = YES;
    RedCircle2.enabled = NO;
    Menu.hidden = NO;
    scorelabeldisplay.hidden = NO;
    scorelabeldisplay.text = scoreString;
    if (score > highscoreint) {
        highscoreint = score;
    }
    highscore.hidden = NO;
    HighScoreString = [NSString stringWithFormat:@"Highscore:%i", highscoreint];

    highscore.text = HighScoreString;
    scoreLabel.hidden = YES;

}


- (IBAction)YellowCircleButton:(id)sender {
    [self Gameover];
}

- (IBAction)PlayAction:(id)sender {
    [self buttonChanger];
    Play.hidden = YES;

}



@end

2 个答案:

答案 0 :(得分:1)

这条线听起来很奇怪:

if (RedCircle2.hidden = YES, BlueCircle.hidden = YES, YellowCircle.hidden = YES) 

您正在为RedCircle2,BlueCircle和YellowCircle的隐藏属性分配YES,并且它将始终为TRUE作为布尔表达式....

你可能想这样做:

if (RedCircle2.hidden && BlueCircle.hidden && YellowCircle.hidden)

答案 1 :(得分:1)

在您的if声明中,您不进行比较即可进行分配。 =是赋值运算符,==是比较运算符。这也是我第一次看到这个,如果用逗号分隔多个语句,它似乎是罪魁祸首。

正确的if应该是这样的:

if (BlueCircle.hidden == YES && RedCircle.hidden == YES && YellowCircle.hidden == YES) {

也就是说,如果你想检查是否所有这些都被隐藏了。如果只有一个就足够了,您可以使用逻辑OR运算符||