8

在此处输入图像描述

正如我的屏幕截图显示我正在开发单词匹配游戏。在这个游戏中,我将我的单词分配给不同位置的特定序列中的不同 UIButtons(我的红色箭头显示这个序列)和其余 UIButtons 我分配一个随机字符( AZ)。当我点击任何 UIButtons 时,它的标题将分配给当前部分 Fornt 中的 UILabel:我将这个 UILabel 文本放在计时器前的 UILabel 文本下方。当它与我的任何 UILabel 匹配时,它会被删除。我已经实施了所有这些过程。

但我的问题是黑线显示的问题。如果玩家找到第一个单词是“DOG”。他单击序列中的两个 UIButtons,但不按序列中的第三个。(如黑线所示)。所以在这里我希望当玩家按下任何不在序列中的 UIButtons 时,删除前面的文本(即“ DO") 的 UILabel ,现在 UILabel 的 Text 只是 "G" 。这是我获取 UIButtons 标题并将其分配给 UILabel 的代码。

- (void)aMethod:(id)sender 
       {
    UIButton *button = (UIButton *)sender;
    NSString    *get = (NSString *)[[button titleLabel] text];
    NSString *origText = mainlabel.text;
    mainlabel.text = [origText stringByAppendingString:get];

 if ([mainlabel.text length ]== 3) 
    {
if([mainlabel.text isEqualToString: a]){
    lbl.text=@"Right";
    [btn1 removeFromSuperview];
    score=score+10;
    lblscore.text=[NSString stringWithFormat:@"%d",score];
    words=words-1;
    lblwords.text=[NSString stringWithFormat:@"%d",words];
    mainlabel.text=@"";
    a=@"tbbb";
}

    else    if([mainlabel.text isEqualToString: c]){
    lbl.text=@"Right";
    [btn2 removeFromSuperview];
    score=score+10;
    lblscore.text=[NSString stringWithFormat:@"%d",score];
    words=words-1;
    lblwords.text=[NSString stringWithFormat:@"%d",words];
    mainlabel.text=@"";
c=@"yyyy";

}
 else   
     if([mainlabel.text isEqualToString: d]){
    lbl.text=@"Right";
    [btn3 removeFromSuperview];
    score=score+10;
    lblscore.text=[NSString stringWithFormat:@"%d",score];
    words=words-1;
    lblwords.text=[NSString stringWithFormat:@"%d",words];
    mainlabel.text=@"";
    d=@"yyyy";
}
else {
    lbl.text=@"Wrong";
   mainlabel.text=@"";
  }

 }}

提前感谢

4

4 回答 4

2

有趣的问题:在这种特殊情况下,我同意 Luke 关于子类化 UIButton 的观点。这样,您可以在网格上为每个按钮提供一个 (X, Y),以及一个 (Xnext, Ynext) 列表,用于所有可能的预期下一次按下位置(如果按钮本身可用于制作多个单词)。在外部,您会将当前命中的值与预期值(Xnext、Ynext)进行比较。如果两者不匹配,这就是您正在寻找的信号。

这是一个解决您所有情况的答案,向前和向后水平(如果您选择实施后件),向上和向下垂直(如果您选择向上实施),以及任何对角线或任何其他组合,您可以想出和!

这也解释了让我们说击中 D,然后是 O,然后尝试再次按下 D 而不是击中 G。它还负责击中不正确的 G。

创建一个新的 .m .h 文件对(一个新对象)并为其命名。

一些用于实现自定义 UIButton(h 文件)的示例代码:

@interface myConnectedUIButton : UIButton {

    BOOL             isAWordBeginCharacter;
    unsigned int     beginWordKey;

    unsigned int     myGridX;
    unsigned int     myGridY;

    NSMutableArray * myConnectedSet;

}

-(id)init;
-(void)initWithGridX:(unsigned int)X GridY:(unsigned int)Y BeginChar:(BOOL)yesNo BeginWordKey:(unsigned int)key;
-(void)setGridPosWithX:(unsigned int)X Y:(unsigned int)Y;
-(void)setGridX:(unsigned int)X;
-(void)setGridY:(unsigned int)Y;
-(unsigned int)getGridX;
-(unsigned int)getGridY;

-(void)setIsABeginChar:(BOOL)yesNo;
-(BOOL)getIsABeginChar;

-(void)addPosToConnectedSetGridX:(unsigned int)X GridY:(unsigned int)Y WordKey:(unsigned int)key; 
-(NSArray *)getMyConnectedSetArray;
-(void)clearConnectedSet;

@end

在您的 .m 文件中

@implementation myConnectedUIButton

-(id)init{
    [super init];

    // Lets go ahead and initialize the NSMutableArray here also IFF it hasnt already been allocated
    if( nil == myConnectedSet ){
        myConnectedSet = [[NSMutableArray alloc] init];
    }

    // Lets also zero out the x, y position
    myGridX = 0;
    myGridY = 0;

    // Lets also state that this is NOT a begin char for the time being and 0 for the begin char key
    isAWordBeginCharacter = NO;
    beginWordKey = 0;

    return self;
}

-(void)initWithGridX:(unsigned int)X GridY:(unsigned int)Y BeginChar:(BOOL)yesNo BeginWordKey:(unsigned int)key{
    // Lets go ahead and initialize the NSMutableArray here also IFF it hasnt already been allocated
    if( nil == myConnectedSet ){
        myConnectedSet = [[NSMutableArray alloc] init];
    }

    myGridX = X;
    myGridY = Y;

    isAWordBeginCharacter = yesNo;
    beginWordKey = key;
}

-(void)setGridPosWithX:(unsigned int)X Y:(unsigned int)Y{
    myGridX = X;
    myGridY = Y;
}

-(void)setGridX:(unsigned int)X{
    myGridX = X;
}

-(void)setGridY:(unsigned int)Y{
    myGridY = Y;
}

-(unsigned int)getGridX{
    return myGridX;
}

-(unsigned int)getGridY{
    return myGridY;    
}

-(void)setIsABeginChar:(BOOL)yesNo{
    isAWordBeginCharacter = yesNo;
}

-(BOOL)getIsABeginChar{
    return isAWordBeginCharacter;
}

-(void)addPosToConnectedSetGridX:(unsigned int)X GridY:(unsigned int)Y WordKey:(unsigned int)key{
    [myConnectedSet addObject:[GridPointNext GridPointNextWithX:X GridPointY:Y  NextWordKey:key]];
}

-(NSArray *)getMyConnectedSetArray{
    return myConnectedSet;
}

-(void)clearConnectedSet{
    [myConnectedSet removeAllObjects];
}

-(void)dealloc{
    [myConnectedSet release];

    [super dealloc];
}

@end

您现在还需要一个“GridPointNext”对象。

网格对象标题应如下所示:

@interface GridPointNext : NSObject {
    unsigned int GridPointX;
    unsigned int GridPointY;

    unsigned int nextWordKey;
}

+(GridPointNext *)GridPointNextWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key;
-(id)initWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key;

-(unsigned int)getGridX;
-(unsigned int)getGridY;
-(unsigned int)getNextWordKey;

@end

对象的 m 文件应如下所示:

@implementation GridPointNext

+(GridPointNext *)GridPointNextWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key{
    GridPointNext * aPoint = [[GridPointNext alloc] initWithX:X GridPointY:Y NextWordKey:key];

    [aPoint autorelease];

    return aPoint;
}

-(id)initWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key{
    GridPointX  = X;
    GridPointY  = Y;

    nextWordKey = key;

    return self;
}


-(unsigned int)getGridX{
    return GridPointX;
}

-(unsigned int)getGridY{
    return GridPointY;
}

-(unsigned int)getNextWordKey{
    return nextWordKey;
}

@end

您将不得不处理 dealloc 部分。这至少为您提供了一些工具来创建自定义按钮和围绕它的单词列表算法。

于 2012-06-01T22:39:10.767 回答
2

从左到右为每个按钮分配标签。所以你会有,

GButton.tag = 0; TButton.tag = 1; DButton.tag = 2; . . . VButton.tag = 9; . . . EButton.tag = 18; . . . CButton.tag = 26;

现在跟踪先前按下的按钮和当前按下的按钮。当您的按钮委托点击时调用以下函数:

将以下代码写入您的 .h 文件

#define SEQ_TYPE_ANY  0
#define SEQ_TYPE_RIGHT 1
#define SEQ_TYPE_LEFT 2
#define SEQ_TYPE_TOP 3
#define SEQ_TYPE_BOTTOM 4
#define SEQ_TYPE_RIGHT_DIAGONAL_DOWN 5
#define SEQ_TYPE_RIGHT_DIAGONAL_UP 6
#define SEQ_TYPE_LEFT_DIAGONAL_DOWN 7
#define SEQ_TYPE_LEFT_DIAGONAL_UP 8

#define NO_OF_BUTTONS_IN_ROW 9

//Add below variables into your class
int curentSequence;
UILabel *resultLabel;
UIButton *previousButton;

//Declare property for previousButton
@property(nonatomic, retain) UIButton *previousButton;


//Write below code to .m file
@synthesize previousButton;

-(BOOL) isAdjacent:(UIButton *)currentButton
{
     if(previousButton == nil)
     {
          resultLabel.text = currentButton.titleLabel.text;
          curentSequence = SEQ_TYPE_ANY;
          return TRUE;
     }


     if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT) &&
          (previousButton.tag + 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_ANY;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT) &&
        (previousButton.tag - 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_LEFT;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_TOP) &&
             (previousButton.tag - NO_OF_BUTTONS_IN_ROW == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_TOP;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_BOTTOM) &&
             (previousButton.tag + NO_OF_BUTTONS_IN_ROW == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_BOTTOM;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT_DIAGONAL_DOWN) &&
             (previousButton.tag + NO_OF_BUTTONS_IN_ROW + 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_RIGHT_DIAGONAL_DOWN;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT_DIAGONAL_UP) &&
             (previousButton.tag -  NO_OF_BUTTONS_IN_ROW + 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_RIGHT_DIAGONAL_UP;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT_DIAGONAL_UP) &&
             (previousButton.tag - NO_OF_BUTTONS_IN_ROW - 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_LEFT_DIAGONAL_UP;
          return TRUE;
     }
     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT_DIAGONAL_DOWN) &&
             (previousButton.tag + NO_OF_BUTTONS_IN_ROW - 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_LEFT_DIAGONAL_DOWN;
          return TRUE;
     }
     else
     {
          resultLabel.text = @"";
          curentSequence = SEQ_TYPE_ANY;
          return FALSE;
     }     
}

