2011-11-10 8 views
2

ココス2dの指タッチでラインを描きたいです。ココス2dドローラインが動作しません

-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [inappropriateTouches anyObject]; 

    CGPoint currentTouchArea = [touch locationInView:[touch view] ]; 
    CGPoint lastTouchArea = [touch previousLocationInView:[touch view]]; 

    currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea]; 
    lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea]; 

    // throw to console my inappropriate touches 
    NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y); 
    NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y); 

    glColor4f(0.8, 1.0, 0.76, 1.0); 
    glLineWidth(6.0f); 
    ccDrawLine(currentTouchArea, lastTouchArea); 
} 

私はこのコードを使用しますが、画面には何も描画されません。私のコードで何が間違っていますか?

答えて

4

描画メソッドで行うすべてのOpenGL描画。このように:

-(void)draw 
{ 
    if(lastTouchArea != 0) 
    { 
     glColor4f(0.8, 1.0, 0.76, 1.0); 
     glLineWidth(6.0f); 
     ccDrawLine(currentTouchArea, lastTouchArea); 
     lastTouchArea = 0; 
    } 
} 
+0

を行を保存するためにそれは動作しますが、新規に描き、なぜとき古いものは削除されていますか? –

+6

画面がリフレッシュされるたびにクリアされるためです。線を永続化するには、描画する線を表す点の配列を格納する必要があります。 –

2

これを試してみてください:NSMutableArrayの

-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event 
{ 
    UITouch *touchMyMinge = [inappropriateTouches anyObject]; 

    CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ]; 
    CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]]; 

    // flip belly up. no one likes being entered from behind. 
    currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea]; 
    lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea]; 

    // throw to console my inappropriate touches 
    NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y); 
    NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y); 

    // add my touches to the naughty touch array 
    naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)]; 
    naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)]; 
} 

@implementation DrawMyTouch

-(id) init 
{ 
    if((self=[super init])) 
    { } 
    return self; 
} 

-(void)draw 
{ 
    glEnable(GL_LINE_SMOOTH); 

    for(int i = 0; i < [naughtyTouchArray count]; i+=2) 
    { 
     start = CGPointFromString([naughtyTouchArray objectAtIndex:i]); 
     end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]); 

     ccDrawLine(start, end); 
    } 
} 


-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    DrawMyTouch *line = [DrawMyTouch node]; 
    [self addChild: line]; 
} 

ホープこのヘルプ

関連する問題