2010-02-19 16 views
0

好吧,我會盡量讓這個更清楚,因爲我最後一個問題非常混亂。這次我收錄了一張照片。這些圓圈中的每一個都是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

回答

0

我認爲創建一個更合理的方式將不同顏色的圖像分配給圖像視圖也是明智之舉,所以此解決方案既可以實現這一點。

這些應該在類

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

0

是的,你可以的UIImageView的image屬性設置爲相同的UIImage。事實上,你已經在做這個了!

imageNamed:類方法將在用相同的圖像文件名再次調用時返回對同一對象的引用。

但是,您應該一次獲取四個UIImage引用,然後使用這些引用。你也應該使用一個數組和循環或者至少一個函數來使這個代碼更簡短。

實際上,它聽起來像您希望您的四個UIImage引用是實例變量,以便您可以稍後比較點擊的UIImageView對象的image屬性。

+0

我更新了我的代碼與圖片和一些更多的代碼。我現在得到一次引用,但我仍然不知道如何檢查圖像視圖中是否仍然存在圖像? – NextRev 2010-02-19 08:46:44

+0

您應該擁有一個跟蹤遊戲狀態的數據模型,以便您可以根據需要應用任何邏輯。 – gerry3 2010-02-19 09:15:45

0

如果您的目的僅僅是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... 

如果您有其他支持的邏輯,那麼您應該定義更多的數據結構。

0

你是否總是按照一定的順序,一個接一個地點擊一次,一次只有一種顏色有效?如果是這樣,你可以創建一堆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]];