滑动方向和循环文本字符串

时间:2011-09-02 14:57:54

标签: iphone text loops swipe

帮助我......我被困住了。我正在尝试制作简单的游戏,比如愤怒的小鸟滑动。我不知道如何编码。我有两个标签和滑动移动代码(向上,向下,向左或向右,任何方向)。

我想在label2.text上使用较短的数字(“3758”)传输label1.text(“333333777777555555888888”)。所以我可以让人工智能游戏变得更好。  我该怎么做?

这是代码。

      for (NSValue *point_value in TouchRecord) {
             CGPoint current_point = [point_value CGPointValue];
             NSUInteger idx = [self subregionIndexContainingPoint:current_point];

             //collect string on label2
             NSString *numValue = [[NSString alloc] initWithFormat:@"%d", idx];
             label1.text = [label1.text  stringByAppendingString:numValue];


       }

感谢。

1 个答案:

答案 0 :(得分:0)

我不完全确定这是你想要的,但我认为是。

要缩短字符串,以便每个数字中只有一个使用此函数。

+(NSString*) removeDuplicateNumbersFromString:(NSString*)first{
    //if first is "1112222334445" the return value of this function would be "12345"

    NSString *end = [NSString stringWithString:@""]; //the return string
    char last; //will track changes in numbers

    for (int i = 0; i < [first length]; i++) {
        char charAtIndex = [first characterAtIndex:i]; 
        if (last != charAtIndex) {
            //if the last character is different than the current character
            //set the current character as last, and add that character to the return string
            last = charAtIndex;
            end = [end stringByAppendingFormat:@"%c",last];
        }
     }
     NSLog(@"First:%@, End:%@",first,end); //prints out the start/end strings
     return end;
}