【问题标题】:Assigning random image with arc4random?用 arc4random 分配随机图像?
【发布时间】:2010-02-19 02:39:06
【问题描述】:

好的,我会尽量让这一点更清楚,因为我的最后一个问题非常令人困惑。这次我附上了一张照片。这些圆圈中的每一个都是一个 UIImageView 并且它们每个都被分配了一个随机图像,该图像是 7 种颜色之一。所以每个圆圈可能是 7 种颜色中的一种。我想做到这一点,以便用户必须根据颜色按照预先确定的顺序点击圆圈。例如,蓝色、绿色、黄色、粉色、紫色、橙色、红色。我的巨大问题是我似乎无法弄清楚如何确定不应该被击中的颜色是否被击中。有没有办法给多个图像视图分配相同的值,然后以某种方式有一个 if 语句说....

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

【问题讨论】:

  • 哈哈是的,你能帮我吗,哈哈,这让我发疯了

标签: iphone image sdk uiimage imageview


【解决方案1】:

我认为创建一种更合理的方式来将不同颜色的图像分配给您的图像视图也是明智之举,因此此解决方案可以兼得。

这些应该在类的头部声明

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
        }

    }

}

【讨论】:

  • circlesByColor 包含一堆会泄漏的数组。它们是 init(保留计数 1),然后由 circlesByColor 数组保留(保留计数 2)。当circlesByColor 被释放时,数组也被释放(保留计数 1),并且您不再拥有对它们的引用并且它会泄漏。这些数组应该像这样创建:[NSMutableArray array]
【解决方案2】:

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

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

但是,您应该获得四个 UIImage 引用,然后使用它们。您还应该使用数组和循环或至少一个函数来使这段代码更短更简单。

实际上,您可能希望您的四个 UIImage 引用成为实例变量,以便您稍后可以将点击的 UIImageView 对象的image 属性与它们进行比较。

【讨论】:

  • 我用图片和更多代码更新了我的代码。我现在得到了一次引用,但我仍然不知道如何检查图像是否仍然存在于图像视图中?
  • 您可能应该拥有一个跟踪游戏状态的数据模型,以便您可以根据需要应用任何逻辑。
【解决方案3】:

如果您的目的只是 if 子句,那么我要做的就是定义两个字典,其中键作为颜色,值作为 burbles 数组,值是 burble 的位置。

NSDictionary *hitDictionaryOfBurbles;
NSDictionary *notCurrentlyBeingHitDictionaryOfBurbles;

定义了这本字典后,您就拥有了当前未命中的所有颜色的所有项目。然后,您可以轻松地检查此字典橙色数组的值,以查看有多少项。

当然,一旦击中 burble,您应该更改这两个字典的值以反映更改。

Es.

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

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

【讨论】:

    【解决方案4】:

    您是否总是要按一定的顺序,一个接一个地,一次只有一种颜色有效?如果是这样,您可以创建一个由颜色按顺序组成的UIImages 堆栈(我认为没有内置的堆栈数据类型,但您可以使用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.png、color2.png 等并说@ 987654328@

    【讨论】:

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