如何在Cocos2D中滚动标签上的字符



我有一个精灵,上面有一个标签(CCLabelTTF)。在那个标签上,我有A,B,C,D,E等打印当他们点击。我想让他们左滚动。我已经谷歌了一些教程,但我无法找到解决方案,并在这里停留了很长时间。这是我的游戏截图。您可以看到从A到j的字符。当我点击更多输入字符时,该部分应该滚动。我怎么做才能使字符滚动?

下面是显示在添加到精灵上的标签(lblSelectedAlpha)上的字符的代码:-

-(void)showSelectedAlphaBet{
    fSelectedAlphaY =26;     
    if (tempBackground) {       
        [tempBackground removeFromParentAndCleanup:YES];
    }   
    tempBackground = [CCSprite spriteWithFile:@"stripe_2.png"];
    tempBackground.position = ccp(160,30);
    [self addChild:tempBackground z:30];        
    for (int i=0; i<[arryAddSelectedChar count]; i++) {     
       // NSLog(@"[arryAddSelectedChar count] %@",[arryAddSelectedChar objectAtIndex:i]);       
        lblSelectedAlpha = [CCLabelTTF labelWithString:
                        [arryAddSelectedChar objectAtIndex:i]dimensions:CGSizeMake(30,30)
                                         alignment:UITextAlignmentCenter  fontName:@"Marker Felt" fontSize:30];     
        lblSelectedAlpha.position = ccp(fSelectedAlphaY,25);
        lblSelectedAlpha.tag=i;
        lblSelectedAlpha.color = ccc3(125,125,125);
        [tempBackground addChild:lblSelectedAlpha z:5];     
        fSelectedAlphaY +=25;     
    }
}

滚动只是位置随时间的不断变化。基本上是这样的:

-(void) update:(ccTime)delta
{
   label.position = CGPointMake(label.position.x - 1, label.position.y);
}

这就是我在cocos2d游戏中实现滚动的方法

//如何添加滚动标签。这是用UITextView

完成的

//在init中,我取

{

    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10,430, 150, 50)]
    alphabetScroll = [[UITextView alloc] initWithFrame:CGRectMake(10,0,50 ,50)];
    alphabetScroll.font = [UIFont fontWithName:@"verdana" size:30.0f];
    alphabetScroll.backgroundColor = [UIColor clearColor];
    alphabetScroll.textColor = [UIColor colorWithRed:125.0/255.0 green:125.0/255.0 blue:125.0/255.0 alpha:1.0f];
    alphabetScroll.userInteractionEnabled=NO;
    [scroll addSubview:alphabetScroll];
    [[[CCDirector sharedDirector]openGLView]addSubview:scroll]; 

}

- (void) showSelectedAlphaBet

{

NSLog(@"showSelectedAlphaBet");
fSelectedAlphaY =26;   // I have 26 alphabets
if (tempBackground) {
    [tempBackground removeFromParentAndCleanup:YES];
}

//在触摸

时添加字母的区域
tempBackground = [CCSprite spriteWithFile:@"stripe_2.png"];
tempBackground.position = ccp(160,30);
[self addChild:tempBackground z:30];
bottomAlphabet = @" ";
for (int i=0; i<[arryAddSelectedChar count]; i++) {
    NSLog(@"[arryAddSelectedChar count] %@",[arryAddSelectedChar objectAtIndex:i]);
    bottomAlphabet = [bottomAlphabet stringByAppendingString:[arryAddSelectedChar objectAtIndex:i]];
}

//实现移动/滚动

int newScrollViewWidth;
int newLabelWidth;
newScrollViewWidth =25*[arryAddSelectedChar count];
newLabelWidth =50*[arryAddSelectedChar count];
[scroll setContentSize:CGSizeMake(newScrollViewWidth, 45)];
alphabetScroll.frame =  CGRectMake(0, 0, newLabelWidth, 45);
alphabetScroll.text = bottomAlphabet;
if (newScrollViewWidth > 150) {
    CGPoint svos;
    CGPoint pt;
    svos = scroll.contentOffset;
    CGRect rc = [alphabetScroll bounds];
    rc = [alphabetScroll convertRect:rc toView:scroll];
    pt = rc.origin;
    pt.y = 0;
    pt.x += 20*([arryAddSelectedChar count]-5);
    [scroll setContentOffset:pt animated:YES];       
}

}

//在dealloc

[alphabetScroll release];

最新更新