2011-12-29 12 views
0

私はユーザーが自分の車をゲームにしたい色を選択させるiPhoneゲームを作っています。どの車を選択できるかを示すために、さまざまな車種の背景画像を持つUIButtonsがあります。どの車が現在選択されているかを示すために、背景色が黄色の現在の車色の背後にあるボタンがあります。私がしたいのは、車のボタンをクリックすると、クリックされたボタンの後ろに黄色のボタンが移動するということです。私のコードは次のようになります:UIButtonは境界をプログラム的に変更しません

-(void)setPlayerCar:(UIButton *)newCar{ 
    //this code is being called when any of the buttons is clicked 
    NSArray *carFiles = [NSArray arrayWithObjects:@"redCar.png",@"blueCar.png",@"greenCar.png",@"purpleCar.png",@"turquioseCar.png",@"yellowCar.png", nil]; 
    NSString *file = [carFiles objectAtIndex:newCar.tag]; 
    currentCarImage = file; 
    CGRect frame; 
    if(currentCarImage == @"redCar.png"){ 
     frame = CGRectMake(48, 78, 30, 30); 
    } 
    if(currentCarImage == @"blueCar.png"){ 
     frame = CGRectMake(83, 78, 30, 30); 
    } 
    if(currentCarImage == @"greenCar.png"){ 
     frame = CGRectMake(118, 78, 30, 30); 
    } 
    if(currentCarImage == @"purpleCar.png"){ 
     frame = CGRectMake(153, 78, 30, 30); 
    } 
    if(currentCarImage == @"turquioseCar.png"){ 
     frame = CGRectMake(188, 78, 30, 30); 
    } 
    if(currentCarImage == @"yellowCar.png"){ 
     frame = CGRectMake(223, 78, 30, 30); 
    } 
    for(UIButton *button in self.pauseScroll.subviews){ 
     if(button.backgroundColor == [UIColor yellowColor]){ 
      button.bounds = frame; 
     } 
     if(button.tag == newCar.tag){ 
      [self.pauseScroll bringSubviewToFront:button]; 
     } 
    } 
} 

私が知る限り、これは黄色のボタンを選択されたボタンに移動する必要があります。問題はこれが起こらないということです。デバッグすると正しいボタンが認識されていて、そのフレームに適切な値が与えられていて、その行にbutton.bounds = frame;表示されているものを見ると、何も変わっていません。

+1

'currentCarImage == @ "redCar.png"'これはあなたが必要な文字列を比較する方法ではありません '[currentCarImage isEqualToString: "redCar.png" @]' –

+0

私も 'if'ステートメントを変更することを検討します毎回すべてのテストを実行しないように 'else if'ステートメントに追加します。また、 'carFiles'配列は、他の場所に置かれている可能性が最も高いですivar –

答えて

1

boundsではなく、ボタンのframeを変更する必要があります。ビューのboundsは、ビューの位置とサイズを独自の座標空間で記述します。 frameは、スーパービューの座標空間内のサイズと位置です。

if (button.backgroundColor == [UIColor yellowColor]) { 
    button.frame = frame; 
} 
+0

ありがとう –

関連する問題