【问题标题】:Randomize object position随机化对象位置
【发布时间】:2010-05-11 15:13:19
【问题描述】:

我正在制作一个小测验式的应用程序,但我遇到了一些问题。

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

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

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

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

提前感谢您的回答, 大卫

【问题讨论】:

    标签: iphone objective-c random position


    【解决方案1】:

    我实际上需要做类似的事情,但我没有移动图像,而是决定这样做:

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

    -(void)checkIfCorrect:(id)sender{ UIImage *buttonImage = sender.image;

    if(buttonImage == [UIImage imageNamed:@"kncpf.png"]){ [self 回答正确]; } 其他 { [自我 答案不正确]; }

    }

    已编辑以包含我推荐的代码:

    还有,你在打电话

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

    【讨论】:

    • 这听起来不错,但现在的问题是我创建了一个带有图像名称的 NSMutableArray,但是即使 NSLog 确认一切正常,随机图像也不会显示在按钮中,如果我添加删除对象的选项,应用程序崩溃....这是我正在使用的代码:shorttext.com/0rr3vxr9te 我该怎么办?谢谢大佬
    • 检查我的补充:一个问题可能是您在创建按钮图像之前从数组中删除了对象。 NSString *laggo02 = [arrayPosizioniPossibili objectAtIndex:chosen02];可能只是创建一个指针,而不是在内存中创建一个全新的片段。
    • [button1 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index1]] forState:UIControlStateNormal]; - 这似乎不能正常工作,可能是因为它不是 NSString,但即使我编码: NSString *stringozzo01 = [immaginiSbagliate objectAtIndex: index1];然后将其分配给随机图像,它崩溃了你有什么建议吗?非常感谢您的时间和耐心
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多