Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I actually needed to do something similar, but instead of moving the images around, I decided instead to do this:</p> <ul> <li>Create three buttons where you want them to appear (predetermined locations).</li> <li>Assign the images to each button randomly (by randomizing a NSMutableArray with the NSStrings of the image names).</li> <li>Instead of assigning @selector(answerCorrect) and @selector(answerUncorrect), assign @selector(checkIfCorrect:)</li> <li>Define checkIfCorrect as such:</li> </ul> <blockquote> <p>-(void)checkIfCorrect:(id)sender{ UIImage *buttonImage = sender.image;</p> <p>if(buttonImage == [UIImage imageNamed:@"kncpf.png"]){ [self answerCorrect]; } else { [self answerIncorrect]; }</p> <p>}</p> </blockquote> <p>EDITED TO INCLUDE THE CODE I RECOMMEND:</p> <p>Also, you are calling</p> <pre><code>int length02 = [arrayPossiblePositions count]; int chosen02 = arc4random() % length02; [arrayPossiblePositions removeObjectAtIndex:chosen02]; int chosen04 = arc4random() % length02; [arrayPossiblePositions removeObjectAtIndex:chosen04]; int chosen05 = arc4random() % length02; </code></pre> <p>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!</p> <p>I have not tested, but should work: (dont forget to define checkanswer() as I mentioned above)</p> <pre><code>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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload