用arc4random指定随机图像?

问题描述:

好吧,我会尽量让这个更清楚,因为我最后一个问题非常混乱。这次我收录了一张照片。这些圆圈中的每一个都是UIImageView,并且每个都分配了一个随机图像,它是7种颜色之一。所以每个圈子可以是7种颜色之一。我想这样做,以便用户必须根据颜色按照预定顺序点击圆圈。例如,蓝色,绿色,黄色,粉红色,紫色,橙色,红色。我的巨大问题是,我似乎无法弄清楚如何确定不应该被击中的颜色是否被击中。是否有分配多个图像的方式享有相同的值,然后以某种方式有一个声明,如果说....用arc4random指定随机图像?

if(a blue circle is hit && ANY ORANGE CIRCLE IS STILL IN VIEW){ 
do something 
} 

我知道如何编写代码,这将是代码的疯狂量的唯一方法,因为的所有随机图像被分配。

bluebubble = [UIImage imageNamed:@"bluebubble.png"]; 
    greenbubble = [UIImage imageNamed:@"greenbubble.png"]; 
    redbubble = [UIImage imageNamed:@"redbubble.png"]; 
    yellowbubble = [UIImage imageNamed:@"yellowbubble.png"]; 
    orangebubble = [UIImage imageNamed:@"orangebubble.png"]; 
    pinkbubble = [UIImage imageNamed:@"pinkbubble.png"]; 
    purplebubble = [UIImage imageNamed:@"purplebubble.png"]; 


    image1 = arc4random()%7; 

    if(image1 == 0){ 
     [test setImage:bluebubble]; 
    } 
    else if(image1 == 1){ 
     [test setImage:greenbubble]; 
    } 
    else if(image1 == 2){ 
     [test setImage:redbubble]; 
    } 
    else if(image1 == 3){ 
     [test setImage:yellowbubble]; 
    } 
    else if(image1 == 4){ 
     [test setImage:orangebubble]; 
    } 
    else if(image1 == 5){ 
     [test setImage:pinkbubble]; 
    } 
    else if(image1 == 6){ 
     [test setImage:purplebubble]; 
    } 

    image2 = arc4random()%7; 

    if(image2 == 0){ 
     [test2 setImage:bluebubble]; 
    } 
    else if(image2 == 1){ 
     [test2 setImage:greenbubble]; 
    } 
    else if(image2 == 2){ 
     [test2 setImage:redbubble]; 
    } 
    else if(image2 == 3){ 
     [test2 setImage:yellowbubble]; 
    } 
    else if(image2 == 4){ 
     [test2 setImage:orangebubble]; 
    } 
    else if(image2 == 5){ 
     [test2 setImage:pinkbubble]; 
    } 
    else if(image2 == 6){ 
     [test2 setImage:purplebubble]; 
    } 

alt text

+1

wheeee这可爱:) – willcodejavaforfood 2010-02-19 08:30:46

+0

哈哈是啊,你能帮我吗虽然哈哈,这是让我疯狂的 – NextRev 2010-02-19 08:50:30

我认为创建一个更合理的方式将不同颜色的图像分配给图像视图也是明智之举,所以此解决方案既可以实现这一点。

这些应该在类

NSArray *allCircleImagesViews; // These are suppose to be the onscreen UIImagesViews 

NSArray *circlesByColor; 
NSMutableArray *correctCircles; // The current circles the user is allowed to click 
NSArray *colorOrder; // The order of the colors to click 
int currentColorIndex; // The current index in the order of colors 

现在的功能的报头中声明: 第一个创建分配不同的彩色图像,设置颜色的正确的顺序,并设定机构确定正确的颜色被点击

- (void) populateImages 
{ 
    NSArray *images = [NSArray arrayWithObjects: 
         [UIImage imageNamed:@"bluebubble.png"], 
         [UIImage imageNamed:@"redbubble.png"], 
         [UIImage imageNamed:@"yellowbubble.png"], 
         [UIImage imageNamed:@"greenbubble.png"], 
         [UIImage imageNamed:@"orangebubble.png"], 
         [UIImage imageNamed:@"pinkbubble.png"], 
         [UIImage imageNamed:@"purplebubble.png"], 
         nil]; 

    NSArray *circlesByColor=[NSArray arrayWithObjects: 
          [NSMutableArray array], 
          [NSMutableArray array], 
          [NSMutableArray array], 
          [NSMutableArray array], 
          [NSMutableArray array], 
          [NSMutableArray array], 
          [NSMutableArray array], 
          nil]; 
    [circlesByColor retain]; 

    // Assign images to circles and set the 
    for (UIImageView *currCircle in allCircleImagesViews) 
    { 
     int nextIndex = arc4random()%7; 
     currCircle.image = [images objectAtIndex:0]; 
     [(NSMutableArray *)[circlesByColor objectAtIndex:nextIndex] addObject:currCircle]; 
    } 

    // Set the correct order 
    NSArray *colorOrder = [NSArray arrayWithObjects: 
        [NSNumber numberWithInt:5], // Pink 
        [NSNumber numberWithInt:0], // Blue 
        [NSNumber numberWithInt:1], // etc. 
        [NSNumber numberWithInt:4], 
        [NSNumber numberWithInt:2], 
        [NSNumber numberWithInt:3], 
        [NSNumber numberWithInt:6],nil]; 
    [colorOrder retain]; 

    currentColorIndex = 0;    
    correctCircles = [circlesByColor objectAtIndex:[[colorOrder objectAtIndex:currentColorIndex] intValue]]; 
} 

