Randomize object position
I'm making a little quiz-style application but I've got a few issues.
I random the questions from a NSMutableArray using arc4random(), then I populate the view with 3 buttons, one which includes a correct answer and the other 2 include two wrong answers
what I need to do is to randomize the X coordinate (position) of the 3 buttons in the view this is the code I'm using, but it gives me problems as it doesn't work properly nd the app often crashes when calling the action:
NSBundle *bundle02 = [NSBundle mainBundle];
NSString *textFilePath02 = [bundle02 pathForResource:@"possiblePositions" ofType:@"txt"];
NSString *fileContents02 = [NSString stringWithContentsOfFile:textFilePath02 encoding:NSUTF8StringEncoding error:nil];
arrayPossiblePositions = [[NSMutableArray alloc] initWithArray:[fileContents02 componentsSeparatedByString:@"\n"]];
int length02 = [arrayPossiblePositions count];
int chosen02 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen02];
int chosen04 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen04];
int chosen05 = arc4random() % length02;
if ([questionString isEqualToString:@"question1"]) {
buttonCorrect = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(chosen02, 80, 130, 130);
buttonCorrect.frame = newSize;
[buttonCor开发者_JAVA百科rect setImage:[UIImage imageNamed:@"kncpf.png"] forState:UIControlStateNormal];
[buttonCorrect addTarget:self action:@selector(answerCorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonCorrect];
buttonUncorrect01 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize02 = CGRectMake(chosen04, 80, 130, 130);
buttonUncorrect01.frame = newSize02;
[buttonUncorrect01 setImage:[UIImage imageNamed:@"kncpf02.png"] forState:UIControlStateNormal];
[buttonUncorrect01 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonUncorrect01];
buttonUncorrect02 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize034578 = CGRectMake(chosen05, 80, 130, 130);
buttonUncorrect02.frame = newSize034578;
[buttonUncorrect02 setImage:[UIImage imageNamed:@"kncpf034578.png"] forState:UIControlStateNormal];
[buttonUncorrect02 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonUncorrect02];
}
can you suggest me doing something different, because I'm really getting crazy ?
Thanks in advance for answers, David
I actually needed to do something similar, but instead of moving the images around, I decided instead to do this:
- Create three buttons where you want them to appear (predetermined locations).
- Assign the images to each button randomly (by randomizing a NSMutableArray with the NSStrings of the image names).
- Instead of assigning @selector(answerCorrect) and @selector(answerUncorrect), assign @selector(checkIfCorrect:)
- Define checkIfCorrect as such:
-(void)checkIfCorrect:(id)sender{ UIImage *buttonImage = sender.image;
if(buttonImage == [UIImage imageNamed:@"kncpf.png"]){ [self answerCorrect]; } else { [self answerIncorrect]; }
}
EDITED TO INCLUDE THE CODE I RECOMMEND:
Also, you are calling
int length02 = [arrayPossiblePositions count];
int chosen02 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen02];
int chosen04 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen04];
int chosen05 = arc4random() % length02;
Notice that length02 remains the same, while the size of arrayPossiblePositions changes. This is probably the first reason why your code crashes: you are trying to remove an index from an array which is outside of the array count!
I have not tested, but should work: (dont forget to define checkanswer() as I mentioned above)
NSMutableArray *imagesArray = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
int count1 = [imagesArray count];
int index1 = arc4random() % count1;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(30, 80, 130, 130);
button1.frame = newSize;
[button1 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index1]] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
[imagesArray removeObjectAtIndex:index1];
int count2 = [imagesArray count];
int index2 = arc4random() % count2;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(160, 80, 130, 130);
button2.frame = newSize;
[button2 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index2]] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
[imagesArray removeObjectAtIndex:index2];
int count3 = [imagesArray count];
int index3 = arc4random() % count3;
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(290, 80, 130, 130);
button3.frame = newSize;
[button3 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index3]] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
I think arc4random () needs an upper bounds. Not sure if the modulo use is correct here or more likely a semantic error, not sure how arc reacts when you try and perform an operation like that without first setting it's upper bounds. I'm not sure, try subbing in a hard value instead of your length02 and see if you're getting the expected behavior, then work backwards from there.
精彩评论