// Event handler for button event
- (void)aMethod:(id)sender
{
     UIButton *currentButton = (UIButton *)sender;
     BOOL result = [self isAdjacent:currentButton];
     if(result == FALSE)
     {
          self.previousButton = nil;
          resultLabel.text = @"";
          curentSequence = SEQ_TYPE_ANY;
     }
     else
     {
          self.previousButton = sender;
     }
}

希望它会有所帮助。

于 2012-06-06T07:18:06.640 回答
1

如果我理解正确,您需要知道按下的按钮是否与先前按下的按钮相邻?使用此函数测试网格中的邻接性:

bool isAdjacent(UIButton* current, UIButton* previous) {
    if( !previous )
        return false;

    //Create a rectangle around the previous button (assuming all buttons are in a fixed grid)
    CGRect previousRect = previous.frame;
    CGRect adjacentRect = CGRectMake(previous.frame.origin.x - previous.frame.size.width,
                                     previous.frame.origin.y - previous.frame.size.height,
                                     previous.frame.size.width*3,
                                     previous.frame.size.height*3);
    return CGRectIntersectsRect(adjacentRect, previousRect);
}

并且仅在它们相邻时才附加,即:

- (void)aMethod:(id)sender 
{
    UIButton *button = (UIButton *)sender;
    static UIButton* previous = nil;
    NSString    *get = (NSString *)[[button titleLabel] text];
    NSString *origText = mainlabel.text;

   if( isAdjacent(button, previous) )
       mainlabel.text = [origText stringByAppendingString:get];

   previous = button;

   //Rest of function
}
于 2012-05-31T12:14:31.633 回答
0

