-7

I have a bug with my game about red circle, blue circle, and yellow circle. You can only touch the red circle. I have a random button selector that I called buttonChanger. Whenever this method is run it selects a random button and after a certain amount of time the button disappears and the method runs again. I am wondering if I made a statement

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

this is where I am stuck. If I just run this if statement the buttons will keep changing and it will continue to overlap the void until it just crashes. I am wondering if inside this if statement I can make another if statement that overrides the previous one so then everything will be fine and running and the void will not overlap. like so:

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

If you wish to see my code here it is:

#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
  • 5
    'Whenever this void is run" makes no sense. You need to spend some time with a good book on Objective-C and iOS. – zaph Aug 19 '15 at 14:43
  • thanks but that doesn't help my question – Blake Olson Aug 19 '15 at 14:45
  • Your terminology makes it clear that asking questions on Stack Overflow is not the place you need to be right now. You should find a good book or a series of online tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Aug 19 '15 at 19:44

2 Answers2

1

This line sounds strange:

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

You are assigning YES to hidden property of RedCircle2, BlueCircle and YellowCircle and this will always be TRUE as a boolean expression....

You probably wants to do this:

if (RedCircle2.hidden && BlueCircle.hidden && YellowCircle.hidden)
tomsoft
  • 4,448
  • 5
  • 28
  • 35
  • should I then make it redcircle2.hidden == YES? – Blake Olson Aug 19 '15 at 14:48
  • yes , but redcircle2.hidden == YES is equivalent to redcircle2.hidden – tomsoft Aug 19 '15 at 14:49
  • 1
    The principles of boolean zen say that you should never test a boolean for equality against true/false since a boolean itself is true/false. Use @tomsoft's suggestion at the end of his answer. – Braains Aug 19 '15 at 14:49
1

In your if statement you make assignements not comparisons. = is an assignment operator, == is a comparison operator. Also this is the first time I have seen this if with multiple statements separated with commas and it seems to be a culprit.

The correct if should look something like this :

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

That is, if you want to check if all of them are hidden. If only one is enough you can use the logical OR operator ||.

Losiowaty
  • 7,911
  • 2
  • 32
  • 47