2010-05-11 111 views
2

我正在做一个小测验式的应用程序,但我有几个问题。随机化对象位置

我随意使用arc4random(),然后我填充3个按钮,其中一个包括一个正确的答案,另2视图从一个NSMutableArray的问题包括两个错误答案

什么,我需要做的是随机化视图中3个按钮的X坐标(位置)这是我正在使用的代码,但它给我带来问题,因为它无法正常工作nd该应用程序在调用动作时经常崩溃:

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; 
    [buttonCorrect 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]; 
} 

你能否建议我做一些不同的事情,因为我真的变得疯了?

预先感谢答案, 大卫

回答

1

我真正需要做同样的事情,但不是四处移动图像,我决定,而不是这样做:

  • 创建三个按钮,你希望它们出现(预定位置)。
  • 随机分配图像到每个按钮(通过随机化一个NSMutableArray与图像名称的NSStrings)。
  • 而不是指定@selector(answerCorrect)和@selector(answerUncorrect)的,分配@selector(checkIfCorrect :)
  • 定义checkIfCorrect这样:

- (无效)checkIfCorrect:(ID)发件人{UIImage * buttonImage = sender.image;

如果(画面按钮画面== [UIImage的 imageNamed:@ “kncpf.png”]){[自 answerCorrect]; } else {[self answerIncorrect]; }

}

编辑,包括代码中,我推荐:

此外,您呼叫

int length02 = [arrayPossiblePositions count]; 
int chosen02 = arc4random() % length02; 
[arrayPossiblePositions removeObjectAtIndex:chosen02]; 
int chosen04 = arc4random() % length02; 
[arrayPossiblePositions removeObjectAtIndex:chosen04]; 
int chosen05 = arc4random() % length02; 

注意length02保持不变,而arrayPossiblePositions的大小变化。这可能是你的代码崩溃的第一个原因:你试图从数组外的数组中删除一个索引!

我没有测试,但应该工作:(不要忘记定义checkanswer()正如我上面提到)

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]; 
+0

这听起来不错,但现在的问题是,我创建了一个带图像名称的NSMutableArray,但即使NSLog确认所有内容都正常工作,随机图像也不会显示在按钮中,并且如果添加了删除选项对象,应用程序崩溃....这是我使用的代码:http://shorttext.com/0rr3vxr9te 我该怎么办? 谢谢你 – 2010-05-11 18:39:57

+0

检查我的补充:一个问题可能是您在创建按钮图像之前从阵列中删除对象。 NSString * laggo02 = [arrayPosizioniPossibili objectAtIndex:chosen02];可能只是创建一个指针,而不是在内存中创建一个全新的部分。 – 2010-05-11 21:35:26

+0

[button1 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index1]] forState:UIControlStateNormal]; - 这似乎不工作正常,也许是因为它不是一个NSString,但即使我编码:NSString * stringozzo01 = [immaginiSbagliate objectAtIndex:index1];然后将其分配给随机图像,它会崩溃 你有什么建议吗? 非常感谢时间和耐心的人 – 2010-05-12 20:58:11

1

我认为arc4random()需要一个上限。不确定模数使用在这里是否正确,或者更可能是语义错误,当您尝试执行类似的操作而没有首先设置上限时,不确定arc是如何反应的。我不确定,试着用一个硬值来代替长度,然后看看你是否得到了预期的行为,然后从那里向后工作。