2010-05-11 201 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是如何反應的。我不確定,試着用一個硬值來代替長度,然後看看你是否得到了預期的行爲,然後從那裏向後工作。