下面的函数需要检查,如果被点击了正确的圆弧照顾

- (void) checkCircle:(UIImageView *)clickedImageView 
{ 
    BOOL result; 

    if ([correctCircles containsObject:clickedImageView]) 
    { 
     [correctCircles removeObject:clickedImageView]; 
     result = YES; 
    } 
    else { 
     result = NO; 
    } 


    if ([correctCircles count] == 0) 
    { 
     currentColorIndex++; 
     if (currentColorIndex < 7) 
      correctCircles = [circlesByColor objectAtIndex:[[colorOrder objectAtIndex:currentColorIndex] intValue]]; 
     else 
      correctCircles = nil; 
    } 

    if (!result) 
    { 
     // Wrong circle clicked logic 
    } 
    else { 
     if (!correctCircles) 
     { 
      // Game won logic 
     } 
     else { 
      // Correct circle clicked logic 
     } 

    } 

} 
+0

'circlesByColor'包含一堆会泄漏的数组。他们做'init'(保留计数1),然后由'circlesByColor'数组保留(保留计数2)。当'circlesByColor'被释放时,数组也被释放(保留计数1),并且你不再有对它们的引用并且它会泄漏。这些arays应该像这样创建:'[NSMutableArray array]' – 2010-02-28 19:34:47

+0

好的。谢谢。将编辑。 – 2010-02-28 19:38:17

是的,你可以的UIImageView的image属性设置为相同的UIImage。事实上,你已经在做这个了!

imageNamed:类方法将在用相同的图像文件名再次调用时返回对同一对象的引用。

但是,您应该一次获取四个UIImage引用,然后使用这些引用。你也应该使用一个数组和循环或者至少一个函数来使这个代码更简短。

实际上,它听起来像您希望您的四个UIImage引用是实例变量,以便您可以稍后比较点击的UIImageView对象的image属性。

+0

我更新了我的代码与图片和一些更多的代码。我现在得到一次引用,但我仍然不知道如何检查图像视图中是否仍然存在图像? – NextRev 2010-02-19 08:46:44

+0

您应该拥有一个跟踪游戏状态的数据模型,以便您可以根据需要应用任何逻辑。 – gerry3 2010-02-19 09:15:45

如果您的目的仅仅是if子句,那么我会做的是定义两个词典,其中键为颜色,值为burble的数组,其值为burble的位置。

NSDictionary *hitDictionaryOfBurbles; 
NSDictionary *notCurrentlyBeingHitDictionaryOfBurbles; 

随着本词典的定义,您可以随身携带所有目前未被击中的颜色。然后,您可以轻松地检查该字典橙色数组的值以查看有多少项。

Offcourse,一旦burble被击中,你应该改变这两个词典的价值来反映这个变化。

Es。

hitDictioanryOfBurbles: 
     "blue", [0,7,13] 
     "green", [2,6,16] 
     etc... 
notCurrentlyBeingHitDictioanryOfBurbles: 
     "blue", [25,33,37] 
     "green", [19,27,29] 
     etc... 

如果您有其他支持的逻辑,那么您应该定义更多的数据结构。

你是否总是按照一定的顺序,一个接一个地点击一次,一次只有一种颜色有效?如果是这样,你可以创建一堆UIImage s(我不认为有内置的堆栈数据类型,但是你可以很容易地用NSMutableArray来模拟它),它由颜色组成。

您还可以在单​​独的NSMutableArray中跟踪屏幕上的所有色圆子视图。然后,每当用户点击了一圈,执行以下代码:

if(hitImageView.image == [imageStack peek]) { 
    //"hit the right one" logic 

    //you hit it, so remove it from the screen 
    [allImageViews removeObject:hitImageView]; 
    [hitImageView removeFromSuperView]; 

    //if it was the last imageview of the "correct" color... 
    BOOL wasLast = YES; 

    for(UIImageView *imageView in allImageViews) { 
     if(imageView.image == [imageStack peek]) { 
      wasLast = NO; 
      break; 
     } 
    } 

    //...then set the correct color to whatever is next 
    if(wasLast) 
     [imageStack pop]; 

    if(![imageStack hasItems]) 
     ;//"won the game" logic 

} 
else { 
    //"hit the wrong one" logic 
} 

(这是O(N)来确定剩余的圆,但如果你正在处理这样的小设置,那么它确实不如果你需要优化它,你当然可以跟踪每种颜色的剩余计数。)

在另一个说明中,如果不是使用那个巨大的if-else块,识别正确的颜色,您只需将文件命名为color1.pngcolor2.png等,并说[UIImage imageNamed:[NSString stringWithFormat:@"color%i.png", randNum]];