views:

520

answers:

2

I have a text in my game which is written in cocos2d. The text moves up and down while we touch and move it up or down. The text is moving. But it is not getting to original position while touches ended. So, I wrote cade for touches ended for text to get original position. But problem is i could not read the entire text now. What I need is scrolling effect. How can I make it in 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;
}
A: 

I need the answer too.

I have posted the answer. Give me any suggestions of code. And if possible clear my problem I stated in the answer too.Thank You.
srikanth rongali
A: 

This creates a textView on cocos2d layer and data will be scrolled only top and down. But I have a problem with this. I am no able to use the custom fonts for the text in textView like "Copperplate Gothic Bold". I am able to use only system fonts. But the code above we can use any font.

-(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];
}  

To remove it again you we have to use the following code

NSArray *subviews = [[[CCDirector sharedDirector]openGLView] subviews];
for (id sv in subviews)
{
    [((UIView *)sv) removeFromSuperview];
    [sv release];
}  

I hope this helps you. Any suggestions please post.

Thank you.

srikanth rongali