如何在 cocos2d 中为图层添加滚动?

Posted

技术标签:

【中文标题】如何在 cocos2d 中为图层添加滚动?【英文标题】:How to add Scrolling to a layer in cocos2d? 【发布时间】:2010-04-14 11:02:05 【问题描述】:

我的游戏中有一段文字是用 cocos2d 编写的。当我们触摸并上下移动文本时,文本会上下移动。文字在动。但是在触摸结束时它没有回到原始位置。所以,我写了 cade 以获取文本以获取原始位置。但问题是我现在无法阅读全文。我需要的是滚动效果。如何在 cocos2d 中制作它?

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event

    CGPoint touchLocation = [touch locationInView: [touch view]];   
    CGPoint prevLocation = [touch previousLocationInView: [touch view]];    

    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];

    CGPoint diff = ccpSub(touchLocation,prevLocation);

    CCNode *node = [self getChildByTag:kTagNode];
    CGPoint currentPos = [node position];
    [node setPosition: ccpAdd(currentPos, diff)];   


-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event

    CGSize windowSize = [[CCDirector sharedDirector] winSize];
    CCNode *node = [self getChildByTag:kTagNode];
    [node setPosition: ccp(text1.contentSize.width/2,text1.contentSize.height/2 - windowSize.height)];



-(id) init

    if((self = [super init]))
    
        CGSize windowSize = [[CCDirector sharedDirector] winSize];
        self.isTouchEnabled = YES;
        NSString *dataPath = @"/Users/sridhar/Srikanth/DrawGameSrikanth/ResourcestextData.txt";
        NSString *dataString = [[NSString alloc] initWithContentsOfFile:dataPath];
       CCLabel *text1 = [CCLabel labelWithString: dataString dimensions: CGSizeMake(300, 600)   alignment: UITextAlignmentLeft fontName:@"American Typewriter"  fontSize:24];
        text1.position = ccp(text1.contentSize.width/2 ,text1.contentSize.height/2 - windowSize.height); 
        text1.color = ccc3(0, 0, 0);
        CCNode *node = text1;
        CCParallaxNode *voidNode = [CCParallaxNode node];
        [voidNode addChild:node z:2 parallaxRatio:ccp(0.0f,1.0f) positionOffset:ccp(text1.contentSize.width/2,text1.contentSize.height/2 - windowSize.height)];         
        [self addChild: voidNode z:2 tag:kTagNode];
    
    return self;

【问题讨论】:

【参考方案1】:

这会在 cocos2d 图层上创建一个 textView,并且数据只会上下滚动。但这有一个问题。

我无法为 textView 中的文本使用自定义字体,例如“Copperplate Gothic Bold”。我只能使用系统字体。但是上面的代码我们可以使用任何字体。

-(id)init

if( (self = [super init]) )

    self.isTouchEnabled  = YES;
    CGSize windowSize = [[CCDirector sharedDirector] winSize];

    description = [[UITextView alloc] initWithFrame:CGRectMake(50,150,200 ,200)];
    description.backgroundColor = [UIColor clearColor];
    description.text = enemyDescription;  //enemyDescription is a string from plist in my code
    [description setEditable:NO]; 
    description.font = [UIFont fontWithName:@"Arial Unicode MS" size:14.0f];

    description.showsHorizontalScrollIndicator = NO;

    description.alwaysBounceVertical = YES;

    description.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS( 90.0f ));

    [[[CCDirector sharedDirector]openGLView]addSubview:description]; 
    [description release];
  

要再次删除它,我们必须使用以下代码

NSArray *subviews = [[[CCDirector sharedDirector]openGLView] subviews];
for (id sv in subviews)

    [((UIView *)sv) removeFromSuperview];
    [sv release];
  

我希望这对你有帮助。任何建议请张贴。

【讨论】:

以上是关于如何在 cocos2d 中为图层添加滚动?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iPhone 中为图层设置动画?

Cocos2D 图层中的自定义滚动

将 UIScrollView contentOffset 和 contentSize 转换为图层位置和边界属性

cocos2d中如何使用UITextView添加滚动文本?

如何在闪亮中为侧边栏面板添加滚动条?

Cocos2d 给图层添加背景图片?