如果我理解正确。如果选择的字母都彼此相邻,用户只能有一个正确的单词?

您是否尝试过:保留两个参考。一个 UIButton 用于 previousPressedButton,一个 UIButton 用于 lastPressedButton。

首先用户按下 D。lastPressedButton 将引用 D。然后用户按下 O。previousPressedButton 将为 D。lastPressedButton 将为 O。

现在,获取 UIButtons 的宽度和高度,并比较 lastPressedButton.frame.origin.x 是否小于宽度或 -width 距离。还要检查 lastPressedButton.frame.origin.y 是否会小于 height 或 -height 。现在您知道它是否触摸了上一个按钮。使用它来确定它是否是一个新词。

我会把它放到一个方法中。

    -(BOOL)isAdjacentLetter {
          float buttonWidth = lastPressedButton.size.width;
          float buttonHeight = lastPressedButton.size.height;
          if(lastPressedButton.frame.origin.x>previousPressedButton.frame.origin.x+buttonWidth) return NO;
          if(lastPressedButton.frame.origin.y>previousPressedButton.frame.origin.y+buttonHeight) return NO;
          if(lastPressedButton.frame.origin.x<previousPressedButton.frame.origin.x-buttonWidth) return NO;
          if(lastPressedButton.frame.origin.y<previousPressedButton.frame.origin.y-buttonHeight) return NO;

          return YES;
}

然后每当单击按钮时。您可以使用

if ([self isAdjacentLetter]) {
   //still same word
} else {
   //new word. erase
}

或者,如果我理解不同的话。只有字母排成一行时才能造字。如:从左到右。从下到上。从右下角到左上角等。在这种情况下,确定所有方向。如左上为0,上为1,右上为2。右为3。右下为4。下为5。左下为6。左为7。

单击两个按钮时,存储方向。例如:如果是从左到右,direction = 3;然后当单击另一个按钮时,检查新方向。如果是 3,那么这个词仍然是同一个方向。如果是别的东西,那么擦除并重新开始。

希望这可以帮助。

于 2012-06-02T08:01:42.920 回答